From: David ‘Bombe’ Roden Date: Sun, 6 Jan 2013 16:19:08 +0000 (+0100) Subject: Store failed states and fail count, too. X-Git-Tag: 0.1~62 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=fb6252bcb9e3689b3320fb6a0ff2805fd2465e2a;p=rhynodge.git Store failed states and fail count, too. --- diff --git a/src/main/java/net/pterodactylus/reactor/State.java b/src/main/java/net/pterodactylus/reactor/State.java index cc37909..278032e 100644 --- a/src/main/java/net/pterodactylus/reactor/State.java +++ b/src/main/java/net/pterodactylus/reactor/State.java @@ -44,6 +44,25 @@ public interface State { boolean success(); /** + * Returns the number of consecutive failures. This method only returns a + * meaningful number iff {@link #success()} returns {@code false}. If + * {@link #success()} returns {@code false} for the first time after + * returning {@code true} and this method is called after {@link #success()} + * it will return {@code 1}. + * + * @return The number of consecutive failures + */ + int failCount(); + + /** + * Sets the fail count of this state. + * + * @param failCount + * The fail count of this state + */ + void setFailCount(int failCount); + + /** * If {@link #success()} returns {@code false}, this method may return a * {@link Throwable} to give some details for the reason why retrieving the * state was not possible. For example, network-based {@link Query}s might diff --git a/src/main/java/net/pterodactylus/reactor/engine/Engine.java b/src/main/java/net/pterodactylus/reactor/engine/Engine.java index 60bc91d..7c50081 100644 --- a/src/main/java/net/pterodactylus/reactor/engine/Engine.java +++ b/src/main/java/net/pterodactylus/reactor/engine/Engine.java @@ -136,6 +136,7 @@ public class Engine extends AbstractExecutionThreadService { /* wait until the next reaction has to run. */ net.pterodactylus.reactor.State lastState = stateManager.loadState(reactionName); long lastStateTime = (lastState != null) ? lastState.time() : 0; + int lastStateFailCount = (lastState != null) ? lastState.failCount() : 0; long waitTime = (lastStateTime + nextReaction.updateInterval()) - System.currentTimeMillis(); logger.debug(String.format("Time to wait for next Reaction: %d millseconds.", waitTime)); if (waitTime > 0) { @@ -179,14 +180,15 @@ public class Engine extends AbstractExecutionThreadService { state = newState; } } - if (state.success()) { - stateManager.saveState(reactionName, state); + if (!state.success()) { + state.setFailCount(lastStateFailCount + 1); } + stateManager.saveState(reactionName, state); - /* only run trigger if we have collected two states. */ + /* only run trigger if we have collected two successful states. */ Trigger trigger = nextReaction.trigger(); boolean triggerHit = false; - if ((lastState != null) && state.success()) { + if ((lastState != null) && lastState.success() && state.success()) { logger.debug("Checking Trigger for changes..."); triggerHit = trigger.triggers(state, lastState); } diff --git a/src/main/java/net/pterodactylus/reactor/states/AbstractState.java b/src/main/java/net/pterodactylus/reactor/states/AbstractState.java index 674f4af..31a1d18 100644 --- a/src/main/java/net/pterodactylus/reactor/states/AbstractState.java +++ b/src/main/java/net/pterodactylus/reactor/states/AbstractState.java @@ -41,6 +41,9 @@ public abstract class AbstractState implements State { /** The optional exception that occured while retrieving the state. */ private final Throwable exception; + /** The number of consecutive failures. */ + private int failCount; + /** * Creates a new successful state. */ @@ -108,6 +111,22 @@ public abstract class AbstractState implements State { * {@inheritDoc} */ @Override + public int failCount() { + return failCount; + } + + /** + * {@inheritDoc} + */ + @Override + public void setFailCount(int failCount) { + this.failCount = failCount; + } + + /** + * {@inheritDoc} + */ + @Override public Throwable exception() { return exception; }