Skip to content
Snippets Groups Projects
Commit bc1afc5a authored by Christoph Stahl's avatar Christoph Stahl
Browse files

Initial work for clean up done

parent 31346ba9
No related branches found
No related tags found
No related merge requests found
...@@ -58,7 +58,7 @@ async def root_handler(request: Any) -> Any: ...@@ -58,7 +58,7 @@ async def root_handler(request: Any) -> Any:
return web.FileResponse("syng/static/index.html") return web.FileResponse("syng/static/index.html")
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -114,7 +114,9 @@ class State: ...@@ -114,7 +114,9 @@ class State:
recent: list[Entry] recent: list[Entry]
sid: str sid: str
config: Config config: Config
last_seen: datetime.datetime = field(init=False, default_factory=datetime.datetime.now) last_seen: datetime.datetime = field(
init=False, default_factory=datetime.datetime.now
)
clients: dict[str, State] = {} clients: dict[str, State] = {}
...@@ -326,7 +328,7 @@ async def handle_pop_then_get_next(sid: str) -> None: ...@@ -326,7 +328,7 @@ async def handle_pop_then_get_next(sid: str) -> None:
if sid != state.sid: if sid != state.sid:
return return
old_entry = await state.queue.popleft() old_entry = await state.queue.popleft()
state.recent.append(old_entry) state.recent.append(old_entry)
state.last_seen = datetime.datetime.now() state.last_seen = datetime.datetime.now()
...@@ -718,12 +720,38 @@ async def handle_search(sid: str, data: dict[str, Any]) -> None: ...@@ -718,12 +720,38 @@ async def handle_search(sid: str, data: dict[str, Any]) -> None:
room=sid, room=sid,
) )
@aiocron.crontab('* * * * *')
async def cleanup(): async def cleanup(app):
logger.info("Start Cleanup") logger.info("Start Cleanup")
to_remove = []
for sid, state in clients.items(): for sid, state in clients.items():
logger.info("Client %d, last seen: %s", sid, str(state.last_seen)) logger.info("Client %s, last seen: %s", sid, str(state.last_seen))
if state.last_seen + datetime.timedelta(hours=4) < datetime.datetime.now():
logger.info("No activity for 4 hours, removing %s", sid)
to_remove.append(sid)
for sid in to_remove:
await sio.disconnect(sid)
del clients[sid]
now = datetime.datetime.now()
today = datetime.datetime(now.year, now.month, now.day)
next = today + datetime.timedelta(days=1)
# next = now + datetime.timedelta(seconds=2)
offset = next.timestamp() - now.timestamp()
loop_next = asyncio.get_event_loop().time() + offset
logger.info("Next Cleanup at %s", str(next))
asyncio.get_event_loop().call_at(loop_next, lambda: asyncio.create_task(cleanup(app)))
async def background_tasks(app):
app["cron_cleanup"] = asyncio.create_task(cleanup(app))
yield
app["cron_cleanup"].cancel()
await app["cron_cleanup"]
def main() -> None: def main() -> None:
""" """
Configure and start the server. Configure and start the server.
...@@ -743,6 +771,8 @@ def main() -> None: ...@@ -743,6 +771,8 @@ def main() -> None:
app.router.add_route("*", "/{room}", root_handler) app.router.add_route("*", "/{room}", root_handler)
app.router.add_route("*", "/{room}/", root_handler) app.router.add_route("*", "/{room}/", root_handler)
app.cleanup_ctx.append(background_tasks)
web.run_app(app, host=args.host, port=args.port) web.run_app(app, host=args.host, port=args.port)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment