b5c2dd74b2d39eb4a0f2792d2f96e1342ed4edc7
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / impl / JobSchedulerInitializer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.vid.job.impl;
22
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;
30 import org.quartz.*;
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;
35
36 import javax.annotation.PostConstruct;
37 import java.util.List;
38
39 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
40
41 @Component
42 public class JobSchedulerInitializer {
43
44     private JobsBrokerService jobsBrokerService;
45     private SchedulerFactoryBean schedulerFactoryBean;
46     private FeatureManager featureManager;
47     private JobCommandFactory jobCommandFactory;
48     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
49     public static final List<Job.JobStatus> WORKERS_TOPICS = ImmutableList.of(
50             Job.JobStatus.PENDING,
51             Job.JobStatus.CREATING,
52             Job.JobStatus.IN_PROGRESS,
53             Job.JobStatus.RESOURCE_IN_PROGRESS,
54             Job.JobStatus.PENDING_RESOURCE
55     );
56
57
58     @Autowired
59     public JobSchedulerInitializer(
60             JobsBrokerService jobsBrokerService,
61             SchedulerFactoryBean schedulerFactoryBean,
62             FeatureManager featureManager,
63             JobCommandFactory jobCommandFactory
64     ) {
65         this.jobsBrokerService = jobsBrokerService;
66         this.schedulerFactoryBean = schedulerFactoryBean;
67         this.featureManager = featureManager;
68         this.jobCommandFactory = jobCommandFactory;
69
70     }
71
72     @PostConstruct
73     public void init() {
74         WORKERS_TOPICS.forEach(topic->scheduleJobWorker(topic, 1));
75     }
76
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,
86                         "topic", topic
87                 )))
88                 .build();
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))
93                 .build();
94         try {
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);
99         }
100     }
101 }