annotate libervia/server/tasks/manager.py @ 1251:a1606e2a92eb

server: fixed watching a directory which is already watched: Twisted file watched is not adding a callback to a watched path when there is already one, but this can happens often in Libervia, as a whole site is recusively watched in dev mode, and tasks may want to add other watchers. To work around this, the new `_checkCallback` will recursively check that callback has been indeed added to the requested path.
author Goffi <goffi@goffi.org>
date Wed, 29 Apr 2020 14:54:33 +0200
parents a6c7f07f1e4d
children 80a92eb82b7f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1239
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1237
diff changeset
1 #!/usr/bin/env python3
f511f8fbbf8a fixed shebangs
Goffi <goffi@goffi.org>
parents: 1237
diff changeset
2
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: a Salut à Toi frontend
1237
987595a254b0 dates update
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
4 # Copyright (C) 2011-2020 Jérôme Poisson <goffi@goffi.org>
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 import os
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import os.path
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
20 from pathlib import Path
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
21 import importlib.util
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
22 from twisted.internet import defer
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
23 from sat.core.log import getLogger
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.i18n import _
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
26 from sat.tools import utils
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from libervia.server.constants import Const as C
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
28 from . import implicit
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 log = getLogger(__name__)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
33 class TasksManager:
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 """Handle tasks of a Libervia site"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def __init__(self, host, site_resource):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 @param site_resource(LiberviaRootResource): root resource of the site to manage
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 self.host = host
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.resource = site_resource
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
42 self.tasks_dir = self.site_path / C.TASKS_DIR
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
43 self.tasks = {}
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self._build_path = None
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self._current_task = None
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def site_path(self):
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
49 return Path(self.resource.site_path)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 def build_path(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 """path where generated files will be build for this site"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 if self._build_path is None:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self._build_path = self.host.getBuildPath(self.site_name)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 return self._build_path
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def site_name(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 return self.resource.site_name
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
62 def validateData(self, task):
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
63 """Check workflow attributes in task"""
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
65 for var, allowed in (("ON_ERROR", ("continue", "stop")),
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
66 ("LOG_OUTPUT", bool),
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
67 ("WATCH_DIRS", list)):
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
68 value = getattr(task, var)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if isinstance(allowed, type):
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
71 if allowed is list and value is None:
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
72 continue
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 if not isinstance(value, allowed):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 raise ValueError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
75 _("Unexpected value for {var}, {allowed} is expected.")
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
76 .format(var=var, allowed=allowed))
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 else:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if not value in allowed:
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
79 raise ValueError(_("Unexpected value for {var}: {value!r}").format(
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
80 var=var, value=value))
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
82 async def parseTasksDir(self, dir_path):
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
83 log.debug(f"parsing tasks in {dir_path}")
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
84 tasks_paths = sorted(dir_path.glob('task_*.py'))
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
85 for task_path in tasks_paths:
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
86 if not task_path.is_file():
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 continue
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
88 task_name = task_path.stem[5:].lower().strip()
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 if not task_name:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 continue
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if task_name in self.tasks:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 raise exceptions.ConflictError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
93 "A task with the name [{name}] already exists".format(
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 name=task_name))
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
95 log.debug(f"task {task_name} found")
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
96 module_name = f"{self.site_name}.task.{task_name}"
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
97
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
98 spec = importlib.util.spec_from_file_location(module_name, task_path)
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
99 task_module = importlib.util.module_from_spec(spec)
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
100 spec.loader.exec_module(task_module)
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
101 task = task_module.Task(self)
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
102
1154
a1625e68b726 server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents: 1147
diff changeset
103 # we launch prepare, which is a method used to prepare
a1625e68b726 server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents: 1147
diff changeset
104 # data at runtime (e.g. set WATCH_DIRS using config)
a1625e68b726 server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents: 1147
diff changeset
105 try:
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
106 prepare = task.prepare
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
107 except AttributeError:
1154
a1625e68b726 server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents: 1147
diff changeset
108 pass
a1625e68b726 server (tasks): task can now use a "prepare" method to prepare data before running (e.g. WATCH_DIRS)
Goffi <goffi@goffi.org>
parents: 1147
diff changeset
109 else:
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
110 try:
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
111 await utils.asDeferred(prepare)
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
112 except exceptions.CancelError as e:
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
113 log.debug(f"Skipping {task_name} which cancelled itself: {e}")
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
114 continue
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
115
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
116 self.tasks[task_name] = task
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
117 self.validateData(task)
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
118 if self.host.options['dev_mode']:
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
119 dirs = task.WATCH_DIRS or []
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
120 for dir_ in dirs:
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
121 self.host.files_watcher.watchDir(
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
122 dir_, auto_add=True, recursive=True,
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
123 callback=self._autorunTask, task_name=task_name)
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
124
1247
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
125 async def parseTasks(self):
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
126 # implicit tasks are always run
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
127 implicit_path = Path(implicit.__file__).parent
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
128 await self.parseTasksDir(implicit_path)
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
129 # now we check if there are tasks specific to this site
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
130 if not self.tasks_dir.is_dir():
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
131 log.debug(_("{name} has no task to launch.").format(
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
132 name = self.resource.site_name or "default site"))
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
133 return
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
134 else:
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
135 await self.parseTasksDir(self.tasks_dir)
a6c7f07f1e4d tasks: implicit tasks + Brython task:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
136
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
137 def _autorunTask(self, host, filepath, flags, task_name):
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
138 """Called when an event is received from a watched directory"""
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
139 if flags == ['create']:
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
140 return
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
141 return defer.ensureDeferred(self.runTask(task_name))
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
142
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
143 async def runTask(self, task_name):
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
144 """Run a single task
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
145
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
146 @param task_name(unicode): name of the task to run
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
147 """
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
148 task = self.tasks[task_name]
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
149 self._current_task = task_name
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
150 log.info(_('== running task "{task_name}" for {site_name} =='.format(
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
151 task_name=task_name, site_name=self.site_name)))
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
152 os.chdir(self.site_path)
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
153 try:
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
154 await utils.asDeferred(task.start)
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
155 except Exception as e:
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
156 on_error = task.ON_ERROR
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
157 if on_error == 'stop':
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
158 raise e
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
159 elif on_error == 'continue':
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
160 log.warning(_('Task "{task_name}" failed for {site_name}: {reason}')
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
161 .format(task_name=task_name, site_name=self.site_name, reason=e))
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
162 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
163 raise exceptions.InternalError("we should never reach this point")
1155
813d54af8c0c server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents: 1154
diff changeset
164 self._current_task = None
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
166 async def runTasks(self):
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 """Run all the tasks found"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 old_path = os.getcwd()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
169 for task_name, task_value in self.tasks.items():
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
170 await self.runTask(task_name)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 os.chdir(old_path)