2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.impl;
24 import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute;
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;
45 public class DeleteOldJobsSchedulerInitializer {
47 private JobsBrokerService jobsBrokerService;
48 private SchedulerFactoryBean schedulerFactoryBean;
49 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DeleteOldJobsSchedulerInitializer.class);
52 public DeleteOldJobsSchedulerInitializer(
53 JobsBrokerService jobsBrokerService,
54 SchedulerFactoryBean schedulerFactoryBean
56 this.jobsBrokerService = jobsBrokerService;
57 this.schedulerFactoryBean = schedulerFactoryBean;
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);
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
85 Trigger createTrigger() {
86 int minutes = new Random(System.nanoTime()).nextInt(59);
88 logger.info("trigger for DeleteOldJobs is {}:{} ", hours, minutes);
90 return TriggerBuilder.newTrigger()
91 .withIdentity("DeleteOldJobsTrigger")
92 .withDescription("Trigger to run delete old vid jobs worker")
93 .withSchedule(dailyAtHourAndMinute(hours, minutes))