- synchronized (reactions) {
- if (!reactions.containsKey(name)) {
- return;
- }
- reactions.remove(name);
- reactions.notifyAll();
- }
- }
-
- //
- // ABSTRACTSERVICE METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void run() {
- while (isRunning()) {
- Optional<Pair<String, Reaction>> nextReaction = getNextReaction();
- if (!nextReaction.isPresent()) {
- continue;
- }
-
- String reactionName = nextReaction.get().getLeft();
- logger.debug(format("Next Reaction: %s.", reactionName));
-
- /* wait until the next reaction has to run. */
- Optional<net.pterodactylus.rhynodge.State> lastState = stateManager.loadLastState(reactionName);
- long lastStateTime = lastState.isPresent() ? lastState.get().time() : 0;
- int lastStateFailCount = lastState.isPresent() ? lastState.get().failCount() : 0;
- long waitTime = (lastStateTime + nextReaction.get().getRight().updateInterval()) - System.currentTimeMillis();
- logger.debug(format("Time to wait for next Reaction: %d millseconds.", waitTime));
- if (waitTime > 0) {
- synchronized (reactions) {
- try {
- logger.info(format("Waiting until %tc.", lastStateTime + nextReaction.get().getRight().updateInterval()));
- reactions.wait(waitTime);
- } catch (InterruptedException ie1) {
- /* we’re looping! */
- }
- }
-
- /* re-start loop to check for new reactions. */
- continue;
- }
-
- /* run reaction. */
- logger.info(format("Running Query for %s...", reactionName));
- Query query = nextReaction.get().getRight().query();
- net.pterodactylus.rhynodge.State state;
- try {
- logger.debug("Querying system...");
- state = query.state();
- if (state == null) {
- state = FailedState.INSTANCE;
- }
- logger.debug("System queried.");
- } catch (Throwable t1) {
- logger.warn("Querying system failed!", t1);
- state = new AbstractState(t1) {
- /* no further state. */
- };
- }
- logger.debug(format("State is %s.", state));
-
- /* convert states. */
- for (Filter filter : nextReaction.get().getRight().filters()) {
- if (state.success()) {
- net.pterodactylus.rhynodge.State newState = filter.filter(state);
- //logger.debug(String.format("Old state is %s, new state is %s.", state, newState));
- state = newState;
- }
- }
- if (!state.success()) {
- state.setFailCount(lastStateFailCount + 1);
- }
- Optional<net.pterodactylus.rhynodge.State> lastSuccessfulState = stateManager.loadLastSuccessfulState(reactionName);
-
- /* merge states. */
- boolean triggerHit = false;
- Trigger trigger = nextReaction.get().getRight().trigger();
- if (lastSuccessfulState.isPresent() && lastSuccessfulState.get().success() && state.success()) {
- net.pterodactylus.rhynodge.State newState = trigger.mergeStates(lastSuccessfulState.get(), state);
-
- /* save new state. */
- stateManager.saveState(reactionName, newState);
-
- triggerHit = trigger.triggers();
- } else {
- /* save first or error state. */
- stateManager.saveState(reactionName, state);
- }
-
- /* run action if trigger was hit. */
- logger.debug(format("Trigger was hit: %s.", triggerHit));
- if (triggerHit) {
- logger.info("Executing Action...");
- nextReaction.get().getRight().action().execute(trigger.output(nextReaction.get().getRight()));
- }
-
- }
- }
-
- private Optional<Pair<String, Reaction>> getNextReaction() {
- while (isRunning()) {
- synchronized (reactions) {
- if (reactions.isEmpty()) {
- logger.debug("Sleeping while no Reactions available.");
- try {
- reactions.wait();
- } catch (InterruptedException ie1) {
- /* ignore, we’re looping anyway. */
- }
- continue;
- }
- }
-
- /* find next reaction. */
- SortedMap<Long, Pair<String, Reaction>> nextReactions = newTreeMap();
- synchronized (reactions) {
- for (Entry<String, Reaction> reactionEntry : reactions.entrySet()) {
- Optional<net.pterodactylus.rhynodge.State> state = stateManager.loadLastState(reactionEntry.getKey());
- long stateTime = state.isPresent() ? state.get().time() : 0;
- nextReactions.put(stateTime + reactionEntry.getValue().updateInterval(), Pair.of(reactionEntry.getKey(), reactionEntry.getValue()));
- }
- return of(nextReactions.get(nextReactions.firstKey()));
- }