Repository: pyload/pyload

Issue:
[bug] Download starts before UpdateManager finishes causing the download to be aborted
This  is a problem because if the download file is big and the hoster does not support resuming, we have to download it from the beginning.
Also , some hosters forces us to wait  before you can download again..

```
21.03.2015 20:00:51 INFO      Starting pyLoad 0.4.9
21.03.2015 20:00:51 INFO      Using home directory: /etc/pyload
21.03.2015 20:01:00 INFO      Activated plugins: Captcha9Kw, ClickAndLoad, ExternalScripts, UnSkipOnFail, UpdateManager, XFil
21.03.2015 20:01:00 INFO      Deactivate plugins: AlldebridCom, AndroidPhoneNotify, AntiVirus, BypassCaptcha, CaptchaBrotherh
21.03.2015 20:01:00 INFO      Downloadtime: True
21.03.2015 20:01:00 INFO      Starting ThriftBackend: 0.0.0.0:7227
21.03.2015 20:01:00 INFO      Free space: 1.63 TiB
21.03.2015 20:01:00 INFO      Activating Accounts...
21.03.2015 20:01:00 INFO      Activating Plugins...
21.03.2015 20:01:00 INFO      XFileSharingPro: Handling any hoster I can!
21.03.2015 20:01:02 INFO      XFileSharingPro: Handling any crypter I can!
21.03.2015 20:01:02 INFO      Starting threaded webserver: 0.0.0.0:8000
21.03.2015 20:01:02 INFO      pyLoad is up and running
21.03.2015 20:01:05 INFO      Download starts: Dany Lorence - Wood (Original Mix).mp3
21.03.2015 20:01:06 INFO      UpdateManager: No new pyLoad version available
21.03.2015 20:01:06 INFO      UpdateManager: New version of plugin: [crypter] DevhostStFolder (v0.04 -> v0.05)
21.03.2015 20:01:06 INFO      UpdateManager: New version of plugin: [hook] DeleteFinished (v1.11 -> v1.12)
21.03.2015 20:01:07 INFO      UpdateManager: New version of plugin: [hook] RestartFailed (v1.57 -> v1.58)
21.03.2015 20:01:07 INFO      Download starts: Dee-Dee ft. Ray Horton & Radu Sirbu - Gimme Your Love (Criswell & Vally V Remi
21.03.2015 20:01:08 INFO      UpdateManager: New version of plugin: [hook] UpdateManager (v0.46 -> v0.47)
21.03.2015 20:01:08 INFO      UpdateManager: New version of plugin: [hoster] CatShareNet (v0.11 -> v0.12)
21.03.2015 20:01:09 INFO      UpdateManager: New version of plugin: [hoster] SimplydebridCom (v0.16 -> v0.17)
21.03.2015 20:01:09 INFO      Download starts: Dee-Dee ft. Ray Horton & Radu Sirbu - Gimme Your Love (DJ Rollin Remix).mp3
21.03.2015 20:01:09 INFO      UpdateManager: New version of plugin: [internal] MultiHoster (v0.38 -> v0.39)
21.03.2015 20:01:10 INFO      UpdateManager: New version of plugin: [internal] SimpleHoster (v1.28 -> v1.29)
21.03.2015 20:01:10 INFO      ClickAndLoad: Proxy listening on 0.0.0.0:9666
21.03.2015 20:01:11 INFO      UpdateManager: *** Plugins have been updated, but need a pyLoad restart to be reloaded ***
21.03.2015 20:01:11 INFO      restarting pyLoad
21.03.2015 20:01:11 INFO      shutting down...
21.03.2015 20:01:12 INFO      Download aborted: Dany Lorence - Wood (Original Mix).mp3
```



Discussion:
Hi,

it seems that this patch does not work.
26.03.2015 15:32:02 INFO    UpdateManager: New version of plugin: [hook] UpdateManager (v0.48 -> v0.50)

![bug](https://cloud.githubusercontent.com/assets/6263592/6895867/f580377e-d6e4-11e4-8270-9a626652e247.png)

@akfedux Do you have "check on startup" set to ON?

Yes. I have.

If that happened on startup, then there is no solution. pyLoad does not have any kind of init system for plugins.

How about something like this?
What do you say?

``` patch
--- UpdateManager.py.bak        2015-03-26 20:59:43.000000000 +0200
+++ UpdateManager.py    2015-03-30 21:59:31.678036599 +0300
@@ -45,10 +45,13 @@


     interval = 0
+    restartrequired = False

     SERVER_URL         = "http://updatemanager.pyload.org"
     MIN_CHECK_INTERVAL = 3 * 60 * 60  #: 3 hours

+    event_list = ["allDownloadsProcessed"]
+

     def coreReady(self):
         if self.checkonstart:
@@ -68,6 +71,13 @@
         else:
             self.checkonstart = False

+        self.restartrequired = False
+
+    def allDownloadsProcessed(self):
+        if self.restartrequired is True:
+            self.logWarning(_("Downloads are done, restarting pyLoad to reload the updated plugins"))
+            self.core.api.restart()
+

     def periodical(self):
         if self.core.debug:
@@ -128,7 +138,12 @@
         self.core.api.pauseServer()

         if self._update() is 2 and self.getConfig('autorestart'):
-            self.core.api.restart()
+            downloads = self.core.api.statusDownloads()
+            if not downloads:
+                self.core.api.restart()
+            else:
+                self.restartrequired = True
+                self.logWarning(_("Downloads are active, will restart once the download is done"))
         else:
             self.core.api.unpauseServer()


```

@vuolter can you comment on the above?

Hi,

I use this patch three days and seems that everything is alright.
Good job, thanks a lot GammaC0de.

@vuolter you can add patch to stable version.

![pyload](https://cloud.githubusercontent.com/assets/6263592/6992225/fee413e0-dac1-11e4-8fd7-73712c980b8f.png)

@vuolter is it safe that plugin files are replaced on disk but no restart have been done until the download is finished?

@GammaC0de UpdateManager replaces .py files, keeping running .pyc, so it's safe. When an addon/hook is updated, was deactivated before.
You can open PRs here: https://github.com/pyload/pyload/pulls ;)


Resolving patch:
diff --git a/module/plugins/hooks/UpdateManager.py b/module/plugins/hooks/UpdateManager.py
--- a/module/plugins/hooks/UpdateManager.py
+++ b/module/plugins/hooks/UpdateManager.py
@@ -29,7 +29,7 @@ def exists(path):
 class UpdateManager(Hook):
     __name__    = "UpdateManager"
     __type__    = "hook"
-    __version__ = "0.50"
+    __version__ = "0.51"
 
     __config__ = [("activated"    , "bool", "Activated"                                , True ),
                   ("checkinterval", "int" , "Check interval in hours"                  , 8    ),
@@ -45,10 +45,13 @@ class UpdateManager(Hook):
 
 
     interval = 0
+    restartrequired = False
 
     SERVER_URL         = "http://updatemanager.pyload.org"
     MIN_CHECK_INTERVAL = 3 * 60 * 60  #: 3 hours
 
+    event_list = ["allDownloadsProcessed"]
+
 
     def coreReady(self):
         if self.checkonstart:
@@ -68,6 +71,14 @@ def setup(self):
         else:
             self.checkonstart = False
 
+        self.restartrequired = False
+
+
+    def allDownloadsProcessed(self):
+        if self.restartrequired is True:
+            self.logWarning(_("Downloads are done, restarting pyLoad to reload the updated plugins"))
+            self.core.api.restart()
+
 
     def periodical(self):
         if self.core.debug:
@@ -128,7 +139,12 @@ def update(self):
         self.core.api.pauseServer()
 
         if self._update() is 2 and self.getConfig('autorestart'):
-            self.core.api.restart()
+            downloads = self.core.api.statusDownloads()
+            if not downloads:
+                self.core.api.restart()
+            else:
+                self.restartrequired = True
+                self.logWarning(_("Downloads are active, will restart once the download is done"))
         else:
             self.core.api.unpauseServer()
 
@@ -258,7 +274,7 @@ def _updatePlugins(self, data):
             if self.core.pluginManager.reloadPlugins(updated):
                 exitcode = 1
             else:
-                self.logWarning(_("Restart pyLoad to reload the updated plugins"))
+                self.logWarning(_("pyLoad restart required to reload the updated plugins"))
                 self.info['plugins'] = True
                 exitcode = 2
 


Test patch: