Skip to content

Commit

Permalink
feat(vertx): Start mining directly after finishing it again (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jul 15, 2023
1 parent 5a5600d commit 072053f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,46 @@

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
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);
Future.join(
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());
Expand All @@ -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<String, Long> timerIDByAnalyzerName = new HashMap<>();

@Override
public void start() throws Exception {
vertx.eventBus().<String>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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 072053f

Please sign in to comment.