From 6f3a1db17d7a81377687612cb2309e8a8a247dde Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Thu, 15 Feb 2024 21:53:46 -0500 Subject: [PATCH] ensure cache, database, and http client are closed on quit --- relay/application.py | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/relay/application.py b/relay/application.py index 20695d7..8fb70df 100644 --- a/relay/application.py +++ b/relay/application.py @@ -57,6 +57,7 @@ class Application(web.Application): return self.on_response_prepare.append(handle_access_log) + self.on_cleanup.append(handle_cleanup) for path, view in VIEWS: self.router.add_view(path, view) @@ -172,30 +173,8 @@ class Application(web.Application): self.set_signal_handler(False) self['proc'] = None - -# not used, but keeping just in case -class GunicornRunner(WSGIApplication): - def __init__(self, app: Application): - self.app = app - self.app_uri = 'relay.application:main_gunicorn' - self.options = { - 'bind': f'{app.config.listen}:{app.config.port}', - 'worker_class': 'aiohttp.GunicornWebWorker', - 'workers': app.config.workers, - 'raw_env': f'CONFIG_FILE={app.config.path}' - } - - WSGIApplication.__init__(self) - - - def load_config(self): - for key, value in self.options.items(): - self.cfg.set(key, value) - - - def run(self): - logging.info('Starting webserver for %s', self.app.config.domain) - WSGIApplication.run(self) + self.cache.close() + self.database.close() async def handle_access_log(request: web.Request, response: web.Response) -> None: @@ -218,6 +197,12 @@ async def handle_access_log(request: web.Request, response: web.Response) -> Non ) +async def handle_cleanup(app: Application) -> None: + await app.client.close() + app.cache.close() + app.database.close() + + async def main_gunicorn(): try: app = Application(os.environ['CONFIG_FILE'], gunicorn = True)