Skip to content

Commit

Permalink
feat(vertx): Start mining earlier if current run finishes faster (#838)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Jul 14, 2023
1 parent 74d9afb commit 0722974
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public void start() throws Exception {
void persistResults(StoreResults storeResults) {
Project project = storeResults.project();
CodeAnalyzerResult result = storeResults.result();
addOrUpdateCommitHash(project, result, SERVICE_NAME);
addOrUpdateCommitHash(project, result, storeResults.analyzerName());
if (result instanceof CodeAnalyzerResult.Failure failure) {
logger.atInfo().log("Analyzer %s failed for project %s", SERVICE_NAME, project.name());
logger.atInfo().log("Analyzer %s failed for project %s", storeResults.analyzerName(), project.name());

} else if (result instanceof CodeAnalyzerResult.Success success) {
logger.atInfo().log("Analyzer %s succeeded for project %s", SERVICE_NAME, project.name());
logger.atInfo().log("Analyzer %s succeeded for project %s", storeResults.analyzerName(), project.name());
}
vertx.eventBus().send(MiningStartup.SERVICE_NAME, storeResults.analyzerName());
}

private AnalyzerStatus getAnalyzerStatus(CodeAnalyzerResult spoonResult, String name) {
Expand Down Expand Up @@ -82,5 +83,6 @@ private void addOrUpdateCommitHash(Project project, CodeAnalyzerResult spoonResu
oldProject.addCommitHash(gitHubCommit);
projectRepository.save(oldProject);
}
vertx.eventBus().send(AnalyzerResultsPersistence.SERVICE_NAME, project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
import io.github.martinwitt.laughing_train.mining.requests.MineNextProject;
import io.quarkus.logging.Log;
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;

Check warning on line 23 in github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Injection point with ambiguous dependencies

Unsatisfied dependency: no bean matches the injection point

Expand All @@ -29,27 +34,53 @@ public class MiningStartup {
@Inject
SpoonPeriodicMiner spoonPeriodicMiner;

Check warning on line 35 in github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Injection point with ambiguous dependencies

Unsatisfied dependency: no bean matches the injection point

@Inject
MiningEventConsumer miningEventConsumer;

Check failure on line 38 in github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect usage of bean type that cannot be proxied

Injected normal scoped bean is not proxyable

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(projectSupplier, options),
vertx.deployVerticle(miningEventConsumer, options))
.onFailure(Throwable::printStackTrace)
.onComplete(v -> System.out.println("All verticles deployed"))
.onSuccess(v -> startPeriodicMining());
.onSuccess(v -> Log.info("Starting periodic mining"));
vertx.eventBus().addInboundInterceptor(v -> {
System.out.println("Received message: " + v.toString());
v.next();
});
}

private void startPeriodicMining() {
Log.info("Starting periodic mining");
vertx.setPeriodic(TimeUnit.MINUTES.toMillis(3), TimeUnit.MINUTES.toMillis(25), v -> vertx.eventBus()
.publish("miner", new MineNextProject(QodanaPeriodicMiner.ANALYZER_NAME)));
vertx.setPeriodic(TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(15), v -> vertx.eventBus()
.publish("miner", new MineNextProject(SpoonPeriodicMiner.ANALYZER_NAME)));
@ApplicationScoped
private static class MiningEventConsumer extends AbstractVerticle {

private Map<String, Long> timerIDByAnalyzerName = new HashMap<>();

Check warning on line 60 in github-bot/src/main/java/io/github/martinwitt/laughing_train/mining/MiningStartup.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Field may be 'final'

Field `timerIDByAnalyzerName` may be 'final'

@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));
}
}
}
}

0 comments on commit 0722974

Please sign in to comment.