Skip to content

Commit

Permalink
Use thread rather than scheduler to send error emails (must take into…
Browse files Browse the repository at this point in the history
… account that some api-servers are running with the scheduler disabled).
  • Loading branch information
stuartcaunt committed Dec 3, 2024
1 parent 9c05be9 commit 4228021
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.quarkus.mailer.Mail;
import io.quarkus.mailer.Mailer;
import io.quarkus.mailer.MailerName;
import io.quarkus.runtime.Shutdown;
import io.quarkus.runtime.Startup;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import jakarta.inject.Singleton;
Expand All @@ -30,6 +32,9 @@
@Singleton
public class ActiveErrorReporter implements ErrorReporter {

private final int MAX_ERRORS_WORKER_TIME_MS = 5000;
private final int CURRENT_ERROR_WORKER_TIME_MS = 60000;

public final static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
private static final Logger logger = LoggerFactory.getLogger(ActiveErrorReporter.class);

Expand All @@ -41,6 +46,9 @@ public class ActiveErrorReporter implements ErrorReporter {
private final int maxErrorsPerReport;

private final boolean enabled;
private final Thread reportedThread;
private boolean running = true;
private Date lastReportingTime = new Date();

private List<ErrorEvent> events = new ArrayList<>();

Expand Down Expand Up @@ -69,17 +77,56 @@ public ActiveErrorReporter(final @MailerName("logging") Mailer mailer,
} else {
logger.info("Error reporting is disabled (configuration is not valid)");
}

this.reportedThread = new Thread(this::run);
}

@Startup
public void start() {
this.reportedThread.start();
}

@Shutdown
public void stop() {
this.running = false;
try {
this.reportedThread.join();
} catch (InterruptedException ignored) {
}
}

public void run() {
while (running) {
try {
Thread.sleep(MAX_ERRORS_WORKER_TIME_MS);
this.work();

} catch (InterruptedException ignored) {
}
}
}

private void work() {
this.handleMaxErrors();

Date currentTime = new Date();
long elapsedTime = currentTime.getTime() - this.lastReportingTime.getTime();
if (elapsedTime > CURRENT_ERROR_WORKER_TIME_MS) {
this.handleCurrentErrors();
}
}

public synchronized void handleMaxErrors() {
if (this.events.size() >= this.maxErrorsPerReport) {
this.generateReportInVirtualThread(events);
this.lastReportingTime = new Date();
}
}

public synchronized void handleCurrentErrors() {
if (!this.events.isEmpty()) {
this.generateReportInVirtualThread(events);
this.lastReportingTime = new Date();
}
}

Expand Down

This file was deleted.

0 comments on commit 4228021

Please sign in to comment.