✨ Only send an email if previous state was not a failure, too
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / engine / ReactionRunner.java
index f4ae72e..45642ce 100644 (file)
@@ -15,8 +15,8 @@ import net.pterodactylus.rhynodge.Filter;
 import net.pterodactylus.rhynodge.Query;
 import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.State;
-import net.pterodactylus.rhynodge.Trigger;
 import net.pterodactylus.rhynodge.actions.EmailAction;
+import net.pterodactylus.rhynodge.Merger;
 import net.pterodactylus.rhynodge.output.DefaultOutput;
 import net.pterodactylus.rhynodge.output.Output;
 import net.pterodactylus.rhynodge.states.FailedState;
@@ -25,7 +25,7 @@ import org.apache.log4j.Logger;
 
 /**
  * Runs a {@link Reaction}, starting with its {@link Query}, running the {@link
- * State} through its {@link Filter}s, and finally checking the {@link Trigger}
+ * State} through its {@link Filter}s, and finally checking the {@link Merger}
  * for whether an {@link Action} needs to be executed.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
@@ -49,8 +49,11 @@ public class ReactionRunner implements Runnable {
                state = runStateThroughFilters(state);
                if (!state.success()) {
                        logger.info(format("Reaction %s failed in %s.", reaction.name(), state));
+                       Optional<State> lastState = reactionState.loadLastState();
                        saveStateWithIncreasedFailCount(state);
-                       errorEmailAction.execute(createErrorOutput(reaction, state));
+                       if (thisFailureIsTheFirstFailure(lastState)) {
+                               errorEmailAction.execute(createErrorOutput(reaction, state));
+                       }
                        return;
                }
                Optional<State> lastSuccessfulState = reactionState.loadLastSuccessfulState();
@@ -59,16 +62,20 @@ public class ReactionRunner implements Runnable {
                        reactionState.saveState(state);
                        return;
                }
-               Trigger trigger = reaction.trigger();
-               State newState = trigger.mergeStates(lastSuccessfulState.get(), state);
+               Merger merger = reaction.merger();
+               State newState = merger.mergeStates(lastSuccessfulState.get(), state);
                reactionState.saveState(newState);
-               if (trigger.triggers()) {
+               if (newState.triggered()) {
                        logger.info(format("Trigger was hit for %s, executing action...", reaction.name()));
-                       reaction.action().execute(trigger.output(reaction));
+                       reaction.action().execute(newState.output(reaction));
                }
                logger.info(format("Reaction %s finished.", reaction.name()));
        }
 
+       private static boolean thisFailureIsTheFirstFailure(Optional<State> lastState) {
+               return lastState.map(State::success).orElse(true);
+       }
+
        private void saveStateWithIncreasedFailCount(State state) {
                Optional<State> lastState = reactionState.loadLastState();
                state.setFailCount(lastState.map(State::failCount).orElse(0) + 1);