🔊 Log exceptions that occur when running a reaction
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 26 Feb 2024 19:13:43 +0000 (20:13 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 26 Feb 2024 19:13:43 +0000 (20:13 +0100)
src/main/java/net/pterodactylus/rhynodge/engine/Engine.java

index 99eff2f..e132b08 100644 (file)
 
 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<Exception, Unit> logExceptionForReaction(Reaction reaction) {
+               return (Exception e) -> {
+                       logger.warn("Exception during Reaction “%s”!".formatted(reaction.name()), e);
+                       return Unit.INSTANCE;
+               };
+       }
+
+       private Function0<Unit> 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);
+
 }