annotate libervia/web/server/tasks/task.py @ 1519:01b8d68edd70

version: use standard Python's `dev0` instead of Libervia specific `D` for dev version
author Goffi <goffi@goffi.org>
date Tue, 06 Jun 2023 17:40:16 +0200
parents eb00d593801d
children
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
1396
822bd0139769 date update
Goffi <goffi@goffi.org>
parents: 1261
diff changeset
4 # Copyright (C) 2011-2021 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 from twisted.python.procutils import which
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
19 from libervia.backend.core.log import getLogger
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
20 from libervia.backend.tools.common import async_process
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
21 from libervia.backend.core import exceptions
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
22 from libervia.backend.core.i18n import _
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
23 from typing import Optional
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
28 class Task:
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 """Handle tasks of a Libervia site"""
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
30 # can be "stop" or "continue"
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
31 ON_ERROR: str = "stop"
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
32 LOG_OUTPUT: bool = True
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
33 # list of directories to check for restarting this task
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
34 # Task.on_dir_event will be called if it exists, otherwise
1260
6161076193e0 tasks: dir event filter:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
35 # the task will be run and Task.start will be called
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
36 WATCH_DIRS: Optional[list] = None
1261
a46d0e0f383b tasks: `AFTER` attribute to handle tasks order:
Goffi <goffi@goffi.org>
parents: 1260
diff changeset
37 # list of task names which must be prepared/started before this one
a46d0e0f383b tasks: `AFTER` attribute to handle tasks order:
Goffi <goffi@goffi.org>
parents: 1260
diff changeset
38 AFTER: Optional[list] = None
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
1261
a46d0e0f383b tasks: `AFTER` attribute to handle tasks order:
Goffi <goffi@goffi.org>
parents: 1260
diff changeset
40 def __init__(self, manager, task_name):
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
41 self.manager = manager
1261
a46d0e0f383b tasks: `AFTER` attribute to handle tasks order:
Goffi <goffi@goffi.org>
parents: 1260
diff changeset
42 self.name = task_name
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
43
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
44 @property
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
45 def host(self):
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
46 return self.manager.host
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
47
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
48 @property
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
49 def resource(self):
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
50 return self.manager.resource
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def site_path(self):
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
54 return self.manager.site_path
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def build_path(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """path where generated files will be build for this site"""
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
59 return self.manager.build_path
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
61 def config_get(self, key, default=None, value_type=None):
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
62 return self.host.config_get(self.resource, key=key, default=default,
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
63 value_type=value_type)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def site_name(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 return self.resource.site_name
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
69 def find_command(self, name, *args):
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 """Find full path of a shell command
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @param name(unicode): name of the command to find
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @param *args(unicode): extra names the command may have
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @return (unicode): full path of the command
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @raise exceptions.NotFound: can't find this command
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 names = (name,) + args
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 for n in names:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 try:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
80 cmd_path = which(n)[0]
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 except IndexError:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 pass
1189
170802865156 server (tasks): fixed findCommand when first name can't be found
Goffi <goffi@goffi.org>
parents: 1166
diff changeset
83 else:
170802865156 server (tasks): fixed findCommand when first name can't be found
Goffi <goffi@goffi.org>
parents: 1166
diff changeset
84 return cmd_path
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 raise exceptions.NotFound(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1189
diff changeset
86 "Can't find {name} command, did you install it?").format(name=name))
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def runCommand(self, command, *args, **kwargs):
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1239
diff changeset
89 kwargs['verbose'] = self.LOG_OUTPUT
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return async_process.CommandProtocol.run(command, *args, **kwargs)