2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.job.impl;
23 import com.google.common.collect.ImmutableMap;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.vid.exceptions.GenericUncheckedException;
26 import org.onap.vid.job.Job;
27 import org.onap.vid.job.JobsBrokerService;
28 import org.onap.vid.job.command.JobCommandFactory;
29 import org.onap.vid.properties.Features;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
33 import org.springframework.stereotype.Component;
34 import org.togglz.core.manager.FeatureManager;
36 import javax.annotation.PostConstruct;
38 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
41 public class JobSchedulerInitializer {
43 private JobsBrokerService jobsBrokerService;
44 private SchedulerFactoryBean schedulerFactoryBean;
45 private FeatureManager featureManager;
46 private JobCommandFactory jobCommandFactory;
47 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
50 public JobSchedulerInitializer(
51 JobsBrokerService jobsBrokerService,
52 SchedulerFactoryBean schedulerFactoryBean,
53 FeatureManager featureManager,
54 JobCommandFactory JobCommandFactory
56 this.jobsBrokerService = jobsBrokerService;
57 this.schedulerFactoryBean = schedulerFactoryBean;
58 this.featureManager = featureManager;
59 this.jobCommandFactory = JobCommandFactory;
65 if (!featureManager.isActive(Features.FLAG_ASYNC_JOBS)) {
68 scheduleJobWorker(Job.JobStatus.PENDING, 1);
69 scheduleJobWorker(Job.JobStatus.CREATING, 1);
70 scheduleJobWorker(Job.JobStatus.IN_PROGRESS, 1);
71 scheduleJobWorker(Job.JobStatus.RESOURCE_IN_PROGRESS, 1);
74 private void scheduleJobWorker(Job.JobStatus topic, int intervalInSeconds) {
75 Scheduler scheduler = schedulerFactoryBean.getScheduler();
76 JobDetail jobDetail = JobBuilder.newJob().ofType(JobWorker.class)
77 .withIdentity("AsyncWorkersJob" + topic)
78 .withDescription("Job that run async worker for " + topic)
79 .setJobData(new JobDataMap(ImmutableMap.of(
80 "jobsBrokerService", jobsBrokerService,
81 "jobCommandFactory", jobCommandFactory,
82 "featureManager", featureManager,
86 Trigger asyncWorkerTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
87 .withIdentity("AsyncWorkersTrigger" + topic)
88 .withDescription("Trigger to run async worker for " + topic)
89 .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(intervalInSeconds))
92 scheduler.scheduleJob(jobDetail, asyncWorkerTrigger);
93 } catch (SchedulerException e) {
94 logger.error(EELFLoggerDelegate.errorLogger, "Failed to schedule trigger for async worker jobs: {}", e.getMessage());
95 throw new GenericUncheckedException(e);