From: David ‘Bombe’ Roden Date: Mon, 4 May 2015 17:55:26 +0000 (+0200) Subject: Send email if a state comes back as empty X-Git-Tag: v2~195 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ffb3c77f6ab233f13d0093ac7f7663a00654fb97;p=rhynodge.git Send email if a state comes back as empty --- diff --git a/src/main/java/net/pterodactylus/rhynodge/State.java b/src/main/java/net/pterodactylus/rhynodge/State.java index fe7f8ea..d549d69 100644 --- a/src/main/java/net/pterodactylus/rhynodge/State.java +++ b/src/main/java/net/pterodactylus/rhynodge/State.java @@ -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 diff --git a/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java b/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java index c9af294..dabc57f 100644 --- a/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java +++ b/src/main/java/net/pterodactylus/rhynodge/engine/ReactionRunner.java @@ -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; + } + } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/AbstractState.java b/src/main/java/net/pterodactylus/rhynodge/states/AbstractState.java index 17cd518..afb7dc2 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/AbstractState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/AbstractState.java @@ -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} */ diff --git a/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java b/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java index 1f600a3..7e80686 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/ComicState.java @@ -36,6 +36,11 @@ public class ComicState extends AbstractState implements Iterable { @JsonProperty private final List comics = Lists.newArrayList(); + @Override + public boolean isEmpty() { + return comics.isEmpty(); + } + public List comics() { return comics; } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java b/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java index a0c2bf4..78ca478 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/EpisodeState.java @@ -65,6 +65,11 @@ public class EpisodeState extends AbstractState implements Iterable { // ACCESSORS // + @Override + public boolean isEmpty() { + return episodes.isEmpty(); + } + /** * Returns all episodes contained in this state. * diff --git a/src/main/java/net/pterodactylus/rhynodge/states/FailedState.java b/src/main/java/net/pterodactylus/rhynodge/states/FailedState.java index 89aa8f2..a9dbba5 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/FailedState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/FailedState.java @@ -46,6 +46,11 @@ public class FailedState extends AbstractState { super(exception); } + @Override + public boolean isEmpty() { + return true; + } + // // STATIC METHODS // diff --git a/src/main/java/net/pterodactylus/rhynodge/states/FileState.java b/src/main/java/net/pterodactylus/rhynodge/states/FileState.java index 3c2e999..0c7b4cb 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/FileState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/FileState.java @@ -77,6 +77,11 @@ public class FileState extends AbstractState { // ACCESSORS // + @Override + public boolean isEmpty() { + return !exists; + } + /** * Returns whether the file exists. * diff --git a/src/main/java/net/pterodactylus/rhynodge/states/HtmlState.java b/src/main/java/net/pterodactylus/rhynodge/states/HtmlState.java index 64ac8a5..d2731f7 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/HtmlState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/HtmlState.java @@ -51,6 +51,11 @@ public class HtmlState extends AbstractState { // ACCESSORS // + @Override + public boolean isEmpty() { + return false; + } + /** * Returns the URI of the parsed document. * diff --git a/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java b/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java index cdadd13..ea733f7 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/HttpState.java @@ -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 diff --git a/src/main/java/net/pterodactylus/rhynodge/states/OutputState.java b/src/main/java/net/pterodactylus/rhynodge/states/OutputState.java index 68a951d..89d1fcb 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/OutputState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/OutputState.java @@ -26,6 +26,11 @@ public class OutputState extends AbstractState { this.htmlOutput = htmlOutput; } + @Override + public boolean isEmpty() { + return !plainTextOutput.isPresent() && !htmlOutput.isPresent(); + } + public Optional plainTextOutput() { return plainTextOutput; } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/StringState.java b/src/main/java/net/pterodactylus/rhynodge/states/StringState.java index 6253c31..12c21a7 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/StringState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/StringState.java @@ -47,4 +47,9 @@ public class StringState extends AbstractState { return value; } + @Override + public boolean isEmpty() { + return value.isEmpty(); + } + } diff --git a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java index c0ba68a..48ac228 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java @@ -65,6 +65,11 @@ public class TorrentState extends AbstractState implements Iterable // ACCESSORS // + @Override + public boolean isEmpty() { + return files.isEmpty(); + } + /** * Returns all torrent files of this state. *