Don't use EELFLoggerDelegate.errorLogger in Async jobs
[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 static org.quartz.SimpleScheduleBuilder.simpleSchedule;
24
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import java.util.List;
28 import javax.annotation.PostConstruct;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.onap.vid.job.Job;
32 import org.onap.vid.job.JobsBrokerService;
33 import org.onap.vid.job.command.JobCommandFactory;
34 import org.quartz.JobBuilder;
35 import org.quartz.JobDataMap;
36 import org.quartz.JobDetail;
37 import org.quartz.Scheduler;
38 import org.quartz.SchedulerException;
39 import org.quartz.Trigger;
40 import org.quartz.TriggerBuilder;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
43 import org.springframework.stereotype.Component;
44 import org.togglz.core.manager.FeatureManager;
45
46 @Component
47 public class JobSchedulerInitializer {
48
49     private JobsBrokerService jobsBrokerService;
50     private SchedulerFactoryBean schedulerFactoryBean;
51     private FeatureManager featureManager;
52     private JobCommandFactory jobCommandFactory;
53     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobSchedulerInitializer.class);
54     public static final List<Job.JobStatus> WORKERS_TOPICS = ImmutableList.of(
55             Job.JobStatus.PENDING,
56             Job.JobStatus.CREATING,
57             Job.JobStatus.IN_PROGRESS,
58             Job.JobStatus.RESOURCE_IN_PROGRESS,
59             Job.JobStatus.PENDING_RESOURCE
60     );
61
62
63     @Autowired
64     public JobSchedulerInitializer(
65             JobsBrokerService jobsBrokerService,
66             SchedulerFactoryBean schedulerFactoryBean,
67             FeatureManager featureManager,
68             JobCommandFactory jobCommandFactory
69     ) {
70         this.jobsBrokerService = jobsBrokerService;
71         this.schedulerFactoryBean = schedulerFactoryBean;
72         this.featureManager = featureManager;
73         this.jobCommandFactory = jobCommandFactory;
74
75     }
76
77     @PostConstruct
78     public void init() {
79         WORKERS_TOPICS.forEach(topic->scheduleJobWorker(topic, 1));
80     }
81
82     private void scheduleJobWorker(Job.JobStatus topic, int intervalInSeconds) {
83         Scheduler scheduler = schedulerFactoryBean.getScheduler();
84         JobDetail jobDetail = JobBuilder.newJob().ofType(JobWorker.class)
85                 .withIdentity("AsyncWorkersJob" + topic)
86                 .withDescription("Job that run async worker for " + topic)
87                 .setJobData(new JobDataMap(ImmutableMap.of(
88                         "jobsBrokerService", jobsBrokerService,
89                         "jobCommandFactory", jobCommandFactory,
90                         "featureManager", featureManager,
91                         "topic", topic
92                 )))
93                 .build();
94         Trigger asyncWorkerTrigger = TriggerBuilder.newTrigger().forJob(jobDetail)
95                 .withIdentity("AsyncWorkersTrigger" + topic)
96                 .withDescription("Trigger to run async worker for " + topic)
97                 .withSchedule(simpleSchedule().repeatForever().withIntervalInSeconds(intervalInSeconds))
98                 .build();
99         try {
100             scheduler.scheduleJob(jobDetail, asyncWorkerTrigger);
101         } catch (SchedulerException e) {
102             logger.error("Failed to schedule trigger for async worker jobs: {}", e.getMessage());
103             throw new GenericUncheckedException(e);
104         }
105     }
106 }