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)
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
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."""
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')
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):
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
51 self
._server
= make_server()
52 self
._event
= threading
.Event()
53 def stopper(event
, server
): # noqa
56 self
._thread
= threading
.Thread(
57 target
=stopper
, args
=(self
._event
, self
._server
))
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.
70 def _one_iteration(self
):
72 if self
._thread
.is_alive():
73 self
._thread
.join(timeout
=0.1)
76 def _snooze(self
, filecnt
):