diff options
author | Vuolter <vuolter@gmail.com> | 2013-05-12 12:45:18 +0200 |
---|---|---|
committer | Vuolter <vuolter@gmail.com> | 2013-05-12 14:54:46 +0200 |
commit | f581c00bd7848cf9f6c07373f61e105099fa1515 (patch) | |
tree | 8b8221fa8cc5d396e72cd0943bcc986526c5e888 | |
parent | Version 1.1: fixes (diff) | |
download | pyload-f581c00bd7848cf9f6c07373f61e105099fa1515.tar.xz |
Version 1.2: all issues fixed + improved code
-rw-r--r-- | module/plugins/hooks/RestartFailed.py | 82 |
1 files changed, 46 insertions, 36 deletions
diff --git a/module/plugins/hooks/RestartFailed.py b/module/plugins/hooks/RestartFailed.py index 3a69ae8c2..b4987bc06 100644 --- a/module/plugins/hooks/RestartFailed.py +++ b/module/plugins/hooks/RestartFailed.py @@ -23,12 +23,12 @@ from time import time class RestartFailed(Hook): __name__ = "RestartFailed" - __version__ = "1.1" + __version__ = "1.2" __description__ = "Automatically restart failed/aborted downloads" __config__ = [ ("activated", "bool", "Activated", "True"), ("dlFail", "bool", "Restart when download fail", "True"), - ("dlFail_n", "int", "Only when failed/aborted downloads are at least", "5"), + ("dlFail_n", "int", "Only when failed downloads are at least", "5"), ("dlFail_i", "int", "Only when elapsed time since last restart is (min)", "10"), ("dlPrcs", "bool", "Restart after all downloads are processed", "True"), ("recnt", "bool", "Restart after reconnecting", "True"), @@ -37,21 +37,21 @@ class RestartFailed(Hook): __author_name__ = ("Walter Purcaro") __author_mail__ = ("vuolter@gmail.com") - event_map = {"pluginConfigChanged": "configEvents"} - def resetCounters(self): + # self.logDebug("self.resetCounters") self.info["dlfailed"] = 0 if self.info["timerflag"]: self.setTimer(False, None) def restart(self): - now = time.time() + now = time() self.resetCounters() self.core.api.restartFailed() - self.logDebug("called self.core.api.restartFailed()") + self.logDebug("self.restart: self.core.api.restartFailed") self.info["lastrstime"] = now def setTimer(self, timerflag, interval): + # self.logDebug("self.setTimer") self.info["timerflag"] = timerflag if interval and interval != self.interval: self.interval = interval @@ -61,7 +61,8 @@ class RestartFailed(Hook): self.removeEvent("periodical", self.restart) def checkFailed_i(self): - now = time.time() + #self.logDebug("self.checkFailed_i") + now = time() lastrstime = self.info["lastrstime"] interval = self.getConfig("dlFail_i") * 60 timerflag = self.info["timerflag"] @@ -71,62 +72,71 @@ class RestartFailed(Hook): self.setTimer(True, interval) def checkFailed_n(self): + # self.logDebug("self.checkFailed_n") curr = self.info["dlfailed"] max = self.getConfig("dlFail_n") if curr >= max: self.checkFailed_i() - else: - self.info["dlfailed"] = curr + 1 def checkFailed(self, pyfile): - status = pyfile.getStatusName() - if status == "failed" or status == "aborted": - self.checkFailed_n() + # self.logDebug("self.checkFailed") + self.info["dlfailed"] += 1 + self.checkFailed_n() def addEvent(self, event, handler): - self.manager.addEvent(event, handler) + if event in self.manager.events: + if handler not in self.manager.events[event]: + self.manager.events[event].append(handler) + # self.logDebug("self.addEvent: " + event + " event: added handler") + else: + # self.logDebug("self.addEvent: " + event + " event: NOT added handler") + return False + else: + self.manager.events[event] = [handler] + # self.logDebug("self.addEvent: " + event + " event: added event and handler") return True def removeEvent(self, event, handler): if event in self.manager.events and handler in self.manager.events[event]: self.manager.events[event].remove(handler) + # self.logDebug("self.removeEvent: " + event + " event: removed handler") return True else: + # self.logDebug("self.removeEvent: " + event + " event: NOT removed handler") return False - def on_allDownloadsProcessed(self): - self.restart() - - def on_downloadStart(self, pyfile): - self.removeEvent("downloadStarts", self.on_downloadStart) - self.addEvent("allDownloadsProcessed", self.on_allDownloadsProcessed) - - def on_allDownloadsFinished(self): - self.removeEvent("allDownloadsProcessed", self.on_allDownloadsProcessed) - self.addEvent("downloadStarts", self.on_downloadStart) - - def on_afterReconnecting(self, ip): + def onAfterReconnecting(self, ip): + # self.logDebug("self.onAfterReconnecting") self.restart() def configEvents(self, plugin, name, value): + # self.logDebug("self.configEvents") if self.getConfig("dlFail"): - self.addEvent("downloadFinished", self.checkFailed) + self.addEvent("downloadFailed", self.checkFailed) else: - self.removeEvent("downloadFinished", self.checkFailed) + self.removeEvent("downloadFailed", self.checkFailed) self.resetCounters() if self.getConfig("dlPrcs"): - self.addEvent("allDownloadsProcessed", self.on_allDownloadsProcessed) - self.addEvent("allDownloadsFinished", self.on_allDownloadsFinished) + self.addEvent("allDownloadsProcessed", self.restart) else: - if not self.removeEvent("allDownloadsProcessed", self.on_allDownloadsProcessed): - self.removeEvent("downloadStarts", self.on_downloadStart) - self.removeEvent("allDownloadsFinished", self.on_allDownloadsFinished) + self.removeEvent("allDownloadsProcessed", self.restart) if self.getConfig("recnt"): - self.addEvent("afterReconnecting", self.on_afterReconnecting) + self.addEvent("afterReconnecting", self.onAfterReconnecting) else: - self.removeEvent("afterReconnecting", self.on_afterReconnecting) - - def setup(self): + self.removeEvent("afterReconnecting", self.onAfterReconnecting) + + def unload(self): + # self.logDebug("self.unload") + self.removeEvent("pluginConfigChanged", self.configEvents) + self.removeEvent("periodical", self.restart) + self.removeEvent("downloadFailed", self.checkFailed) + self.removeEvent("allDownloadsProcessed", self.restart) + self.removeEvent("afterReconnecting", self.onAfterReconnecting) + + def coreReady(self): + # self.logDebug("self.coreReady") self.info = {"dlfailed": 0, "lastrstime": 0, "timerflag": False} if self.getConfig("rsLoad"): self.restart() + self.addEvent("pluginConfigChanged", self.configEvents) + self.configEvents(None, None, None) |