478ae309d8a59551019a79d8f3afd1e286f91dbf
[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 import javax.annotation.PostConstruct;
25 import org.onap.dcaegen2.collectors.datafile.tasks.ScheduledTasks;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Configuration;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.scheduling.TaskScheduler;
31 import org.springframework.scheduling.annotation.EnableScheduling;
32 import io.swagger.annotations.ApiOperation;
33 import reactor.core.publisher.Mono;
34
35 /**
36  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 6/13/18
37  */
38 @Configuration
39 @EnableScheduling
40 public class SchedulerConfig extends DatafileAppConfig {
41
42     private static final int SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS = 15;
43     private static final int SCHEDULING_REQUEST_FOR_CONFIGURATION_DELAY = 5;
44     private static volatile List<ScheduledFuture<?>> scheduledFutureList = new ArrayList<>();
45
46     private final TaskScheduler taskScheduler;
47     private final ScheduledTasks scheduledTask;
48     private final CloudConfiguration cloudConfiguration;
49
50     @Autowired
51     public SchedulerConfig(TaskScheduler taskScheduler, ScheduledTasks scheduledTask,
52         CloudConfiguration cloudConfiguration) {
53         this.taskScheduler = taskScheduler;
54         this.scheduledTask = scheduledTask;
55         this.cloudConfiguration = cloudConfiguration;
56     }
57
58     /**
59      * Function which have to stop tasks execution.
60      *
61      * @return response entity about status of cancellation operation
62      */
63     @ApiOperation(value = "Get response on stopping task execution")
64     public synchronized Mono<ResponseEntity<String>> getResponseFromCancellationOfTasks() {
65         scheduledFutureList.forEach(x -> x.cancel(false));
66         scheduledFutureList.clear();
67         return Mono.defer(() -> Mono
68             .just(new ResponseEntity<>("Datafile Service has already been stopped!", HttpStatus.CREATED)));
69     }
70
71     /**
72      * Function for starting scheduling Datafile workflow.
73      *
74      * @return status of operation execution: true - started, false - not started
75      */
76     @PostConstruct
77     @ApiOperation(value = "Start task if possible")
78     public synchronized boolean tryToStartTask() {
79         if (scheduledFutureList.isEmpty()) {
80             scheduledFutureList.add(taskScheduler
81                 .scheduleAtFixedRate(cloudConfiguration::runTask, Instant.now(),
82                     Duration.ofMinutes(SCHEDULING_REQUEST_FOR_CONFIGURATION_DELAY)));
83             scheduledFutureList.add(taskScheduler.scheduleWithFixedDelay(scheduledTask::scheduleMainDatafileEventTask,
84                 Duration.ofSeconds(SCHEDULING_DELAY_FOR_DATAFILE_COLLECTOR_TASKS)));
85             return true;
86         } else {
87             return false;
88         }
89
90     }
91 }