Skip to content

Commit

Permalink
TS-31571 Wait for flush on termination
Browse files Browse the repository at this point in the history
  • Loading branch information
stahlbauer committed Oct 16, 2024
1 parent 23a8362 commit 98662a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 0 additions & 2 deletions agent/src/main/java/com/teamscale/jacoco/agent/PreMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.teamscale.jacoco.agent;

import com.teamscale.client.HttpUtils;
import com.teamscale.client.TeamscaleServer;
import com.teamscale.jacoco.agent.configuration.AgentOptionReceiveException;
import com.teamscale.jacoco.agent.logging.LogToTeamscaleAppender;
import com.teamscale.jacoco.agent.options.AgentOptionParseException;
Expand All @@ -13,7 +12,6 @@
import com.teamscale.jacoco.agent.options.TeamscalePropertiesUtils;
import com.teamscale.jacoco.agent.testimpact.TestwiseCoverageAgent;
import com.teamscale.jacoco.agent.upload.UploaderException;
import com.teamscale.jacoco.agent.upload.teamscale.TeamscaleConfig;
import com.teamscale.jacoco.agent.util.AgentUtils;
import com.teamscale.jacoco.agent.logging.DebugLogDirectoryPropertyDefiner;
import com.teamscale.jacoco.agent.logging.LogDirectoryPropertyDefiner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import com.teamscale.client.TeamscaleClient;
import com.teamscale.jacoco.agent.options.AgentOptions;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.conqat.lib.commons.collections.IdentityHashSet;
import retrofit2.Call;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

Check warning on line 18 in agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java

View check run for this annotation

cqse.teamscale.io / teamscale-findings

agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java#L18

Star import of `java.util.concurrent.*` should not be used https://cqse.teamscale.io/findings/details/teamscale-jacoco-agent?t=ts%2F31571_send_log_to_teamscale_via_appender%3AHEAD&id=37AC472955602FF3B639815C2FEAECFF

public class LogToTeamscaleAppender extends AppenderBase<ILoggingEvent> {

Check warning on line 20 in agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java

View check run for this annotation

cqse.teamscale.io / teamscale-findings

agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java#L20

Interface comment missing https://cqse.teamscale.io/findings/details/teamscale-jacoco-agent?t=ts%2F31571_send_log_to_teamscale_via_appender%3AHEAD&id=D63084C6B2A4A673F56C9102501F76C1
Expand All @@ -35,6 +37,9 @@ public class LogToTeamscaleAppender extends AppenderBase<ILoggingEvent> {
/** Scheduler for sending logs after the configured time interval */
private final ScheduledExecutorService scheduler;

/** Active log flushing threads */
private final Set<CompletableFuture<Void>> activeLogFlushes = new IdentityHashSet<>();

public LogToTeamscaleAppender() {
this.scheduler = Executors.newScheduledThreadPool(1, r -> {
// Make the thread a daemon so that it does not prevent the JVM from terminating.
Expand Down Expand Up @@ -81,7 +86,7 @@ private void flush() {

/** Send logs in a separate thread */
private void sendLogs(List<ProfilerLogEntry> logs) {
CompletableFuture.runAsync(() -> {
activeLogFlushes.add(CompletableFuture.runAsync(() -> {
try {
if (teamscaleClient == null) {
// There might be no connection configured.
Expand All @@ -96,7 +101,9 @@ private void sendLogs(List<ProfilerLogEntry> logs) {
} catch (Exception e) {
System.err.println("Sending logs to Teamscale failed: " + e.getMessage());

Check warning on line 102 in agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java

View check run for this annotation

cqse.teamscale.io / teamscale-findings

agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java#L102

Method `System.err.println` should not be called https://cqse.teamscale.io/findings/details/teamscale-jacoco-agent?t=ts%2F31571_send_log_to_teamscale_via_appender%3AHEAD&id=53BE4AA54E99ED7DCD55878D93D6D1CC
}

Check failure on line 103 in agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java

View check run for this annotation

cqse.teamscale.io / teamscale-findings

agent/src/main/java/com/teamscale/jacoco/agent/logging/LogToTeamscaleAppender.java#L101-L103

Catch clause catches generic exception `Exception` https://cqse.teamscale.io/findings/details/teamscale-jacoco-agent?t=ts%2F31571_send_log_to_teamscale_via_appender%3AHEAD&id=D703581B7FC61F46D42D1C9EA3B59C48
});
}).whenComplete((result, throwable) -> {
activeLogFlushes.removeIf(CompletableFuture::isDone);
}));
}

@Override
Expand All @@ -116,6 +123,9 @@ public void stop() {
// A final flush after the scheduler has been shut down.
flush();

// Block until all flushes are done
CompletableFuture.allOf(activeLogFlushes.toArray(new CompletableFuture[0])).join();

super.stop();
}

Expand Down

0 comments on commit 98662a4

Please sign in to comment.