234c10614ca53eb52b631758d50ffda65e5f503e
[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.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;
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
38 import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
39
40 @Component
41 public class JobSchedulerInitializer {
42
43     private JobsBrokerService jobsBrokerService;
44     private SchedulerFactoryBean schedulerFactoryBean;
45     private FeatureManager featureManager;
46     private JobCommandFactory jobCommandFactory;
47     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
48
49     @Autowired
50     public JobSchedulerInitializer(
51             JobsBrokerService jobsBrokerService,
52             SchedulerFactoryBean schedulerFactoryBean,
53             FeatureManager featureManager,
54             JobCommandFactory JobCommandFactory
55     ) {
56         this.jobsBrokerService = jobsBrokerService;
57         this.schedulerFactoryBean = schedulerFactoryBean;
58         this.featureManager = featureManager;
59         this.jobCommandFactory = JobCommandFactory;
60
61     }
62
63     @PostConstruct
64     public void init() {
65         if (!featureManager.isActive(Features.FLAG_ASYNC_JOBS)) {
66             return;
67         }
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);
72     }
73
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,
83                         "topic", topic
84                 )))
85                 .build();
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))
90                 .build();
91         try {
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);
96         }
97     }
98 }