From 1087708b1f7416de4084649a68ac9d495d56ac4a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 26 Feb 2024 20:13:43 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=94=8A=20Log=20exceptions=20that=20occur?= =?utf8?q?=20when=20running=20a=20reaction?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/rhynodge/engine/Engine.java | 42 +++++++++++++++------- 1 file changed, 29 insertions(+), 13 deletions(-) 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); + } -- 2.7.4