From: David ‘Bombe’ Roden Date: Mon, 26 Feb 2024 19:13:43 +0000 (+0100) Subject: 🔊 Log exceptions that occur when running a reaction X-Git-Tag: v2~23 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1087708b1f7416de4084649a68ac9d495d56ac4a;p=rhynodge.git 🔊 Log exceptions that occur when running a reaction --- diff --git a/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java b/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java index 99eff2f..e132b08 100644 --- a/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java +++ b/src/main/java/net/pterodactylus/rhynodge/engine/Engine.java @@ -17,23 +17,23 @@ package net.pterodactylus.rhynodge.engine; -import static java.lang.System.currentTimeMillis; -import static java.util.concurrent.TimeUnit.MILLISECONDS; - -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Future; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ScheduledThreadPoolExecutor; - import jakarta.inject.Inject; import jakarta.inject.Singleton; - +import kotlin.Unit; +import kotlin.jvm.functions.Function0; +import kotlin.jvm.functions.Function1; import net.pterodactylus.rhynodge.Reaction; import net.pterodactylus.rhynodge.actions.EmailAction; import net.pterodactylus.rhynodge.states.StateManager; +import org.apache.log4j.Logger; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.*; + +import static java.lang.System.currentTimeMillis; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static net.pterodactylus.util.exception.ExceptionsKt.suppressException; /** * Rhynodge main engine. @@ -73,10 +73,24 @@ public class Engine { long lastExecutionTime = lastState.map(net.pterodactylus.rhynodge.State::time).orElse(0L); long nextExecutionTime = lastExecutionTime + reaction.updateInterval(); ReactionRunner reactionRunner = new ReactionRunner(reaction, reactionState, errorEmailAction); - ScheduledFuture future = executorService.scheduleWithFixedDelay(reactionRunner, nextExecutionTime - currentTimeMillis(), reaction.updateInterval(), MILLISECONDS); + ScheduledFuture future = executorService.scheduleWithFixedDelay(suppressException(wrapRunnable(reactionRunner), logExceptionForReaction(reaction))::invoke, nextExecutionTime - currentTimeMillis(), reaction.updateInterval(), MILLISECONDS); scheduledFutures.put(name, future); } + private Function1 logExceptionForReaction(Reaction reaction) { + return (Exception e) -> { + logger.warn("Exception during Reaction “%s”!".formatted(reaction.name()), e); + return Unit.INSTANCE; + }; + } + + private Function0 wrapRunnable(Runnable runnable) { + return () -> { + runnable.run(); + return Unit.INSTANCE; + }; + } + /** * Removes the reaction with the given name. * @@ -90,4 +104,6 @@ public class Engine { scheduledFutures.remove(name).cancel(true); } + private static final Logger logger = Logger.getLogger(Engine.class); + }