2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.enrichment.tasks;
23 import org.oransc.enrichment.clients.AsyncRestClient;
24 import org.oransc.enrichment.clients.AsyncRestClientFactory;
25 import org.oransc.enrichment.configuration.ApplicationConfig;
26 import org.oransc.enrichment.controllers.consumer.ConsumerCallbacks;
27 import org.oransc.enrichment.repository.EiJobs;
28 import org.oransc.enrichment.repository.EiProducer;
29 import org.oransc.enrichment.repository.EiProducers;
30 import org.oransc.enrichment.repository.EiTypes;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.scheduling.annotation.EnableScheduling;
35 import org.springframework.scheduling.annotation.Scheduled;
36 import org.springframework.stereotype.Component;
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
42 * Regularly checks the availability of the EI Producers
46 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
47 public class ProducerSupervision {
48 private static final Logger logger = LoggerFactory.getLogger(ProducerSupervision.class);
50 private final EiProducers eiProducers;
51 private final EiJobs eiJobs;
52 private final EiTypes eiTypes;
53 private final AsyncRestClient restClient;
54 private final ConsumerCallbacks consumerCallbacks;
57 public ProducerSupervision(ApplicationConfig applicationConfig, EiProducers eiProducers, EiJobs eiJobs,
58 EiTypes eiTypes, ConsumerCallbacks consumerCallbacks) {
59 AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
60 this.restClient = restClientFactory.createRestClient("");
62 this.eiProducers = eiProducers;
63 this.eiTypes = eiTypes;
64 this.consumerCallbacks = consumerCallbacks;
67 @Scheduled(fixedRate = 1000 * 60 * 5)
68 public void checkAllProducers() {
69 logger.debug("Checking producers starting");
70 createTask().subscribe(null, null, () -> logger.debug("Checking all Producers completed"));
73 public Flux<EiProducer> createTask() {
74 return Flux.fromIterable(eiProducers.getAllProducers()) //
75 .flatMap(this::checkOneProducer);
78 private Mono<EiProducer> checkOneProducer(EiProducer producer) {
79 return restClient.get(producer.getProducerSupervisionCallbackUrl()) //
80 .onErrorResume(throwable -> {
81 handleNonRespondingProducer(throwable, producer);
84 .doOnNext(response -> handleRespondingProducer(response, producer))
85 .flatMap(response -> Mono.just(producer));
88 private void handleNonRespondingProducer(Throwable throwable, EiProducer producer) {
89 logger.warn("Unresponsive producer: {} exception: {}", producer.getId(), throwable.getMessage());
90 producer.setAliveStatus(false);
91 if (producer.isDead()) {
92 this.eiProducers.deregisterProducer(producer, this.eiTypes, this.eiJobs);
93 this.consumerCallbacks.notifyConsumersProducerDeleted(producer);
97 private void handleRespondingProducer(String response, EiProducer producer) {
98 logger.debug("{}", response);
99 producer.setAliveStatus(true);