comparison libervia/server/tasks/manager.py @ 1252:80a92eb82b7f

server (tasks manager): added a label for default site
author Goffi <goffi@goffi.org>
date Wed, 29 Apr 2020 15:00:54 +0200
parents a6c7f07f1e4d
children 6161076193e0
comparison
equal deleted inserted replaced
1251:a1606e2a92eb 1252:80a92eb82b7f
26 from sat.tools import utils 26 from sat.tools import utils
27 from libervia.server.constants import Const as C 27 from libervia.server.constants import Const as C
28 from . import implicit 28 from . import implicit
29 29
30 log = getLogger(__name__) 30 log = getLogger(__name__)
31
32 DEFAULT_SITE_LABEL = _("default site")
31 33
32 34
33 class TasksManager: 35 class TasksManager:
34 """Handle tasks of a Libervia site""" 36 """Handle tasks of a Libervia site"""
35 37
91 if task_name in self.tasks: 93 if task_name in self.tasks:
92 raise exceptions.ConflictError( 94 raise exceptions.ConflictError(
93 "A task with the name [{name}] already exists".format( 95 "A task with the name [{name}] already exists".format(
94 name=task_name)) 96 name=task_name))
95 log.debug(f"task {task_name} found") 97 log.debug(f"task {task_name} found")
96 module_name = f"{self.site_name}.task.{task_name}" 98 module_name = f"{self.site_name or C.SITE_NAME_DEFAULT}.task.{task_name}"
97 99
98 spec = importlib.util.spec_from_file_location(module_name, task_path) 100 spec = importlib.util.spec_from_file_location(module_name, task_path)
99 task_module = importlib.util.module_from_spec(spec) 101 task_module = importlib.util.module_from_spec(spec)
100 spec.loader.exec_module(task_module) 102 spec.loader.exec_module(task_module)
101 task = task_module.Task(self) 103 task = task_module.Task(self)
127 implicit_path = Path(implicit.__file__).parent 129 implicit_path = Path(implicit.__file__).parent
128 await self.parseTasksDir(implicit_path) 130 await self.parseTasksDir(implicit_path)
129 # now we check if there are tasks specific to this site 131 # now we check if there are tasks specific to this site
130 if not self.tasks_dir.is_dir(): 132 if not self.tasks_dir.is_dir():
131 log.debug(_("{name} has no task to launch.").format( 133 log.debug(_("{name} has no task to launch.").format(
132 name = self.resource.site_name or "default site")) 134 name = self.resource.site_name or DEFAULT_SITE_LABEL))
133 return 135 return
134 else: 136 else:
135 await self.parseTasksDir(self.tasks_dir) 137 await self.parseTasksDir(self.tasks_dir)
136 138
137 def _autorunTask(self, host, filepath, flags, task_name): 139 def _autorunTask(self, host, filepath, flags, task_name):
146 @param task_name(unicode): name of the task to run 148 @param task_name(unicode): name of the task to run
147 """ 149 """
148 task = self.tasks[task_name] 150 task = self.tasks[task_name]
149 self._current_task = task_name 151 self._current_task = task_name
150 log.info(_('== running task "{task_name}" for {site_name} =='.format( 152 log.info(_('== running task "{task_name}" for {site_name} =='.format(
151 task_name=task_name, site_name=self.site_name))) 153 task_name=task_name, site_name=self.site_name or DEFAULT_SITE_LABEL)))
152 os.chdir(self.site_path) 154 os.chdir(self.site_path)
153 try: 155 try:
154 await utils.asDeferred(task.start) 156 await utils.asDeferred(task.start)
155 except Exception as e: 157 except Exception as e:
156 on_error = task.ON_ERROR 158 on_error = task.ON_ERROR