Skip to content
Snippets Groups Projects
Commit ad98fae2 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel
Browse files

Worker: perform a clean shutdown when receiving SIGTERM

parent bfa52831
No related branches found
No related tags found
No related merge requests found
...@@ -65,6 +65,20 @@ def main(): ...@@ -65,6 +65,20 @@ def main():
loop=loop, loop=loop,
) )
def shutdown(signum, stackframe):
"""Perform a clean shutdown."""
# Raise an exception, so that the exception is bubbled upwards, until
# the asyncio loop stops executing the current task. Only then can we
# run things like loop.run_until_complete(mir_work_task).
log.warning('Shutting down due to signal %i', signum)
raise KeyboardInterrupt()
# Shut down cleanly upon TERM signal
import signal
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
# Start asynchronous tasks. # Start asynchronous tasks.
asyncio.ensure_future(tuqueue.work(loop=loop)) asyncio.ensure_future(tuqueue.work(loop=loop))
mir_work_task = asyncio.ensure_future(mir.work()) mir_work_task = asyncio.ensure_future(mir.work())
...@@ -76,9 +90,7 @@ def main(): ...@@ -76,9 +90,7 @@ def main():
# The worker will have logged something, we'll just shut down cleanly. # The worker will have logged something, we'll just shut down cleanly.
pass pass
except KeyboardInterrupt: except KeyboardInterrupt:
log.warning('Shutting down due to keyboard interrupt')
shutdown_future.cancel() shutdown_future.cancel()
mir_work_task.cancel() mir_work_task.cancel()
loop.run_until_complete(mir_work_task) loop.run_until_complete(mir_work_task)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment