blob: 526b82345625e06dfb5d0f51736fac506bcb941d [file] [log] [blame]
Aharoni, Pavel (pa0916)8c70f072018-11-18 00:07:12 +02001/*-
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 */
20package org.onap.osam.job.impl;
21
22import com.google.common.collect.ImmutableMap;
23import lombok.extern.slf4j.Slf4j;
24import org.onap.osam.common.exception.GenericUncheckedException;
25import org.onap.osam.job.dao.job.JobStatus;
26import org.onap.osam.job.IJobsDataAccessService;
27import org.onap.osam.job.command.JobCommandFactory;
28import org.quartz.JobBuilder;
29import org.quartz.JobDataMap;
30import org.quartz.JobDetail;
31import org.quartz.Scheduler;
32import org.quartz.SchedulerException;
33import org.quartz.SimpleTrigger;
34import org.quartz.TriggerBuilder;
35import org.springframework.beans.factory.annotation.Autowired;
36import org.springframework.scheduling.quartz.SchedulerFactoryBean;
37import org.springframework.stereotype.Component;
38
39import javax.annotation.PostConstruct;
40
41import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
42
43@Slf4j
44@Component
45public class JobSchedulerInitializer {
46
47 private IJobsDataAccessService jobsDataAccessService;
48 private SchedulerFactoryBean schedulerFactoryBean;
49 private JobCommandFactory jobCommandFactory;
50
51 @Autowired
52 public JobSchedulerInitializer(
53 IJobsDataAccessService jobsDataAccessService,
54 SchedulerFactoryBean schedulerFactoryBean,
55 JobCommandFactory JobCommandFactory
56 ) {
57 this.jobsDataAccessService = jobsDataAccessService;
58 this.schedulerFactoryBean = schedulerFactoryBean;
59 this.jobCommandFactory = JobCommandFactory;
60
61 }
62
63 @PostConstruct
64 public void init() {
65 scheduleJobWorker(JobStatus.PENDING, 1);
66 scheduleJobWorker(JobStatus.CREATING, 1);
67 scheduleJobWorker(JobStatus.IN_PROGRESS, 1);
68 scheduleJobWorker(JobStatus.RESOURCE_IN_PROGRESS, 1);
69 }
70
71 private void scheduleJobWorker(JobStatus topic, int intervalInSeconds) {
72 final Scheduler scheduler = schedulerFactoryBean.getScheduler();
73 JobDetail jobDetail = JobBuilder.newJob().ofType(JobWorker.class)
74 .withIdentity("AsyncWorkersJob" + topic)
75 .withDescription("Job that run async worker for " + topic)
76 .setJobData(new JobDataMap(ImmutableMap.of(
77 "jobsDataAccessService", jobsDataAccessService,
78 "jobCommandFactory", jobCommandFactory,
79 "topic", topic
80 )))
81 .build();
82 SimpleTrigger asyncWorkerTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
83 .withIdentity("AsyncWorkersTrigger" + topic)
84 .withDescription("Trigger to run async worker for " + topic)
85 .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(intervalInSeconds))
86 .build();
87 try {
88 scheduler.scheduleJob(jobDetail, asyncWorkerTrigger);
89 } catch (SchedulerException e) {
90 log.error("Failed to schedule trigger for async worker jobs: {}", e.getMessage());
91 throw new GenericUncheckedException(e);
92 }
93 }
94}