Skip to content

Commit 0cf313d

Browse files
committed
Improve shut down handling, document related issue
1 parent 176c3ca commit 0cf313d

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

docs/deploy.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ We assume that you have already copied your application working directory to the
7373

7474
[Service]
7575
Type=simple
76-
Restart=always
76+
Restart=on-failure
7777
RestartSec=1
7878
User=www-data
7979
Group=www-data
@@ -85,6 +85,8 @@ We assume that you have already copied your application working directory to the
8585

8686
Adapt the options as needed. ``Description`` should be a meaningful description of your Webware application. With ``User`` and ``Group`` you specify under which user and group your Webware application shall run, see the remarks above. Adapt the ``EexecStart`` option so that it uses the path to your virtual environment, and specify the path to your application working directory as the ``WorkingDirectory`` option. You can change the host address, port and add other options to ``webware serve`` in the ``ExecStart`` option. By default, the server runs on port 8080, but you can specify a different port using the ``-p`` option. If you want to run waitress behind a reverse proxy, for instance because you want to run on port 80 which needs superuser privileges or you need TLS support which is not provided by waitress, then you you need to serve only on the local interface, using options such as ``-l 127.0.0.1 -p 8080``. The ``--prod`` option tells Webware to run in production mode.
8787

88+
Note that if you use the ``--reload`` option with ``webware serve`` in ``ExecStart``, then you should also set ``KillMode=process`` and ``ExecStopPost=/bin/sleep 1`` in the service file to make sure that Webware can be shut down properly.
89+
8890
After adding or changing the service file, you need to run the following command so that systemd refreshes its configuration::
8991

9092
sudo systemctl daemon-reload

webware/Application.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ def __init__(self, path=None, settings=None, development=None):
242242

243243
self._needsShutDown = [True]
244244
atexit.register(self.shutDown)
245-
signal.signal(signal.SIGTERM, self.sigTerm)
245+
self._sigTerm = signal.signal(signal.SIGTERM, self.sigTerm)
246246
try:
247-
signal.signal(signal.SIGHUP, self.sigTerm)
247+
self._sigHup = signal.signal(signal.SIGHUP, self.sigTerm)
248248
except AttributeError:
249249
pass # SIGHUP does not exist on Windows
250250

@@ -386,10 +386,9 @@ def shutDown(self):
386386
if tm:
387387
tm.stop()
388388
# Call all registered shutdown handlers
389-
shutDownHandlers = self._shutDownHandlers
390-
while shutDownHandlers:
389+
for shutDownHandler in self._shutDownHandlers:
391390
try:
392-
shutDownHandlers.pop(0)()
391+
shutDownHandler()
393392
except Exception:
394393
pass
395394
print("Application has been successfully shut down.")
@@ -405,9 +404,9 @@ def addShutDownHandler(self, func):
405404

406405
def sigTerm(self, _signum, _frame):
407406
"""Signal handler for terminating the process."""
408-
print("\nApplication has been signaled to terminate.")
409-
self.shutDown()
410-
sys.exit()
407+
if self._needsShutDown:
408+
print("\nApplication has been signaled to terminate.")
409+
self.shutDown()
411410

412411
# endregion Init
413412

0 commit comments

Comments
 (0)