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.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.vid.exceptions.GenericUncheckedException;
27 import org.onap.vid.job.Job;
28 import org.onap.vid.job.JobsBrokerService;
29 import org.onap.vid.job.command.JobCommandFactory;
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;
37 import java.util.List;
39 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
42 public class JobSchedulerInitializer {
44 private JobsBrokerService jobsBrokerService;
45 private SchedulerFactoryBean schedulerFactoryBean;
46 private FeatureManager featureManager;
47 private JobCommandFactory jobCommandFactory;
48 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
51 public JobSchedulerInitializer(
52 JobsBrokerService jobsBrokerService,
53 SchedulerFactoryBean schedulerFactoryBean,
54 FeatureManager featureManager,
55 JobCommandFactory jobCommandFactory
57 this.jobsBrokerService = jobsBrokerService;
58 this.schedulerFactoryBean = schedulerFactoryBean;
59 this.featureManager = featureManager;
60 this.jobCommandFactory = jobCommandFactory;
64 public static final List<Job.JobStatus> WORKERS_TOPICS = ImmutableList.of(
65 Job.JobStatus.PENDING,
66 Job.JobStatus.CREATING,
67 Job.JobStatus.IN_PROGRESS,
68 Job.JobStatus.RESOURCE_IN_PROGRESS,
69 Job.JobStatus.PENDING_RESOURCE
74 WORKERS_TOPICS.forEach(topic->scheduleJobWorker(topic, 1));
77 private void scheduleJobWorker(Job.JobStatus topic, int intervalInSeconds) {
78 Scheduler scheduler = schedulerFactoryBean.getScheduler();
79 JobDetail jobDetail = JobBuilder.newJob().ofType(JobWorker.class)
80 .withIdentity("AsyncWorkersJob" + topic)
81 .withDescription("Job that run async worker for " + topic)
82 .setJobData(new JobDataMap(ImmutableMap.of(
83 "jobsBrokerService", jobsBrokerService,
84 "jobCommandFactory", jobCommandFactory,
85 "featureManager", featureManager,
89 Trigger asyncWorkerTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
90 .withIdentity("AsyncWorkersTrigger" + topic)
91 .withDescription("Trigger to run async worker for " + topic)
92 .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(intervalInSeconds))
95 scheduler.scheduleJob(jobDetail, asyncWorkerTrigger);
96 } catch (SchedulerException e) {
97 logger.error(EELFLoggerDelegate.errorLogger, "Failed to schedule trigger for async worker jobs: {}", e.getMessage());
98 throw new GenericUncheckedException(e);