From 8f8f38cc4cf3bcff98d08daba94238dcfb2ab8cb Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Thu, 15 Feb 2024 22:49:55 -0500 Subject: [PATCH] add thread for cache cleanup --- relay/application.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/relay/application.py b/relay/application.py index 32577ef..2e8040d 100644 --- a/relay/application.py +++ b/relay/application.py @@ -11,6 +11,7 @@ import typing from aiohttp import web from aputils.signer import Signer from datetime import datetime, timedelta +from threading import Event, Thread from . import logger as logging from .cache import get_cache @@ -44,6 +45,7 @@ class Application(web.Application): self['proc'] = None self['signer'] = None self['start_time'] = None + self['cleanup_thread'] = None self['config'] = Config(cfgpath, load = True) self['database'] = get_database(self.config) @@ -150,12 +152,15 @@ class Application(web.Application): self.set_signal_handler(True) self['proc'] = subprocess.Popen(cmd) # pylint: disable=consider-using-with + self['cleanup_thread'] = CacheCleanupThread(self) + self['cleanup_thread'].start() def stop(self, *_) -> None: if not self['proc']: return + self['cleanup_thread'].stop() self['proc'].terminate() time_wait = 0.0 @@ -174,6 +179,34 @@ class Application(web.Application): self.database.close() +class CacheCleanupThread(Thread): + def __init__(self, app: Application): + Thread.__init__(self) + + self.app = app + self.running = Event() + + + def run(self) -> None: + cache = get_cache(self.app) + + while self.running.is_set(): + time.sleep(3600) + logging.verbose("Removing old cache items") + cache.delete_old(14) + + cache.close() + + + def start(self) -> None: + self.running.set() + Thread.start(self) + + + def stop(self) -> None: + self.running.clear() + + async def handle_access_log(request: web.Request, response: web.Response) -> None: address = request.headers.get( 'X-Forwarded-For',