X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Frhynodge%2Fengine%2FReactionRunner.java;h=45642cebc6c696b545540cb271bbded6552feb5b;hb=8aeef90590095a455e2323d70ba8ccae032f4276;hp=a4006d81a2b5260ab39cf5dc8df15e665fdbeac6;hpb=5d962b76adef88663cfa4acc093836c71fe9dd82;p=rhynodge.git
diff --git a/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java b/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java
index a4006d8..45642ce 100644
--- a/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java
+++ b/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java
@@ -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 David âBombeâ Roden
@@ -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 lastState = reactionState.loadLastState();
saveStateWithIncreasedFailCount(state);
- errorEmailAction.execute(createErrorOutput(reaction, state));
+ if (thisFailureIsTheFirstFailure(lastState)) {
+ errorEmailAction.execute(createErrorOutput(reaction, state));
+ }
return;
}
Optional 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(newState.output(reaction));
}
logger.info(format("Reaction %s finished.", reaction.name()));
}
+ private static boolean thisFailureIsTheFirstFailure(Optional lastState) {
+ return lastState.map(State::success).orElse(true);
+ }
+
private void saveStateWithIncreasedFailCount(State state) {
Optional lastState = reactionState.loadLastState();
state.setFailCount(lastState.map(State::failCount).orElse(0) + 1);