Commit c6f67269 authored by Andrey Veltischev's avatar Andrey Veltischev
Browse files

Add default handlers for SIGTERM and SIGHUP signals

parent 4d7db971
import asyncio
import signal
from aiohttp import web
import prometheus_client
......@@ -29,7 +30,6 @@ class AioAppWrapper:
def initialize(self):
LOG.info(f'Initialize web app on port {self.port}')
self.runner = web.AppRunner(self.app)
self.loop.run_until_complete(self.runner.setup())
site = web.TCPSite(self.runner, '0.0.0.0', self.port)
......@@ -86,16 +86,16 @@ class ApplicationWrapper:
def handle_signal(self, _signal_name, callback):
self.loop.add_signal_handler(_signal_name, callback)
def stop_applications(self):
for app in self.apps:
app.shutdown()
def run_all(self):
try:
LOG.info('Start serving')
exceptions = self.loop.run_until_complete(
asyncio.gather(*[app.initialize() for app in self.apps], return_exceptions=True)
)
for exception in filter(lambda ex: isinstance(ex, Exception), exceptions):
LOG.error('Unexpected exception', exc_info=exception)
except KeyboardInterrupt:
pass
finally:
for app in self.apps:
app.shutdown()
self.handle_signal(signal.SIGTERM, self.stop_applications)
self.handle_signal(signal.SIGHUP, self.stop_applications)
LOG.info('Start serving')
exceptions = self.loop.run_until_complete(
asyncio.gather(*[app.initialize() for app in self.apps], return_exceptions=True)
)
for exception in filter(lambda ex: isinstance(ex, Exception), exceptions):
LOG.error('Unexpected exception', exc_info=exception)
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