Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vertx): Start mining earlier if current run finishes faster #838

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

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

@Inject
MiningEventConsumer miningEventConsumer;

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<>();

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