/
shutdown.py
88 lines (70 loc) · 3.33 KB
/
shutdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# This file is a part of Dramatiq.
#
# Copyright (C) 2017,2018 CLEARTYPE SRL <bogdan@cleartype.io>
#
# Dramatiq is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# Dramatiq is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import threading
import warnings
from ..logging import get_logger
from .middleware import Middleware
from .threading import Interrupt, current_platform, raise_thread_exception, supported_platforms
class Shutdown(Interrupt):
"""Exception used to interrupt worker threads when their worker
processes have been signaled for termination.
"""
class ShutdownNotifications(Middleware):
"""Middleware that interrupts actors whose worker process has been
signaled for termination.
Currently, this is only available on CPython.
Note:
This works by setting an async exception in the worker thread
that runs the actor. This means that the exception will only get
called the next time that thread acquires the GIL. Concretely,
this means that this middleware can't cancel system calls.
Parameters:
notify_shutdown(bool): When true, the actor will be interrupted
if the worker process was terminated.
"""
def __init__(self, notify_shutdown=False):
self.logger = get_logger(__name__, type(self))
self.notify_shutdown = notify_shutdown
self.notifications = set()
@property
def actor_options(self):
return {"notify_shutdown"}
def should_notify(self, actor, message):
notify = message.options.get("notify_shutdown")
if notify is None:
notify = actor.options.get("notify_shutdown")
if notify is None:
notify = self.notify_shutdown
return bool(notify)
def after_process_boot(self, broker):
if current_platform not in supported_platforms: # pragma: no cover
msg = "ShutdownNotifications cannot kill threads on your current platform (%r)."
warnings.warn(msg % current_platform, category=RuntimeWarning, stacklevel=2)
def before_worker_shutdown(self, broker, worker):
self.logger.debug("Sending shutdown notification to worker threads...")
for thread_id in self.notifications:
self.logger.info("Worker shutdown notification. Raising exception in worker thread %r.", thread_id)
raise_thread_exception(thread_id, Shutdown)
def before_process_message(self, broker, message):
actor = broker.get_actor(message.actor_name)
if self.should_notify(actor, message):
self.notifications.add(threading.get_ident())
def after_process_message(self, broker, message, *, result=None, exception=None):
thread_id = threading.get_ident()
if thread_id in self.notifications:
self.notifications.remove(thread_id)
after_skip_message = after_process_message