1 package org.onap.vid.job.impl;
3 import com.google.common.collect.ImmutableMap;
4 import org.onap.vid.exceptions.GenericUncheckedException;
5 import org.onap.vid.job.Job;
6 import org.onap.vid.job.JobsBrokerService;
7 import org.onap.vid.job.command.JobCommandFactory;
8 import org.onap.vid.properties.Features;
9 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
13 import org.springframework.stereotype.Component;
14 import org.togglz.core.manager.FeatureManager;
16 import javax.annotation.PostConstruct;
18 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
21 public class JobSchedulerInitializer {
23 private JobsBrokerService jobsBrokerService;
24 private SchedulerFactoryBean schedulerFactoryBean;
25 private FeatureManager featureManager;
26 private JobCommandFactory jobCommandFactory;
27 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
30 public JobSchedulerInitializer(
31 JobsBrokerService jobsBrokerService,
32 SchedulerFactoryBean schedulerFactoryBean,
33 FeatureManager featureManager,
34 JobCommandFactory JobCommandFactory
36 this.jobsBrokerService = jobsBrokerService;
37 this.schedulerFactoryBean = schedulerFactoryBean;
38 this.featureManager = featureManager;
39 this.jobCommandFactory = JobCommandFactory;
45 if (!featureManager.isActive(Features.FLAG_ASYNC_JOBS)) {
48 scheduleJobWorker(Job.JobStatus.PENDING, 1);
49 scheduleJobWorker(Job.JobStatus.IN_PROGRESS, 1);
52 private void scheduleJobWorker(Job.JobStatus topic, int intervalInSeconds) {
53 Scheduler scheduler = schedulerFactoryBean.getScheduler();
54 JobDetail jobDetail = JobBuilder.newJob().ofType(JobWorker.class)
55 .withIdentity("AsyncWorkersJob" + topic)
56 .withDescription("Job that run async worker for " + topic)
57 .setJobData(new JobDataMap(ImmutableMap.of(
58 "jobsBrokerService", jobsBrokerService,
59 "jobCommandFactory", jobCommandFactory,
60 "featureManager", featureManager,
64 Trigger asyncWorkerTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
65 .withIdentity("AsyncWorkersTrigger" + topic)
66 .withDescription("Trigger to run async worker for " + topic)
67 .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(intervalInSeconds))
70 scheduler.scheduleJob(jobDetail, asyncWorkerTrigger);
71 } catch (SchedulerException e) {
72 logger.error(EELFLoggerDelegate.errorLogger, "Failed to schedule trigger for async worker jobs: {}", e.getMessage());
73 throw new GenericUncheckedException(e);