Don't use `flake8: noqa`.
[mailman.git] / src / mailman / runners / rest.py
blob0100b0db95f46db4cb5585ab1f35874a09aea8a8
1 # Copyright (C) 2009-2016 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Start the administrative HTTP server."""
20 import signal
21 import logging
22 import threading
24 from mailman import public
25 from mailman.core.runner import Runner
26 from mailman.rest.wsgiapp import make_server
29 log = logging.getLogger('mailman.http')
32 @public
33 class RESTRunner(Runner):
34 # Don't install the standard signal handlers because as defined, they
35 # won't actually stop the TCPServer started by .serve_forever().
36 is_queue_runner = False
38 def __init__(self, name, slice=None):
39 """See `IRunner`."""
40 super().__init__(name, slice)
41 # Both the REST server and the signal handlers must run in the main
42 # thread; the former because of SQLite requirements (objects created
43 # in one thread cannot be shared with the other threads), and the
44 # latter because of Python's signal handling semantics.
46 # Unfortunately, we cannot issue a TCPServer shutdown in the main
47 # thread, because that will cause a deadlock. Yay. So what we do is
48 # to use the signal handler to notify a shutdown thread that the
49 # shutdown should happen. That thread will wake up and stop the main
50 # server.
51 self._server = make_server()
52 self._event = threading.Event()
53 def stopper(event, server): # noqa
54 event.wait()
55 server.shutdown()
56 self._thread = threading.Thread(
57 target=stopper, args=(self._event, self._server))
58 self._thread.start()
60 def run(self):
61 """See `IRunner`."""
62 self._server.serve_forever()
64 def signal_handler(self, signum, frame):
65 super().signal_handler(signum, frame)
66 if signum in (signal.SIGTERM, signal.SIGINT, signal.SIGUSR1):
67 # Set the flag that will terminate the TCPserver loop.
68 self._event.set()
70 def _one_iteration(self):
71 # Just keep going
72 if self._thread.is_alive():
73 self._thread.join(timeout=0.1)
74 return 1
76 def _snooze(self, filecnt):
77 pass