Added some logging
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / tasks / ProducerSupervision.java
index d8ee970..d73127f 100644 (file)
 
 package org.oransc.enrichment.tasks;
 
-import org.oransc.enrichment.clients.AsyncRestClient;
-import org.oransc.enrichment.clients.AsyncRestClientFactory;
 import org.oransc.enrichment.configuration.ApplicationConfig;
 import org.oransc.enrichment.controllers.consumer.ConsumerCallbacks;
+import org.oransc.enrichment.controllers.producer.ProducerCallbacks;
+import org.oransc.enrichment.repository.EiJob;
 import org.oransc.enrichment.repository.EiJobs;
 import org.oransc.enrichment.repository.EiProducer;
 import org.oransc.enrichment.repository.EiProducers;
-import org.oransc.enrichment.repository.EiTypes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +36,7 @@ import org.springframework.stereotype.Component;
 
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
+import reactor.util.retry.Retry;
 
 /**
  * Regularly checks the availability of the EI Producers
@@ -49,18 +49,15 @@ public class ProducerSupervision {
 
     private final EiProducers eiProducers;
     private final EiJobs eiJobs;
-    private final EiTypes eiTypes;
-    private final AsyncRestClient restClient;
+    private final ProducerCallbacks producerCallbacks;
     private final ConsumerCallbacks consumerCallbacks;
 
     @Autowired
     public ProducerSupervision(ApplicationConfig applicationConfig, EiProducers eiProducers, EiJobs eiJobs,
-        EiTypes eiTypes, ConsumerCallbacks consumerCallbacks) {
-        AsyncRestClientFactory restClientFactory = new AsyncRestClientFactory(applicationConfig.getWebClientConfig());
-        this.restClient = restClientFactory.createRestClientNoHttpProxy("");
-        this.eiJobs = eiJobs;
+        ProducerCallbacks producerCallbacks, ConsumerCallbacks consumerCallbacks) {
         this.eiProducers = eiProducers;
-        this.eiTypes = eiTypes;
+        this.eiJobs = eiJobs;
+        this.producerCallbacks = producerCallbacks;
         this.consumerCallbacks = consumerCallbacks;
     }
 
@@ -76,21 +73,36 @@ public class ProducerSupervision {
     }
 
     private Mono<EiProducer> checkOneProducer(EiProducer producer) {
-        return restClient.get(producer.getProducerSupervisionCallbackUrl()) //
+        return this.producerCallbacks.healthCheck(producer) //
             .onErrorResume(throwable -> {
                 handleNonRespondingProducer(throwable, producer);
                 return Mono.empty();
             })//
             .doOnNext(response -> handleRespondingProducer(response, producer))
-            .flatMap(response -> Mono.just(producer));
+            .flatMap(response -> checkProducerJobs(producer)) //
+            .flatMap(responses -> Mono.just(producer));
+    }
+
+    private Mono<?> checkProducerJobs(EiProducer producer) {
+        final int MAX_CONCURRENCY = 10;
+        return getEiJobs(producer) //
+            .filter(eiJob -> !producer.isJobEnabled(eiJob)) //
+            .flatMap(eiJob -> producerCallbacks.startEiJob(producer, eiJob, Retry.max(1)), MAX_CONCURRENCY) //
+            .collectList() //
+            .flatMapMany(startedJobs -> consumerCallbacks.notifyJobStatus(producer.getEiTypes())) //
+            .collectList();
+    }
+
+    private Flux<EiJob> getEiJobs(EiProducer producer) {
+        return Flux.fromIterable(producer.getEiTypes()) //
+            .flatMap(eiType -> Flux.fromIterable(eiJobs.getJobsForType(eiType)));
     }
 
     private void handleNonRespondingProducer(Throwable throwable, EiProducer producer) {
         logger.warn("Unresponsive producer: {} exception: {}", producer.getId(), throwable.getMessage());
         producer.setAliveStatus(false);
         if (producer.isDead()) {
-            this.eiProducers.deregisterProducer(producer, this.eiTypes);
-            this.consumerCallbacks.notifyConsumersProducerDeleted(producer);
+            this.eiProducers.deregisterProducer(producer);
         }
     }