Commit e1f18d81 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

implement graceful shutdown in the controller

parent 48046815
......@@ -908,6 +908,15 @@ class ImageManager:
def push(self, version_id: int):
return self.sandbox_push_manager.process(version_id)
@asyncio.coroutine
def shutdown(self, **kw):
yield from asyncio.gather(
self.sandbox_pull_manager.shutdown(),
self.sandbox_push_manager.shutdown(),
self.swarm_pull_manager.shutdown(),
return_exceptions=True)
class DockerController:
def __init__(self, sandbox_host, swarm_host, mysql_host,
......@@ -935,7 +944,8 @@ class DockerController:
self.datastore_path = datastore_path
self.sandbox_path = sandbox_path
self._finished = None
self._task = None
self._shutdown_requested = None
img = "busybox:latest"
try:
......@@ -1028,26 +1038,43 @@ class DockerController:
return
def shutdown(self):
#TODO: graceful shutdown
# - close socket
# - wait for all managers to finish their running tasks
# - cancel other tasks
self._finished.set_result(None)
def run(self):
assert self._finished is None
try:
self._finished = asyncio.Future()
if not self._shutdown_requested.done():
self._shutdown_requested.set_result(None)
return self._task
@asyncio.coroutine
def _run(self):
assert self._shutdown_requested is None
try:
loop = asyncio.get_event_loop()
self._shutdown_requested = asyncio.Future()
loop.add_signal_handler(signal.SIGTERM, self.shutdown)
loop.add_signal_handler(signal.SIGINT, self.shutdown)
loop.add_reader(self.sock, self._sock_callback)
self.check_db()
loop.run_until_complete(self._finished)
try:
yield from self._shutdown_requested
finally:
# graceful shutdown
# close socket (to disable incoming notifications)
self.sock.close()
# terminate all pending tasks
yield from asyncio.gather(
self.image_manager.shutdown(),
self.sandbox_manager.shutdown(),
self.job_manager.shutdown(),
return_exceptions=True)
finally:
self.sock.close()
self.sandbox.close()
self.swarm.close()
def run(self):
asyncio.get_event_loop().run_until_complete(self._run())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment