From 072053f08eba16b651038a10de8cfbcf0275413e Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sat, 15 Jul 2023 17:08:30 +0200 Subject: [PATCH] feat(vertx): Start mining directly after finishing it again (#841) --- .../laughing_train/mining/MiningStartup.java | 73 +++++-------------- .../mining/QodanaPeriodicMiner.java | 22 +++--- .../mining/SpoonPeriodicMiner.java | 22 +++--- .../AnalyzerResultPersistenceService.java | 3 + 4 files changed, 47 insertions(+), 73 deletions(-) diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java index dc529badc..459a1ee51 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java @@ -2,15 +2,12 @@ import io.github.martinwitt.laughing_train.mining.requests.MineNextProject; import io.quarkus.runtime.StartupEvent; -import io.vertx.core.AbstractVerticle; import io.vertx.core.DeploymentOptions; import io.vertx.core.Future; import io.vertx.core.Vertx; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.TimeUnit; @ApplicationScoped @@ -18,23 +15,25 @@ public class MiningStartup { public static final String SERVICE_NAME = "miningStartup"; - @Inject - Vertx vertx; - - @Inject - AnalyzerResultsPersistence persistence; - - @Inject - ProjectSupplier projectSupplier; - - @Inject - QodanaPeriodicMiner qodanaPeriodicMiner; + final Vertx vertx; + final AnalyzerResultsPersistence persistence; + final ProjectSupplier projectSupplier; + final QodanaPeriodicMiner qodanaPeriodicMiner; + final SpoonPeriodicMiner spoonPeriodicMiner; @Inject - SpoonPeriodicMiner spoonPeriodicMiner; - - @Inject - MiningEventConsumer miningEventConsumer; + public MiningStartup( + Vertx vertx, + AnalyzerResultsPersistence persistence, + ProjectSupplier projectSupplier, + QodanaPeriodicMiner qodanaPeriodicMiner, + SpoonPeriodicMiner spoonPeriodicMiner) { + this.vertx = vertx; + this.persistence = persistence; + this.projectSupplier = projectSupplier; + this.qodanaPeriodicMiner = qodanaPeriodicMiner; + this.spoonPeriodicMiner = spoonPeriodicMiner; + } void startup(@Observes StartupEvent event) { DeploymentOptions options = new DeploymentOptions().setWorker(true); @@ -42,9 +41,7 @@ void startup(@Observes StartupEvent event) { vertx.deployVerticle(qodanaPeriodicMiner, options), vertx.deployVerticle(spoonPeriodicMiner, options), vertx.deployVerticle(persistence, options), - vertx.deployVerticle(projectSupplier, options) - // vertx.deployVerticle(miningEventConsumer, options) - ) + vertx.deployVerticle(projectSupplier, options)) .onFailure(Throwable::printStackTrace) .onComplete(v -> System.out.println("All verticles deployed")) .onSuccess(v -> startMining()); @@ -55,39 +52,9 @@ void startup(@Observes StartupEvent event) { } private void startMining() { - vertx.setPeriodic(TimeUnit.MINUTES.toMillis(3), TimeUnit.MINUTES.toMillis(25), v -> vertx.eventBus() + vertx.setTimer(TimeUnit.MINUTES.toMillis(3), v -> vertx.eventBus() .publish("miner", new MineNextProject(QodanaPeriodicMiner.ANALYZER_NAME))); - vertx.setPeriodic(TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(10), v -> vertx.eventBus() + vertx.setTimer(TimeUnit.MINUTES.toMillis(2), v -> vertx.eventBus() .publish("miner", new MineNextProject(SpoonPeriodicMiner.ANALYZER_NAME))); } - - @ApplicationScoped - private static class MiningEventConsumer extends AbstractVerticle { - - private Map timerIDByAnalyzerName = new HashMap<>(); - - @Override - public void start() throws Exception { - vertx.eventBus().consumer(SERVICE_NAME, v -> startMiningAgain(v.body())); - timerIDByAnalyzerName.put( - QodanaPeriodicMiner.ANALYZER_NAME, - vertx.setPeriodic(TimeUnit.MINUTES.toMillis(3), TimeUnit.MINUTES.toMillis(25), v -> vertx.eventBus() - .publish("miner", new MineNextProject(QodanaPeriodicMiner.ANALYZER_NAME)))); - timerIDByAnalyzerName.put( - SpoonPeriodicMiner.ANALYZER_NAME, - vertx.setPeriodic(TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(15), v -> vertx.eventBus() - .publish("miner", new MineNextProject(SpoonPeriodicMiner.ANALYZER_NAME)))); - } - - void startMiningAgain(String analyzerName) { - if (vertx.cancelTimer(timerIDByAnalyzerName.get(analyzerName))) { - timerIDByAnalyzerName.put( - analyzerName, - vertx.setPeriodic( - TimeUnit.MINUTES.toMillis(3), TimeUnit.MINUTES.toMillis(25), v -> vertx.eventBus() - .publish("miner", new MineNextProject(analyzerName)))); - vertx.eventBus().publish("miner", new MineNextProject(analyzerName)); - } - } - } } diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/QodanaPeriodicMiner.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/QodanaPeriodicMiner.java index 1916ec05f..3ad2ecfca 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/QodanaPeriodicMiner.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/QodanaPeriodicMiner.java @@ -67,19 +67,21 @@ void mineWithQodana(MineNextProject event) { new GetProject(ANALYZER_NAME), new DeliveryOptions().setSendTimeout(TimeUnit.MINUTES.toMillis(5))); request.onSuccess(v -> { - if (v.body() instanceof ProjectResult.Success success) { - var qodanaResult = analyzeProject(success); - if (qodanaResult instanceof QodanaResult.Success qodanaSuccess) { - storeSuccess(success, qodanaSuccess); - } else { - if (qodanaResult instanceof QodanaResult.Failure error) { - storeFailure(success, error); + if (v.body() instanceof ProjectResult.Success success) { + var qodanaResult = analyzeProject(success); + if (qodanaResult instanceof QodanaResult.Success qodanaSuccess) { + storeSuccess(success, qodanaSuccess); + } else { + if (qodanaResult instanceof QodanaResult.Failure error) { + storeFailure(success, error); + } + } } - } - } - }); + }) + .onComplete(v -> vertx.eventBus().publish("miner", new MineNextProject(ANALYZER_NAME))); request.onFailure(v -> { logger.atWarning().withCause(v).log("Failed to get project"); + vertx.eventBus().publish("miner", new MineNextProject(ANALYZER_NAME)); }); } diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/SpoonPeriodicMiner.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/SpoonPeriodicMiner.java index faed32297..a9ba02fee 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/SpoonPeriodicMiner.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/SpoonPeriodicMiner.java @@ -66,19 +66,21 @@ void mineWithSpoon(MineNextProject event) { new GetProject(ANALYZER_NAME), new DeliveryOptions().setSendTimeout(TimeUnit.MINUTES.toMillis(5))); request.onSuccess(v -> { - if (v.body() instanceof ProjectResult.Success success) { - var spoonResult = analyzeProjectWithSpoon(success); - if (spoonResult instanceof CodeAnalyzerResult.Success spoonSuccess) { - storeSuccess(success, spoonSuccess); - } else { - if (spoonResult instanceof CodeAnalyzerResult.Failure error) { - storeFailure(success, error); + if (v.body() instanceof ProjectResult.Success success) { + var spoonResult = analyzeProjectWithSpoon(success); + if (spoonResult instanceof CodeAnalyzerResult.Success spoonSuccess) { + storeSuccess(success, spoonSuccess); + } else { + if (spoonResult instanceof CodeAnalyzerResult.Failure error) { + storeFailure(success, error); + } + } } - } - } - }); + }) + .onComplete(v -> vertx.eventBus().publish("miner", new MineNextProject(ANALYZER_NAME))); request.onFailure(v -> { logger.atWarning().withCause(v).log("Failed to get project"); + vertx.eventBus().publish("miner", new MineNextProject(ANALYZER_NAME)); }); } diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/services/AnalyzerResultPersistenceService.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/services/AnalyzerResultPersistenceService.java index 984d0ec20..7aa4393c8 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/services/AnalyzerResultPersistenceService.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/services/AnalyzerResultPersistenceService.java @@ -38,6 +38,9 @@ void persistResults(QodanaResult result) { void persistResults(CodeAnalyzerResult result) { if (result instanceof CodeAnalyzerResult.Success success) { + logger.atInfo().log( + "Persisting %s results for project %s", + success.results().size(), success.project().name()); Project project = success.project(); Multi.createFrom() .iterable(success.results())