Send email if a state comes back as empty
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 May 2015 17:55:26 +0000 (19:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 May 2015 17:55:26 +0000 (19:55 +0200)
12 files changed:
src/main/java/net/pterodactylus/rhynodge/State.java
src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java
src/main/java/net/pterodactylus/rhynodge/states/AbstractState.java
src/main/java/net/pterodactylus/rhynodge/states/ComicState.java
src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java
src/main/java/net/pterodactylus/rhynodge/states/FailedState.java
src/main/java/net/pterodactylus/rhynodge/states/FileState.java
src/main/java/net/pterodactylus/rhynodge/states/HtmlState.java
src/main/java/net/pterodactylus/rhynodge/states/HttpState.java
src/main/java/net/pterodactylus/rhynodge/states/OutputState.java
src/main/java/net/pterodactylus/rhynodge/states/StringState.java
src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java

index fe7f8ea..d549d69 100644 (file)
@@ -43,6 +43,8 @@ public interface State {
         */
        boolean success();
 
+       boolean isEmpty();
+
        /**
         * Returns the number of consecutive failures. This method only returns a
         * meaningful number iff {@link #success()} returns {@code false}. If
index c9af294..dabc57f 100644 (file)
@@ -77,11 +77,11 @@ 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", createEmailText(reaction, state));
+               output.addText("text/plain; charset=utf-8", createErrorEmailText(reaction, state));
                return output;
        }
 
-       private String createEmailText(Reaction reaction, State state) {
+       private String createErrorEmailText(Reaction reaction, State state) {
                StringBuilder emailText = new StringBuilder();
                emailText.append(String.format("An error occured while processing “.”\n\n", reaction.name()));
                appendExceptionToEmailText(state.exception(), emailText);
@@ -118,6 +118,9 @@ 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);
@@ -127,4 +130,10 @@ 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;
+       }
+
 }
index 17cd518..afb7dc2 100644 (file)
@@ -37,6 +37,7 @@ public abstract class AbstractState implements State {
 
        /** Whether the state was successfully retrieved. */
        private final boolean success;
+       private final boolean empty;
 
        /** The optional exception that occured while retrieving the state. */
        private final Throwable exception;
@@ -60,7 +61,11 @@ public abstract class AbstractState implements State {
         *            otherwise
         */
        protected AbstractState(boolean success) {
-               this(success, null);
+               this(success, true, null);
+       }
+
+       protected AbstractState(boolean success, boolean empty) {
+               this(success, empty, null);
        }
 
        /**
@@ -70,7 +75,7 @@ public abstract class AbstractState implements State {
         *            The exception that occured while retrieving the state
         */
        protected AbstractState(Throwable exception) {
-               this(false, exception);
+               this(false, true, exception);
        }
 
        /**
@@ -82,9 +87,10 @@ public abstract class AbstractState implements State {
         * @param exception
         *            The exception that occured while retrieving the state
         */
-       protected AbstractState(boolean success, Throwable exception) {
+       protected AbstractState(boolean success, boolean empty, Throwable exception) {
                this.time = System.currentTimeMillis();
                this.success = success;
+               this.empty = empty;
                this.exception = exception;
        }
 
@@ -108,6 +114,11 @@ public abstract class AbstractState implements State {
                return success;
        }
 
+       @Override
+       public boolean isEmpty() {
+               return empty;
+       }
+
        /**
         * {@inheritDoc}
         */
index 1f600a3..7e80686 100644 (file)
@@ -36,6 +36,11 @@ public class ComicState extends AbstractState implements Iterable<Comic> {
        @JsonProperty
        private final List<Comic> comics = Lists.newArrayList();
 
+       @Override
+       public boolean isEmpty() {
+               return comics.isEmpty();
+       }
+
        public List<Comic> comics() {
                return comics;
        }
index a0c2bf4..78ca478 100644 (file)
@@ -65,6 +65,11 @@ public class EpisodeState extends AbstractState implements Iterable<Episode> {
        // ACCESSORS
        //
 
+       @Override
+       public boolean isEmpty() {
+               return episodes.isEmpty();
+       }
+
        /**
         * Returns all episodes contained in this state.
         *
index 89aa8f2..a9dbba5 100644 (file)
@@ -46,6 +46,11 @@ public class FailedState extends AbstractState {
                super(exception);
        }
 
+       @Override
+       public boolean isEmpty() {
+               return true;
+       }
+
        //
        // STATIC METHODS
        //
index 3c2e999..0c7b4cb 100644 (file)
@@ -77,6 +77,11 @@ public class FileState extends AbstractState {
        // ACCESSORS
        //
 
+       @Override
+       public boolean isEmpty() {
+               return !exists;
+       }
+
        /**
         * Returns whether the file exists.
         *
index 64ac8a5..d2731f7 100644 (file)
@@ -51,6 +51,11 @@ public class HtmlState extends AbstractState {
        // ACCESSORS
        //
 
+       @Override
+       public boolean isEmpty() {
+               return false;
+       }
+
        /**
         * Returns the URI of the parsed document.
         *
index cdadd13..ea733f7 100644 (file)
@@ -106,6 +106,11 @@ public class HttpState extends AbstractState {
                return copyOf(rawResult, rawResult.length);
        }
 
+       @Override
+       public boolean isEmpty() {
+               return rawResult.length == 0;
+       }
+
        /**
         * Returns the decoded content of the reply. This method uses the charset
         * information from the {@link #contentType()}, if present, or UTF-8 if no
index 68a951d..89d1fcb 100644 (file)
@@ -26,6 +26,11 @@ public class OutputState extends AbstractState {
                this.htmlOutput = htmlOutput;
        }
 
+       @Override
+       public boolean isEmpty() {
+               return !plainTextOutput.isPresent() && !htmlOutput.isPresent();
+       }
+
        public Optional<String> plainTextOutput() {
                return plainTextOutput;
        }
index 6253c31..12c21a7 100644 (file)
@@ -47,4 +47,9 @@ public class StringState extends AbstractState {
                return value;
        }
 
+       @Override
+       public boolean isEmpty() {
+               return value.isEmpty();
+       }
+
 }
index c0ba68a..48ac228 100644 (file)
@@ -65,6 +65,11 @@ public class TorrentState extends AbstractState implements Iterable<TorrentFile>
        // ACCESSORS
        //
 
+       @Override
+       public boolean isEmpty() {
+               return files.isEmpty();
+       }
+
        /**
         * Returns all torrent files of this state.
         *