Mercurial > libervia-web
annotate libervia/server/tasks.py @ 1155:813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
A task can specify directories to watch in "WATCH_DIRS" list of path.
In dev_mode, when a file is created of modified in a watched dir, the task is run again.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 22 Feb 2019 18:50:33 +0100 |
parents | a1625e68b726 |
children | 5baf7ece44a0 |
rev | line source |
---|---|
1146 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Libervia: a Salut à Toi frontend | |
5 # Copyright (C) 2011-2019 Jérôme Poisson <goffi@goffi.org> | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 import os | |
20 import os.path | |
21 from twisted.internet import defer | |
22 from twisted.python.procutils import which | |
23 from sat.core import exceptions | |
24 from sat.core.i18n import _ | |
25 from libervia.server.constants import Const as C | |
26 from collections import OrderedDict | |
27 from sat.core.log import getLogger | |
28 from sat.tools.common import async_process | |
29 | |
30 log = getLogger(__name__) | |
31 | |
32 | |
33 class TasksManager(object): | |
34 """Handle tasks of a Libervia site""" | |
35 FILE_EXTS = {u'py'} | |
36 | |
37 def __init__(self, host, site_resource): | |
38 """ | |
39 @param site_resource(LiberviaRootResource): root resource of the site to manage | |
40 """ | |
41 self.host = host | |
42 self.resource = site_resource | |
43 self.tasks_dir = os.path.join(self.resource.site_path, C.TASKS_DIR) | |
44 self.tasks = OrderedDict() | |
45 self.parseTasks() | |
46 self._build_path = None | |
47 self._current_task = None | |
48 | |
49 @property | |
50 def site_path(self): | |
51 return self.resource.site_path | |
52 | |
53 @property | |
54 def build_path(self): | |
55 """path where generated files will be build for this site""" | |
56 if self._build_path is None: | |
57 self._build_path = self.host.getBuildPath(self.site_name) | |
58 return self._build_path | |
59 | |
60 def getConfig(self, key, default=None, value_type=None): | |
1147
02afab1b15c5
server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents:
1146
diff
changeset
|
61 return self.host.getConfig(self.resource, key=key, default=default, |
02afab1b15c5
server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents:
1146
diff
changeset
|
62 value_type=value_type) |
1146 | 63 |
64 @property | |
65 def site_name(self): | |
66 return self.resource.site_name | |
67 | |
68 @property | |
69 def task_data(self): | |
70 return self.tasks[self._current_task][u'data'] | |
71 | |
72 def validateData(self, data): | |
73 """Check values in data""" | |
74 | |
75 for var, default, allowed in ((u"ON_ERROR", u"stop", (u"continue", u"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
|
76 (u"LOG_OUTPUT", True, bool), |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
77 (u"WATCH_DIRS", [], list)): |
1146 | 78 value = data.setdefault(var, default) |
79 if isinstance(allowed, type): | |
80 if not isinstance(value, allowed): | |
81 raise ValueError( | |
82 _(u"Unexpected value for {var}, {allowed} is expected.") | |
83 .format(var = var, allowed = allowed)) | |
84 else: | |
85 if not value in allowed: | |
86 raise ValueError(_(u"Unexpected value for {var}: {value}").format( | |
87 var = var, value = value)) | |
88 | |
89 for var, default, allowed in [[u"ON_ERROR", u"stop", (u"continue", u"stop")]]: | |
90 value = data.setdefault(var, default) | |
91 if not value in allowed: | |
92 raise ValueError(_(u"Unexpected value for {var}: {value}").format( | |
93 var = var, value = value)) | |
94 | |
95 def parseTasks(self): | |
96 if not os.path.isdir(self.tasks_dir): | |
97 log.debug(_(u"{name} has no task to launch.").format( | |
98 name = self.resource.site_name or u"default site")) | |
99 return | |
100 filenames = os.listdir(self.tasks_dir) | |
101 filenames.sort() | |
102 for filename in filenames: | |
103 filepath = os.path.join(self.tasks_dir, filename) | |
104 if not filename.startswith(u'task_') or not os.path.isfile(filepath): | |
105 continue | |
106 task_name, ext = os.path.splitext(filename) | |
107 task_name = task_name[5:].lower().strip() | |
108 if not task_name: | |
109 continue | |
110 if ext[1:] not in self.FILE_EXTS: | |
111 continue | |
112 if task_name in self.tasks: | |
113 raise exceptions.ConflictError( | |
114 u"A task with the name [{name}] already exists".format( | |
115 name=task_name)) | |
116 task_data = {u"__name__": "{site_name}.task.{name}".format( | |
117 site_name=self.site_name, name=task_name)} | |
118 self.tasks[task_name] = { | |
119 u'path': filepath, | |
120 u'data': task_data, | |
121 } | |
122 execfile(filepath, task_data) | |
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
|
123 # 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
|
124 # 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
|
125 try: |
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
|
126 prepare = task_data['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
|
127 except KeyError: |
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
|
128 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
|
129 else: |
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
|
130 prepare(self) |
1146 | 131 self.validateData(task_data) |
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
|
132 if self.host.options['dev_mode']: |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
133 dirs = task_data.get['WATCH_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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 |
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 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
|
140 """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
|
141 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
|
142 return |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
143 return self.runTask(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
|
144 |
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 @defer.inlineCallbacks |
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 def runTask(self, 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
|
147 """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
|
148 |
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 @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
|
150 """ |
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_value = self.tasks[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
|
152 self._current_task = 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
|
153 log.info(_(u'== running task "{task_name}" for {site_name} =='.format( |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
154 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
|
155 data = task_value[u'data'] |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
156 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
|
157 try: |
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 yield data['start'](self) |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
159 except Exception as 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
|
160 on_error = data[u'ON_ERROR'] |
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 if on_error == u'stop': |
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 raise 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
|
163 elif on_error == u'continue': |
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 log.warning(_(u'Task "{task_name}" failed for {site_name}: {reason}') |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
165 .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
|
166 else: |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
167 raise exceptions.InternalError(u"we should never reach this point") |
813d54af8c0c
server (tasks): tasks can now be automatically ran when something happen in a watched dir:
Goffi <goffi@goffi.org>
parents:
1154
diff
changeset
|
168 self._current_task = None |
1146 | 169 |
170 @defer.inlineCallbacks | |
171 def runTasks(self): | |
172 """Run all the tasks found""" | |
173 old_path = os.getcwd() | |
174 for task_name, task_value in self.tasks.iteritems(): | |
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
|
175 yield self.runTask(task_name) |
1146 | 176 os.chdir(old_path) |
177 | |
178 def findCommand(self, name, *args): | |
179 """Find full path of a shell command | |
180 | |
181 @param name(unicode): name of the command to find | |
182 @param *args(unicode): extra names the command may have | |
183 @return (unicode): full path of the command | |
184 @raise exceptions.NotFound: can't find this command | |
185 """ | |
186 names = (name,) + args | |
187 for n in names: | |
188 try: | |
189 cmd_path = which(n)[0].encode('utf-8') | |
190 except IndexError: | |
191 pass | |
192 return cmd_path | |
193 raise exceptions.NotFound(_( | |
194 u"Can't find {name} command, did you install it?").format(name=name)) | |
195 | |
196 def runCommand(self, command, *args, **kwargs): | |
197 kwargs['verbose'] = self.task_data[u"LOG_OUTPUT"] | |
198 return async_process.CommandProtocol.run(command, *args, **kwargs) |