pyobs-core 2.29.0.dev1__py3-none-any.whl → 2.29.0.dev2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyobs/application.py +17 -5
- pyobs/cli/pyobs.py +10 -0
- pyobs/cli/pyobsd.py +24 -1
- pyobs/utils/shutdown.py +70 -2
- {pyobs_core-2.29.0.dev1.dist-info → pyobs_core-2.29.0.dev2.dist-info}/METADATA +1 -1
- {pyobs_core-2.29.0.dev1.dist-info → pyobs_core-2.29.0.dev2.dist-info}/RECORD +9 -9
- {pyobs_core-2.29.0.dev1.dist-info → pyobs_core-2.29.0.dev2.dist-info}/WHEEL +0 -0
- {pyobs_core-2.29.0.dev1.dist-info → pyobs_core-2.29.0.dev2.dist-info}/entry_points.txt +0 -0
- {pyobs_core-2.29.0.dev1.dist-info → pyobs_core-2.29.0.dev2.dist-info}/licenses/LICENSE +0 -0
pyobs/application.py
CHANGED
|
@@ -113,6 +113,7 @@ class Application:
|
|
|
113
113
|
influx_log: InfluxLogConfig | None = None,
|
|
114
114
|
iers_offline: bool = False,
|
|
115
115
|
pid_file: str | None = None,
|
|
116
|
+
stop_timeout: float = 30.0,
|
|
116
117
|
**kwargs: Any,
|
|
117
118
|
):
|
|
118
119
|
"""Initializes a pyobs application.
|
|
@@ -142,7 +143,12 @@ class Application:
|
|
|
142
143
|
pid_file: PID file of a daemonized module, if any. A module that needs more time to shut
|
|
143
144
|
down then writes its deadline into a stop file next to it (see pyobs.utils.shutdown),
|
|
144
145
|
which pyobsd and web-admin respect before sending SIGKILL.
|
|
146
|
+
stop_timeout: Seconds the shutdown may take after the first signal, unless the module asks for more
|
|
147
|
+
time (see pyobs.utils.shutdown). After that, plus a short grace time, the process ends itself.
|
|
148
|
+
pyobsd passes its own stop_timeout here.
|
|
145
149
|
"""
|
|
150
|
+
self._stop_timeout = float(stop_timeout)
|
|
151
|
+
self._stopping = False
|
|
146
152
|
# stop file for extend_stop_timeout(), removed again at the end of run(); one left behind by an earlier
|
|
147
153
|
# run (e.g. killed during shutdown) would make this run look like it is stopping
|
|
148
154
|
shutdown.set_stop_file(shutdown.stop_file_for(pid_file) if pid_file is not None else None)
|
|
@@ -371,8 +377,18 @@ class Application:
|
|
|
371
377
|
def _signal_handler(self, sig: int) -> None:
|
|
372
378
|
"""React to signals and quit the module."""
|
|
373
379
|
|
|
374
|
-
#
|
|
380
|
+
# only the first signal starts the shutdown. The handlers stay installed, since removing them would
|
|
381
|
+
# restore the default action, and a second SIGTERM (e.g. "pyobsd stop" run again) would then kill the
|
|
382
|
+
# module in the middle of its shutdown (e.g. while a camera warms up)
|
|
383
|
+
if self._stopping:
|
|
384
|
+
log.info("Got signal: %s, already shutting down.", sig)
|
|
385
|
+
return
|
|
386
|
+
self._stopping = True
|
|
387
|
+
|
|
388
|
+
# let pyobsd/web-admin show the module as "stopping" until it is gone, and end the process if that
|
|
389
|
+
# takes too long, also when nobody waits to send SIGKILL
|
|
375
390
|
shutdown.mark_stopping()
|
|
391
|
+
shutdown.start_watchdog(self._stop_timeout)
|
|
376
392
|
|
|
377
393
|
if self._module is not None:
|
|
378
394
|
self._module.quit()
|
|
@@ -383,11 +399,7 @@ class Application:
|
|
|
383
399
|
self._main_task.cancel()
|
|
384
400
|
self._loop.stop()
|
|
385
401
|
|
|
386
|
-
# reset signal handlers
|
|
387
402
|
log.info("Got signal: %s, shutting down.", sig)
|
|
388
|
-
loop = asyncio.get_running_loop()
|
|
389
|
-
loop.remove_signal_handler(signal.SIGTERM)
|
|
390
|
-
loop.add_signal_handler(signal.SIGINT, lambda: None)
|
|
391
403
|
|
|
392
404
|
async def _main(self) -> None:
|
|
393
405
|
"""Actually run the application."""
|
pyobs/cli/pyobs.py
CHANGED
|
@@ -24,6 +24,7 @@ class PyobsCLI(CLI):
|
|
|
24
24
|
"influx_log",
|
|
25
25
|
"debug_time",
|
|
26
26
|
"iers_offline",
|
|
27
|
+
"stop_timeout",
|
|
27
28
|
]
|
|
28
29
|
|
|
29
30
|
def init_cli(self) -> None:
|
|
@@ -63,6 +64,15 @@ class PyobsCLI(CLI):
|
|
|
63
64
|
default=self._config.get("iers_offline", False),
|
|
64
65
|
)
|
|
65
66
|
|
|
67
|
+
# shutdown
|
|
68
|
+
self._parser.add_argument(
|
|
69
|
+
"--stop-timeout",
|
|
70
|
+
type=float,
|
|
71
|
+
default=self._config.get("stop_timeout", 30.0),
|
|
72
|
+
metavar="SECONDS",
|
|
73
|
+
help="seconds the shutdown may take before the module ends itself, unless it asked for more time",
|
|
74
|
+
)
|
|
75
|
+
|
|
66
76
|
# debug stuff
|
|
67
77
|
self._parser.add_argument(
|
|
68
78
|
"--debug-time",
|
pyobs/cli/pyobsd.py
CHANGED
|
@@ -325,7 +325,21 @@ class PyobsDaemon:
|
|
|
325
325
|
stopping[module] = pid
|
|
326
326
|
|
|
327
327
|
if wait:
|
|
328
|
-
|
|
328
|
+
try:
|
|
329
|
+
self._wait_stopped(stopping)
|
|
330
|
+
except KeyboardInterrupt:
|
|
331
|
+
# the modules run in their own session and keep shutting down without us
|
|
332
|
+
print("\nStopped waiting.")
|
|
333
|
+
now = time.time()
|
|
334
|
+
for module, pid in stopping.items():
|
|
335
|
+
if not self._is_alive(pid):
|
|
336
|
+
continue
|
|
337
|
+
deadline = read_stop_deadline(self._stop_file(module))
|
|
338
|
+
until = ""
|
|
339
|
+
if deadline is not None and deadline > now:
|
|
340
|
+
until = f" (up to {self._fmt_wait(deadline - now)})"
|
|
341
|
+
print(f'{module} keeps shutting down in the background{until}, see "pyobsd status".')
|
|
342
|
+
sys.exit(130)
|
|
329
343
|
|
|
330
344
|
def restart(self, modules: list[str] | None = None) -> None:
|
|
331
345
|
self.stop(modules=modules)
|
|
@@ -433,6 +447,9 @@ class PyobsDaemon:
|
|
|
433
447
|
self._pid_file(module),
|
|
434
448
|
"--log-level",
|
|
435
449
|
self._log_level,
|
|
450
|
+
# the module ends itself if its shutdown takes longer than this (or what it asked for)
|
|
451
|
+
"--stop-timeout",
|
|
452
|
+
str(self._stop_timeout),
|
|
436
453
|
self._config_file(module),
|
|
437
454
|
]
|
|
438
455
|
if self._file_log:
|
|
@@ -499,6 +516,12 @@ class PyobsDaemon:
|
|
|
499
516
|
print(f"{module} is not running.")
|
|
500
517
|
return None
|
|
501
518
|
|
|
519
|
+
# already shutting down (e.g. an earlier "pyobsd stop" was interrupted): don't signal again, a module with
|
|
520
|
+
# an older pyobs-core would get killed by a second SIGTERM in the middle of its shutdown
|
|
521
|
+
if os.path.exists(self._stop_file(module)):
|
|
522
|
+
print(f"{module} is already shutting down.")
|
|
523
|
+
return pid
|
|
524
|
+
|
|
502
525
|
try:
|
|
503
526
|
os.kill(pid, signal.SIGTERM)
|
|
504
527
|
except OSError:
|
pyobs/utils/shutdown.py
CHANGED
|
@@ -9,7 +9,12 @@ A module with a long shutdown (e.g. a cooled camera warming up in ``close()``) c
|
|
|
9
9
|
started with ``--pid-file`` (pyobsd's and web-admin's PID-file mode). They read it with
|
|
10
10
|
:func:`read_stop_deadline` and only send SIGKILL after that deadline.
|
|
11
11
|
|
|
12
|
-
Without either, it
|
|
12
|
+
Without either, it only moves the module's own deadline (see :func:`start_watchdog`).
|
|
13
|
+
|
|
14
|
+
On the first signal, the application also starts a watchdog thread (:func:`start_watchdog`). It ends the
|
|
15
|
+
process with ``os._exit(1)`` if the shutdown runs past the deadline, so a module whose shutdown hangs does not
|
|
16
|
+
stay "stopping" forever when nobody is waiting to send SIGKILL (``pyobsd stop --no-wait``, Ctrl+C on
|
|
17
|
+
``pyobsd stop``).
|
|
13
18
|
"""
|
|
14
19
|
|
|
15
20
|
from __future__ import annotations
|
|
@@ -17,14 +22,23 @@ from __future__ import annotations
|
|
|
17
22
|
import logging
|
|
18
23
|
import os
|
|
19
24
|
import socket
|
|
25
|
+
import threading
|
|
20
26
|
import time
|
|
21
27
|
|
|
22
28
|
log = logging.getLogger(__name__)
|
|
23
29
|
|
|
24
30
|
STOP_FILE_SUFFIX = ".stopping"
|
|
25
31
|
|
|
32
|
+
# seconds the watchdog waits past the deadline before ending the process, so that pyobsd's SIGKILL (at the
|
|
33
|
+
# deadline) usually comes first, and the module only ends itself when nobody else does
|
|
34
|
+
WATCHDOG_GRACE_SECONDS = 5.0
|
|
35
|
+
|
|
26
36
|
_stop_file: str | None = None
|
|
27
37
|
|
|
38
|
+
# the module's own deadline (epoch seconds), as last set by mark_stopping()/extend_stop_timeout()
|
|
39
|
+
_deadline: float | None = None
|
|
40
|
+
_watchdog: threading.Thread | None = None
|
|
41
|
+
|
|
28
42
|
|
|
29
43
|
def stop_file_for(pid_file: str) -> str:
|
|
30
44
|
"""Path of the stop file belonging to a PID file."""
|
|
@@ -43,6 +57,8 @@ def mark_stopping() -> None:
|
|
|
43
57
|
In PID-file mode, the stop file is what makes pyobsd and web-admin show the module as "stopping". The deadline
|
|
44
58
|
written is "now" (or a later one that is already there), which extends nothing, since they always wait at least
|
|
45
59
|
their own stop timeout. Under systemd, sends ``STOPPING=1``. Never raises."""
|
|
60
|
+
global _deadline
|
|
61
|
+
_deadline = max(time.time(), _deadline or 0.0)
|
|
46
62
|
notify("STOPPING=1")
|
|
47
63
|
if _stop_file is not None:
|
|
48
64
|
_write_deadline(_stop_file, max(time.time(), read_stop_deadline(_stop_file) or 0.0))
|
|
@@ -50,13 +66,18 @@ def mark_stopping() -> None:
|
|
|
50
66
|
|
|
51
67
|
def extend_stop_timeout(seconds: float) -> None:
|
|
52
68
|
"""Asks for up to ``seconds`` more time to shut down, counted from now. Never raises."""
|
|
69
|
+
global _deadline
|
|
70
|
+
_deadline = time.time() + seconds
|
|
53
71
|
notify(f"EXTEND_TIMEOUT_USEC={int(seconds * 1_000_000)}")
|
|
54
72
|
if _stop_file is not None:
|
|
55
73
|
_write_deadline(_stop_file, time.time() + seconds)
|
|
56
74
|
|
|
57
75
|
|
|
58
76
|
def clear_stop_file() -> None:
|
|
59
|
-
"""Removes the stop file, if any. Called by the application at the end of its
|
|
77
|
+
"""Removes the stop file, if any, and forgets the deadline. Called by the application at the end of its
|
|
78
|
+
shutdown."""
|
|
79
|
+
global _deadline
|
|
80
|
+
_deadline = None
|
|
60
81
|
if _stop_file is None:
|
|
61
82
|
return
|
|
62
83
|
try:
|
|
@@ -76,6 +97,51 @@ def read_stop_deadline(path: str) -> float | None:
|
|
|
76
97
|
return None
|
|
77
98
|
|
|
78
99
|
|
|
100
|
+
def current_deadline() -> float | None:
|
|
101
|
+
"""The module's own deadline (epoch seconds), as last set by :func:`mark_stopping` or
|
|
102
|
+
:func:`extend_stop_timeout`, or None."""
|
|
103
|
+
return _deadline
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def start_watchdog(stop_timeout: float, grace: float = WATCHDOG_GRACE_SECONDS, interval: float = 1.0) -> None:
|
|
107
|
+
"""Starts a daemon thread that ends the process if its shutdown takes too long. Called by the application
|
|
108
|
+
on the first signal; calling it again does nothing.
|
|
109
|
+
|
|
110
|
+
"Too long" is the same as for pyobsd: ``stop_timeout`` after now, or later if the module asked for more
|
|
111
|
+
time via :func:`extend_stop_timeout`, plus ``grace``. The thread checks the deadline every ``interval``
|
|
112
|
+
seconds, so later extensions apply. Being a daemon thread, it also covers a hang after ``run()`` returned,
|
|
113
|
+
e.g. while the interpreter waits for a blocked executor thread."""
|
|
114
|
+
global _watchdog
|
|
115
|
+
if _watchdog is not None:
|
|
116
|
+
return
|
|
117
|
+
default_deadline = time.time() + stop_timeout
|
|
118
|
+
_watchdog = threading.Thread(
|
|
119
|
+
target=_watch, args=(default_deadline, grace, interval), name="pyobs-shutdown-watchdog", daemon=True
|
|
120
|
+
)
|
|
121
|
+
_watchdog.start()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _watch(default_deadline: float, grace: float, interval: float) -> None:
|
|
125
|
+
last: float | None = None
|
|
126
|
+
while True:
|
|
127
|
+
# keep the last deadline seen, clear_stop_file() resets it at the very end of the shutdown
|
|
128
|
+
last = _deadline if _deadline is not None else last
|
|
129
|
+
deadline = max(default_deadline, last or 0.0) + grace
|
|
130
|
+
now = time.time()
|
|
131
|
+
if now >= deadline:
|
|
132
|
+
break
|
|
133
|
+
time.sleep(min(interval, deadline - now))
|
|
134
|
+
|
|
135
|
+
log.error("Shutdown did not finish in time, exiting now.")
|
|
136
|
+
clear_stop_file()
|
|
137
|
+
for handler in logging.getLogger().handlers:
|
|
138
|
+
try:
|
|
139
|
+
handler.flush()
|
|
140
|
+
except Exception:
|
|
141
|
+
pass
|
|
142
|
+
os._exit(1)
|
|
143
|
+
|
|
144
|
+
|
|
79
145
|
def notify(message: str) -> bool:
|
|
80
146
|
"""Sends one message to systemd's notification socket, without libsystemd.
|
|
81
147
|
|
|
@@ -119,5 +185,7 @@ __all__ = [
|
|
|
119
185
|
"extend_stop_timeout",
|
|
120
186
|
"clear_stop_file",
|
|
121
187
|
"read_stop_deadline",
|
|
188
|
+
"current_deadline",
|
|
189
|
+
"start_watchdog",
|
|
122
190
|
"notify",
|
|
123
191
|
]
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
pyobs/__init__.py,sha256=O4I43dWATvTIBYkvlJRAZZFoyozZHCWJM5O3RDk99sg,80
|
|
2
|
-
pyobs/application.py,sha256=
|
|
2
|
+
pyobs/application.py,sha256=H-sjZWfGqHCGocgCkDQYJVsbQPJAoFLax_7bG_9Zmig,23664
|
|
3
3
|
pyobs/background_task.py,sha256=bwGEDwQFfPMEH2DIrMEU1pA10uscicTPZwGuogCfLr8,2437
|
|
4
4
|
pyobs/object.py,sha256=ft9eiDza7lIU9acJZB9dgtTvjnNeS4dkvNrezswevZA,24356
|
|
5
5
|
pyobs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
pyobs/version.py,sha256=y7kcc_eQKcC7SFFUYIr8dhkhDYXwshVu7imSEbC9VW0,257
|
|
7
7
|
pyobs/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
8
|
pyobs/cli/_cli.py,sha256=NTPVrUNE-b2MspccFEgfeDsnmtFqYTGkMpL9_tixiEc,2050
|
|
9
|
-
pyobs/cli/pyobs.py,sha256=
|
|
10
|
-
pyobs/cli/pyobsd.py,sha256=
|
|
9
|
+
pyobs/cli/pyobs.py,sha256=uFcIpK1prJPTom5LoC2coL0E57QqxDVHd6mjIswM-X8,4472
|
|
10
|
+
pyobs/cli/pyobsd.py,sha256=Cq6B6lHsSzTzoPPTGBg9HJCxPeO00Qx2Oij6HwoSH8c,25352
|
|
11
11
|
pyobs/cli/pyobsw.py,sha256=uXrJoz8fpGjADPUyf0-MxNcCKqVK2PkI8i9TWS-VS7w,740
|
|
12
12
|
pyobs/comm/__init__.py,sha256=Pc_hlxXLcVf5sWtf_wCJ9PbC1_sGJHaNsF6qWBEQJzU,1567
|
|
13
13
|
pyobs/comm/comm.py,sha256=-HEM8fkK62bWvg9gFRdcaKdsNAlNYnAXwU8kkfoXs64,29202
|
|
@@ -419,7 +419,7 @@ pyobs/utils/modulegui.py,sha256=k7JhACvBFWyqOxDfwCfU2v7Z-YmTN_QoaOEx2mmy2pw,1744
|
|
|
419
419
|
pyobs/utils/parallel.py,sha256=UJVDlexRsYicx_lEB6TJF_Wh7Lqf6EnvBWao32FMnlw,3126
|
|
420
420
|
pyobs/utils/serialization.py,sha256=QdJq20rGZ5qw9QlZK9AITR2il2iKKZEqv9Ftr7rPg9A,6498
|
|
421
421
|
pyobs/utils/shellcommand.py,sha256=gdqT62Zqeynws3JHA96OzTnDn4VHEwcB6f4UFGaGCLY,6385
|
|
422
|
-
pyobs/utils/shutdown.py,sha256=
|
|
422
|
+
pyobs/utils/shutdown.py,sha256=jaUH9Jy78slIqo9QLoaiyRuqg4UgXaBawGY_8MJXfj0,7005
|
|
423
423
|
pyobs/utils/stretch.py,sha256=u0rVY3Mk6r6NsZVTisjYBa6c2kBQzm_gqJUWvJAxng0,6194
|
|
424
424
|
pyobs/utils/time.py,sha256=CUWV7xfT5GYRP8coe-kbPxK60-JB90z9-K14JdksbVY,3229
|
|
425
425
|
pyobs/utils/units.py,sha256=_C2CQMSpTEF3vVwiC9tZgrgyejmKcq-25h9JF2g2_Cc,1662
|
|
@@ -479,8 +479,8 @@ pyobs/vfs/vfs.py,sha256=PTZcapn4U3yr2Ksa0xfbP0ssSiJhA6_XzgmNMdNav3U,14077
|
|
|
479
479
|
pyobs/vfs/filelists/__init__.py,sha256=NaTGFjLGh043j_F7kWEx_cXaII7t3CbYguRk2g5hEqw,111
|
|
480
480
|
pyobs/vfs/filelists/filelist.py,sha256=kCco_5G2gLH-nxWik-wcs6lGuc_-3LeeyR9L4_srf30,201
|
|
481
481
|
pyobs/vfs/filelists/testing.py,sha256=M8f2BULdjRCc7JFOKhAH7Kwre_uVQC0xLcv9xGTfNFw,425
|
|
482
|
-
pyobs_core-2.29.0.
|
|
483
|
-
pyobs_core-2.29.0.
|
|
484
|
-
pyobs_core-2.29.0.
|
|
485
|
-
pyobs_core-2.29.0.
|
|
486
|
-
pyobs_core-2.29.0.
|
|
482
|
+
pyobs_core-2.29.0.dev2.dist-info/METADATA,sha256=8iXmY86MTsIsqdt614xLBddg5t3nz0C-2m4_z2iKxyo,3928
|
|
483
|
+
pyobs_core-2.29.0.dev2.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
484
|
+
pyobs_core-2.29.0.dev2.dist-info/entry_points.txt,sha256=0ceIn3dTDwWdGdJ5VdTvYim7vKkdTe6aqqpoaG065UY,109
|
|
485
|
+
pyobs_core-2.29.0.dev2.dist-info/licenses/LICENSE,sha256=Za86Qj8Jl9nLP446ogz9uK_nKoD5qJLcNZj6Qu2UdFQ,1101
|
|
486
|
+
pyobs_core-2.29.0.dev2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|