diff libervia/server/server.py @ 1245:079e8eb6e327

server (tasks): refactoring: - moved `TasksManager` to `server.tasks.manager` - tasks modules now must have a `Task` class which will be instanciated by TasksManager - `server.tasks.task` has a basis for `Task` class - `Task.prepare` can now be asynchronous - `importlib` is now used to import tasks, instead of exec - tasks are now parsed/run after pages are imported - `server.BackendReady` is now a coroutine - type hinting for Task attributes
author Goffi <goffi@goffi.org>
date Sat, 25 Apr 2020 16:08:55 +0200
parents 8f39d98416c5
children aaf28d45ae67
line wrap: on
line diff
--- a/libervia/server/server.py	Mon Apr 20 17:33:41 2020 +0200
+++ b/libervia/server/server.py	Sat Apr 25 16:08:55 2020 +0200
@@ -54,7 +54,7 @@
 from libervia.server import websockets
 from libervia.server.pages import LiberviaPage
 from libervia.server.utils import quote, ProgressHandler
-from libervia.server.tasks import TasksManager
+from libervia.server.tasks.manager import TasksManager
 from functools import partial
 
 try:
@@ -734,8 +734,7 @@
         if default_dict:
             conf[''] = default_dict
 
-    @defer.inlineCallbacks
-    def backendReady(self, __):
+    async def backendReady(self):
         log.info(f"Libervia v{self.full_version}")
         if self.options['dev_mode']:
             log.info(_("Developer mode activated"))
@@ -763,9 +762,10 @@
                 default_site_path, auto_add=True, recursive=True,
                 callback=LiberviaPage.onFileChange, site_root=self.sat_root,
                 site_path=default_site_path)
+        LiberviaPage.importPages(self, self.sat_root)
         tasks_manager = TasksManager(self, self.sat_root)
-        yield tasks_manager.runTasks()
-        LiberviaPage.importPages(self, self.sat_root)
+        await tasks_manager.parseTasks()
+        await tasks_manager.runTasks()
         # FIXME: handle _setMenu in a more generic way, taking care of external sites
         self.sat_root._setMenu(self.options["menu_json"])
         self.vhost_root.default = default_root
@@ -805,8 +805,6 @@
                         site_path, auto_add=True, recursive=True,
                         callback=LiberviaPage.onFileChange, site_root=res,
                         site_path=site_path)
-                tasks_manager = TasksManager(self, res)
-                yield tasks_manager.runTasks()
                 res.putChild(
                     C.BUILD_DIR.encode('utf-8'),
                     ProtectedFile(
@@ -820,6 +818,9 @@
                 #        we may want to disable access to the page by direct URL
                 #        (e.g. /blog disabled except if called by external site)
                 LiberviaPage.importPages(self, res, root_path=default_site_path)
+                tasks_manager = TasksManager(self, res)
+                await tasks_manager.parseTasks()
+                await tasks_manager.runTasks()
                 res._setMenu(self.options["menu_json"])
 
             self.vhost_root.addHost(host_name.encode('utf-8'), res)
@@ -895,7 +896,7 @@
             lambda: self.initialised.callback(None),
             lambda failure: self.initialised.errback(Exception(failure)),
         )
-        self.initialised.addCallback(self.backendReady)
+        self.initialised.addCallback(lambda __: defer.ensureDeferred(self.backendReady()))
         self.initialised.addErrback(self.initEb)
 
     def _bridgeEb(self, failure_):