comparison 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
comparison
equal deleted inserted replaced
1154:a1625e68b726 1155:813d54af8c0c
71 71
72 def validateData(self, data): 72 def validateData(self, data):
73 """Check values in data""" 73 """Check values in data"""
74 74
75 for var, default, allowed in ((u"ON_ERROR", u"stop", (u"continue", u"stop")), 75 for var, default, allowed in ((u"ON_ERROR", u"stop", (u"continue", u"stop")),
76 (u"LOG_OUTPUT", True, bool)): 76 (u"LOG_OUTPUT", True, bool),
77 (u"WATCH_DIRS", [], list)):
77 value = data.setdefault(var, default) 78 value = data.setdefault(var, default)
78 if isinstance(allowed, type): 79 if isinstance(allowed, type):
79 if not isinstance(value, allowed): 80 if not isinstance(value, allowed):
80 raise ValueError( 81 raise ValueError(
81 _(u"Unexpected value for {var}, {allowed} is expected.") 82 _(u"Unexpected value for {var}, {allowed} is expected.")
126 except KeyError: 127 except KeyError:
127 pass 128 pass
128 else: 129 else:
129 prepare(self) 130 prepare(self)
130 self.validateData(task_data) 131 self.validateData(task_data)
132 if self.host.options['dev_mode']:
133 dirs = task_data.get['WATCH_DIRS']
134 for dir_ in dirs:
135 self.host.files_watcher.watchDir(
136 dir_, auto_add=True, recursive=True,
137 callback=self._autorunTask, task_name=task_name)
138
139 def _autorunTask(self, host, filepath, flags, task_name):
140 """Called when an event is received from a watched directory"""
141 if flags == ['create']:
142 return
143 return self.runTask(task_name)
144
145 @defer.inlineCallbacks
146 def runTask(self, task_name):
147 """Run a single task
148
149 @param task_name(unicode): name of the task to run
150 """
151 task_value = self.tasks[task_name]
152 self._current_task = task_name
153 log.info(_(u'== running task "{task_name}" for {site_name} =='.format(
154 task_name=task_name, site_name=self.site_name)))
155 data = task_value[u'data']
156 os.chdir(self.site_path)
157 try:
158 yield data['start'](self)
159 except Exception as e:
160 on_error = data[u'ON_ERROR']
161 if on_error == u'stop':
162 raise e
163 elif on_error == u'continue':
164 log.warning(_(u'Task "{task_name}" failed for {site_name}: {reason}')
165 .format(task_name=task_name, site_name=self.site_name, reason=e))
166 else:
167 raise exceptions.InternalError(u"we should never reach this point")
168 self._current_task = None
131 169
132 @defer.inlineCallbacks 170 @defer.inlineCallbacks
133 def runTasks(self): 171 def runTasks(self):
134 """Run all the tasks found""" 172 """Run all the tasks found"""
135 old_path = os.getcwd() 173 old_path = os.getcwd()
136 for task_name, task_value in self.tasks.iteritems(): 174 for task_name, task_value in self.tasks.iteritems():
137 self._current_task = task_name 175 yield self.runTask(task_name)
138 log.info(_(u'== running task "{task_name}" for {site_name} =='.format(
139 task_name=task_name, site_name=self.site_name)))
140 data = task_value[u'data']
141 os.chdir(self.site_path)
142 try:
143 yield data['start'](self)
144 except Exception as e:
145 on_error = data[u'ON_ERROR']
146 if on_error == u'stop':
147 raise e
148 elif on_error == u'continue':
149 log.warning(_(u'Task "{task_name}" failed for {site_name}: {reason}')
150 .format(task_name = task_name, site_name = self.site_name, reason = e))
151 else:
152 raise exceptions.InternalError(u"we should never reach this point")
153 self._current_task = None
154 os.chdir(old_path) 176 os.chdir(old_path)
155 177
156 def findCommand(self, name, *args): 178 def findCommand(self, name, *args):
157 """Find full path of a shell command 179 """Find full path of a shell command
158 180