blob: 7a983c8e8d14aa55f07fb02307eb1279eec085f1 [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
22
23package org.onap.osam.job.impl;
24
25import com.google.common.collect.ImmutableMap;
26import org.onap.osam.exceptions.GenericUncheckedException;
27import org.onap.osam.job.Job;
28import org.onap.osam.job.JobsBrokerService;
29import org.onap.osam.job.command.JobCommandFactory;
30import org.onap.osam.properties.Features;
31import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
32import org.quartz.*;
33import org.springframework.beans.factory.annotation.Autowired;
34import org.springframework.scheduling.quartz.SchedulerFactoryBean;
35import org.springframework.stereotype.Component;
36import org.togglz.core.manager.FeatureManager;
37
38import javax.annotation.PostConstruct;
39
40import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
41
42@Component
43public class JobSchedulerInitializer {
44
45 private JobsBrokerService jobsBrokerService;
46 private SchedulerFactoryBean schedulerFactoryBean;
47 private FeatureManager featureManager;
48 private JobCommandFactory jobCommandFactory;
49 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
50
51 @Autowired
52 public JobSchedulerInitializer(
53 JobsBrokerService jobsBrokerService,
54 SchedulerFactoryBean schedulerFactoryBean,
55 FeatureManager featureManager,
56 JobCommandFactory JobCommandFactory
57 ) {
58 this.jobsBrokerService = jobsBrokerService;
59 this.schedulerFactoryBean = schedulerFactoryBean;
60 this.featureManager = featureManager;
61 this.jobCommandFactory = JobCommandFactory;
62
63 }
64
65 @PostConstruct
66 public void init() {
67 if (!featureManager.isActive(Features.FLAG_ASYNC_JOBS)) {
68 return;
69 }
70 scheduleJobWorker(Job.JobStatus.PENDING, 1);
71 scheduleJobWorker(Job.JobStatus.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}