bc21f96c8217122570af388a97aa06c5d60399ad
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2018 NOKIA Intellectual Property, 2018-2019 Nordix Foundation. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6  * in compliance with the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
19 import java.time.Duration;
20 import java.time.Instant;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.concurrent.ScheduledFuture;
24
25 import javax.annotation.PostConstruct;
26
27 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.Configuration;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.scheduling.TaskScheduler;
33 import org.springframework.scheduling.annotation.EnableScheduling;
34
35 import io.swagger.annotations.ApiOperation;
36 import reactor.core.publisher.Mono;
37
38 /**
39  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
40  */
41 @Configuration
42 @EnableScheduling
43 public class SchedulerConfig {
44
45     private static final Duration SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS = Duration.ofSeconds(15);
46     private static final Duration SCHEDULING_REQUEST_FOR_CONFIGURATION_DELAY = Duration.ofMinutes(5);
47     private static final Duration SCHEDULING_DELAY_FOR_DATAFILE_PURGE_CACHE = Duration.ofHours(1);
48     private static volatile List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
49
50     private final TaskScheduler taskScheduler;
51     private final ScheduledTasks scheduledTask;
52     private final CloudConfiguration cloudConfiguration;
53
54     @Autowired
55     public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTask,
56         CloudConfiguration cloudConfiguration) {
57         this.taskScheduler = taskScheduler;
58         this.scheduledTask = scheduledTask;
59         this.cloudConfiguration = cloudConfiguration;
60     }
61
62     /**
63      * Function which have to stop tasks execution.
64      *
65      * @return response entity about status of cancellation operation
66      */
67     @ApiOperation(value = "Get response on stopping task execution")
68     public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
69         scheduledFutureList.forEach(x -> x.cancel(false));
70         scheduledFutureList.clear();
71         return Mono.defer(() -> Mono
72             .just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED)));
73     }
74
75     /**
76      * Function for starting scheduling Datafile workflow.
77      *
78      * @return status of operation execution: true - started, false - not started
79      */
80     @PostConstruct
81     @ApiOperation(value = "Start task if possible")
82     public synchronized boolean tryToStartTask() {
83         if (scheduledFutureList.isEmpty()) {
84             scheduledFutureList.add(taskScheduler.scheduleAtFixedRate(cloudConfiguration::runTask, Instant.now(),
85                     SCHEDULING_REQUEST_FOR_CONFIGURATION_DELAY));
86             scheduledFutureList.add(taskScheduler.scheduleWithFixedDelay(scheduledTask::scheduleMainDatafileEventTask,
87                     SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS));
88             scheduledFutureList.add(taskScheduler.scheduleWithFixedDelay(() -> scheduledTask.purgeCachedInformation(Instant.now()),
89                    SCHEDULING_DELAY_FOR_DATAFILE_PURGE_CACHE));
90
91             return true;
92         } else {
93             return false;
94         }
95
96     }
97 }