114c20105199afd89b37a874a1c55df786857934
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / impl / DeleteOldJobsSchedulerInitializer.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
24 import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
25
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Random;
28 import java.util.concurrent.TimeUnit;
29 import javax.annotation.PostConstruct;
30 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.onap.portalsdk.core.util.SystemProperties;
32 import org.onap.vid.exceptions.GenericUncheckedException;
33 import org.onap.vid.job.JobsBrokerService;
34 import org.quartz.JobBuilder;
35 import org.quartz.JobDataMap;
36 import org.quartz.JobDetail;
37 import org.quartz.SchedulerException;
38 import org.quartz.Trigger;
39 import org.quartz.TriggerBuilder;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class DeleteOldJobsSchedulerInitializer {
46
47     private JobsBrokerService jobsBrokerService;
48     private SchedulerFactoryBean schedulerFactoryBean;
49     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DeleteOldJobsSchedulerInitializer.class);
50
51     @Autowired
52     public DeleteOldJobsSchedulerInitializer(
53             JobsBrokerService jobsBrokerService,
54             SchedulerFactoryBean schedulerFactoryBean
55     ) {
56         this.jobsBrokerService = jobsBrokerService;
57         this.schedulerFactoryBean = schedulerFactoryBean;
58     }
59
60     @PostConstruct
61     public void init() {
62         try {
63             JobDetail jobDetail = createJobDetail();
64             Trigger deleteOldJobsTrigger = createTrigger();
65             schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, deleteOldJobsTrigger);
66         } catch (SchedulerException e) {
67             logger.error(EELFLoggerDelegate.errorLogger, "Failed to schedule trigger for delete old jobs: {}", e.getMessage());
68             throw new GenericUncheckedException(e);
69         }
70     }
71
72     JobDetail createJobDetail() {
73         int days = Integer.parseInt(SystemProperties.getProperty("vid.asyncJob.howLongToKeepOldJobsInDays"));
74         long secondsAgo = TimeUnit.DAYS.toSeconds(days);
75         return JobBuilder.newJob().ofType(DeleteOldJobsWorker.class)
76                 .withIdentity("DeleteOldJobsWorker")
77                 .withDescription("worker that delete old vid jobs from DB")
78                 .setJobData(new JobDataMap(ImmutableMap.of(
79                         "jobsBrokerService", jobsBrokerService,
80                         "secondsAgo", secondsAgo
81                 )))
82                 .build();
83     }
84
85     Trigger createTrigger() {
86         int minutes = new Random(System.nanoTime()).nextInt(59);
87         int hours = 6;
88         logger.info(EELFLoggerDelegate.debugLogger, "trigger for DeleteOldJobs is {}:{} ", hours, minutes);
89
90         return TriggerBuilder.newTrigger()
91                 .withIdentity("DeleteOldJobsTrigger")
92                 .withDescription("Trigger to run delete old vid jobs worker")
93                 .withSchedule(dailyAtHourAndMinute(hours, minutes))
94                 .build();
95     }
96 }