Files
mtusi2026lab2/src/messenger/attachments/entrypoints/sweeper.py
T
ArtyomandClaude Opus 5.5 5ae3d77152 Carry over the lab 5 solution
Bring the application, own tests, Dockerfile, toolchain, compose stack,
HAProxy, k6, RustFS and RabbitMQ configuration, migrations, hooks,
scenario scripts and the CI job from the lab 5 solution commit 64d5c67,
keep the lab 5 Makefile targets on top of the unchanged skeleton
targets, and append the lab 5 solution variables to the skeleton
.env.example. The package and the application follow the contract
version 1.6.0, and the image, the throwaway test containers and the
broker vhost description are tagged lab6 so they do not collide with a
lab 5 stand on the same host.

This is the starting point of the lab 6 cycle: lint, own tests and the
offline skeleton check pass before any lab 6 change.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-29 03:51:58 +03:00

54 lines
1.8 KiB
Python

import asyncio
from contextlib import suppress
from datetime import timedelta
from dishka import AsyncContainer
from messenger.attachments.application.bus import AttachmentsCommandBus
from messenger.attachments.application.commands import SweepAbandonedUploads
from messenger.attachments.config import AttachmentsSettings
from messenger.shared.application.ports import Logger
class UploadSweeper:
__slots__ = ("_command", "_container", "_interval", "_logger", "_task")
def __init__(
self, container: AsyncContainer, settings: AttachmentsSettings, logger: Logger
) -> None:
self._container = container
self._logger = logger
self._interval = settings.upload_sweep_interval_seconds
self._command = SweepAbandonedUploads(
grace=timedelta(seconds=settings.upload_grace_seconds),
limit=settings.upload_sweep_batch,
)
self._task: asyncio.Task[None] | None = None
def start(self) -> None:
self._task = asyncio.create_task(self._run())
async def stop(self) -> None:
if self._task is None:
return
self._task.cancel()
with suppress(asyncio.CancelledError):
await self._task
self._task = None
async def sweep(self) -> int:
async with self._container() as scope:
bus = await scope.get(AttachmentsCommandBus)
return await bus.execute(self._command)
async def _run(self) -> None:
while True:
await asyncio.sleep(self._interval)
try:
swept = await self.sweep()
except Exception as error:
self._logger.warning("attachments.sweep_failed", error=type(error).__name__)
continue
if swept:
self._logger.info("attachments.swept", count=swept)