Ignore chains and states default directories.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / engine / Engine.java
index e82267c..6f4376b 100644 (file)
@@ -47,12 +47,22 @@ public class Engine extends AbstractExecutionThreadService {
        private static final Logger logger = Logger.getLogger(Engine.class);
 
        /** The state manager. */
-       private final StateManager stateManager = new StateManager("states");
+       private final StateManager stateManager;
 
        /** All defined reactions. */
        /* synchronize on itself. */
        private final Map<String, Reaction> reactions = new HashMap<String, Reaction>();
 
+       /**
+        * Creates a new engine.
+        *
+        * @param stateManager
+        *            The state manager
+        */
+       public Engine(StateManager stateManager) {
+               this.stateManager = stateManager;
+       }
+
        //
        // ACCESSORS
        //
@@ -125,7 +135,7 @@ public class Engine extends AbstractExecutionThreadService {
                                reactionName = nextReactions.get(nextReactions.firstKey()).getLeft();
                                nextReaction = nextReactions.get(nextReactions.firstKey()).getRight();
                        }
-                       logger.debug(String.format("Next Reaction: %s.", nextReaction));
+                       logger.debug(String.format("Next Reaction: %s.", reactionName));
 
                        /* wait until the next reaction has to run. */
                        net.pterodactylus.reactor.State lastState = stateManager.loadLastState(reactionName);
@@ -136,7 +146,7 @@ public class Engine extends AbstractExecutionThreadService {
                        if (waitTime > 0) {
                                synchronized (reactions) {
                                        try {
-                                               logger.debug(String.format("Waiting for %d milliseconds.", waitTime));
+                                               logger.info(String.format("Waiting until %tc.", lastStateTime + nextReaction.updateInterval()));
                                                reactions.wait(waitTime);
                                        } catch (InterruptedException ie1) {
                                                /* we’re looping! */
@@ -177,21 +187,22 @@ public class Engine extends AbstractExecutionThreadService {
                        if (!state.success()) {
                                state.setFailCount(lastStateFailCount + 1);
                        }
+                       net.pterodactylus.reactor.State lastSuccessfulState = stateManager.loadLastSuccessfulState(reactionName);
                        stateManager.saveState(reactionName, state);
 
                        /* only run trigger if we have collected two successful states. */
                        Trigger trigger = nextReaction.trigger();
                        boolean triggerHit = false;
-                       if ((lastState != null) && lastState.success() && state.success()) {
+                       if ((lastSuccessfulState != null) && lastSuccessfulState.success() && state.success()) {
                                logger.debug("Checking Trigger for changes...");
-                               triggerHit = trigger.triggers(state, lastState);
+                               triggerHit = trigger.triggers(state, lastSuccessfulState);
                        }
 
                        /* run action if trigger was hit. */
                        logger.debug(String.format("Trigger was hit: %s.", triggerHit));
                        if (triggerHit) {
                                logger.info("Executing Action...");
-                               nextReaction.action().execute(trigger.output());
+                               nextReaction.action().execute(trigger.output(nextReaction));
                        }
 
                }