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=ac04543ef4dbb18e6ab83782e9cbc70b8f3181c6;hpb=7a62280f42b7430279b8fd1b5818ddf0e7530b2a;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 ac04543..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
@@ -48,9 +48,12 @@ public class ReactionRunner implements Runnable {
State state = runQuery();
state = runStateThroughFilters(state);
if (!state.success()) {
- logger.info(format("Reaction %s failed.", reaction.name()));
+ 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(trigger.output(reaction));
+ 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);
@@ -77,14 +84,14 @@ public class ReactionRunner implements Runnable {
private Output createErrorOutput(Reaction reaction, State state) {
DefaultOutput output = new DefaultOutput(String.format("Error while processing â%s!â", reaction.name()));
- output.addText("text/plain; charset=utf-8", createErrorEmailText(reaction, state));
- output.addText("text/html; charset=utf-8", createErrorEmailText(reaction, state));
+ output.addText("text/plain", createErrorEmailText(reaction, state));
+ output.addText("text/html", createErrorEmailText(reaction, state));
return output;
}
private String createErrorEmailText(Reaction reaction, State state) {
StringBuilder emailText = new StringBuilder();
- emailText.append(String.format("An error occured while processing â.â\n\n", reaction.name()));
+ emailText.append(String.format("An error occured while processing â%s.â\n\n", reaction.name()));
appendExceptionToEmailText(state.exception(), emailText);
return emailText.toString();
}
@@ -119,9 +126,6 @@ public class ReactionRunner implements Runnable {
logger.debug(format("Filtering state through %s...", filter.getClass().getSimpleName()));
try {
currentState = filter.filter(currentState);
- if (currentState.success() && currentState.isEmpty()) {
- errorEmailAction.execute(createEmptyStateOutput(reaction, state));
- }
} catch (Throwable t1) {
logger.warn(format("Error during filter %s for %s.", filter.getClass().getSimpleName(), reaction.name()), t1);
return new FailedState(t1);
@@ -131,10 +135,4 @@ public class ReactionRunner implements Runnable {
return currentState;
}
- private Output createEmptyStateOutput(Reaction reaction, State state) {
- DefaultOutput defaultOutput = new DefaultOutput(String.format("Reached Empty State for â%s!â", reaction.name()));
- defaultOutput.addText("text/plain; charset=utf-8", String.format("The %s for %s was empty.", state.getClass().getSimpleName(), reaction.name()));
- return defaultOutput;
- }
-
}