64d7798e206bd61517abcda68888ffa067737868
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2023 Deutsche Telekom 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 package org.onap.dcaegen2.services.prh.tasks.commit;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.ScheduledFuture;
26 import javax.annotation.PreDestroy;
27 import org.onap.dcaegen2.services.prh.configuration.PrhProperties;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.slf4j.Marker;
31 import org.slf4j.MarkerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.context.event.ApplicationStartedEvent;
34 import org.springframework.context.annotation.Configuration;
35 import org.springframework.context.annotation.Profile;
36 import org.springframework.context.event.EventListener;
37 import org.springframework.scheduling.TaskScheduler;
38 import org.springframework.scheduling.annotation.EnableScheduling;
39
40 /**
41  * @author <a href="mailto:pravin.kokane@t-systems.com">Pravin Kokane</a> on 3/13/23
42  */
43
44 @Profile("autoCommitDisabled")
45 @Configuration
46 @EnableScheduling
47 public class ScheduledTasksRunnerWithCommit {
48     private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledTasksRunnerWithCommit.class);
49     private static final Marker ENTRY = MarkerFactory.getMarker("ENTRY");
50     private static List<ScheduledFuture> scheduledPrhTaskFutureList = new ArrayList<>();
51
52     private final TaskScheduler taskScheduler;
53     private final PrhProperties prhProperties;
54
55     @Autowired
56     private ScheduledTasksWithCommit scheduledTasksWithCommit;
57
58     public ScheduledTasksRunnerWithCommit(TaskScheduler taskScheduler, ScheduledTasksWithCommit scheduledTasksWithCommit,
59                                           PrhProperties prhProperties) {
60         this.taskScheduler = taskScheduler;
61         this.scheduledTasksWithCommit = scheduledTasksWithCommit;
62         this.prhProperties = prhProperties;
63     }
64
65     @EventListener
66     public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) {
67         tryToStartTaskWithCommit();
68     }
69
70     /**
71      * Function which have to stop tasks execution.
72      */
73     @PreDestroy
74     public synchronized void cancelTasks() {
75         scheduledPrhTaskFutureList.forEach(x -> x.cancel(false));
76         scheduledPrhTaskFutureList.clear();
77     }
78
79     /**
80      * Function for starting scheduling PRH workflow.
81      *
82      * @return status of operation execution: true - started, false - not started
83      */
84
85     public synchronized boolean tryToStartTaskWithCommit() {
86         LOGGER.info(ENTRY, "Start scheduling PRH workflow with Commit  Tasks Runner");
87         if (scheduledPrhTaskFutureList.isEmpty()) {
88             Collections.synchronizedList(scheduledPrhTaskFutureList);
89             scheduledPrhTaskFutureList.add(taskScheduler
90                 .scheduleWithFixedDelay(scheduledTasksWithCommit::scheduleKafkaPrhEventTask,
91                     prhProperties.getWorkflowSchedulingInterval()));
92             return true;
93         } else {
94             return false;
95         }
96     }
97
98 }
99