Fixed cache shutdown to properly await task cancellation and added names to background tasks.
CI / Formatting (push) Successful in 5s
CI / Linting (push) Successful in 5s
CI / Tests (push) Successful in 22s
CI / Type Checking (push) Successful in 10s
CI / Spelling (push) Successful in 5s

This commit is contained in:
2026-03-22 20:17:06 -04:00
parent 23cc721612
commit c1f967ece9
3 changed files with 14 additions and 7 deletions
+5 -3
View File
@@ -179,7 +179,7 @@ class Crabstero(commands.Bot):
worker.cancel()
await asyncio.gather(*self._ingestion_workers, return_exceptions=True)
self._ingestion_workers.clear()
self.ingest_cache.stop()
await self.ingest_cache.stop()
if self._metrics_server is not None:
await self._metrics_server.stop()
if self._db is not None:
@@ -195,8 +195,10 @@ class Crabstero(commands.Bot):
def _start_ingestion_workers(self) -> None:
"""Spawn the fixed pool of ingestion worker tasks."""
for _ in range(self._ingestion_worker_count):
task = asyncio.create_task(self._ingestion_worker())
for i in range(self._ingestion_worker_count):
task = asyncio.create_task(
self._ingestion_worker(), name=f"ingestion-worker-{i}"
)
self._ingestion_workers.append(task)
async def _ingestion_worker(self) -> None:
+8 -3
View File
@@ -15,6 +15,7 @@
"""TTL cache for recently ingested messages, enabling uningest on delete."""
import asyncio
import contextlib
import time
from dataclasses import dataclass
@@ -101,12 +102,16 @@ class IngestCache:
"""Start the periodic background cleanup task."""
if self._task is not None:
return
self._task = asyncio.create_task(self._cleanup_loop())
self._task = asyncio.create_task(
self._cleanup_loop(), name="ingest-cache-cleanup"
)
def stop(self) -> None:
"""Stop the periodic background cleanup task."""
async def stop(self) -> None:
"""Stop the periodic background cleanup task and wait for it to finish."""
if self._task is not None:
self._task.cancel()
with contextlib.suppress(asyncio.CancelledError):
await self._task
self._task = None
async def _cleanup_loop(self) -> None: