관리자 상태 기록 보존 정책을 API 안에서 하루 한 번 실행한다

소유자가 2026-10-27 실제 적용과 정기 실행을 승인했다. 원본 90일·집계 365일·
집계 대상 7일 창은 2026-10-20 운영 dry-run과 같은 값으로 고정하고, 데이터를
지우는 작업이라 기본값은 꺼짐이다. DB 풀이 정상일 때만 예약하며 실패는 기록만
하고 다음 회차를 계속한다. NAS compose는 환경변수를 명시 전달하므로 설정 3개를
api 환경에 추가한다.
This commit is contained in:
Yun Chan 2026-10-27 11:20:44 +09:00
parent 8d6201e19e
commit b6de32c1dd
6 changed files with 210 additions and 0 deletions

View file

@ -2,11 +2,23 @@
from __future__ import annotations
import asyncio
from dataclasses import dataclass
import logging
import re
from ..config import settings
from ..db import acquire
# 2026-10-20 운영 dry-run과 같은 값으로 소유자가 승인한 고정 보존 창이다.
# 설정으로 노출하지 않는다.
_RETENTION_ROLLUP_DAYS = 7
_RETENTION_RETENTION_DAYS = 90
_RETENTION_ROLLUP_RETENTION_DAYS = 365
_RETENTION_TASK: asyncio.Task[None] | None = None
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class AdminHealthMaintenanceResult:
@ -266,3 +278,55 @@ async def maintain_admin_health_events(
prunable_rollup_count=_row_int(rollup_prune_stats, "rollup_count"),
deleted_rollups=deleted_rollups,
)
async def _retention_loop() -> None:
delay = settings.admin_health_retention_startup_delay_seconds
if delay:
await asyncio.sleep(delay)
while True:
try:
result = await maintain_admin_health_events(
rollup_days=_RETENTION_ROLLUP_DAYS,
retention_days=_RETENTION_RETENTION_DAYS,
rollup_retention_days=_RETENTION_ROLLUP_RETENTION_DAYS,
apply=True,
)
logger.info(
"관리자 헬스 보존 정책 적용 완료: deleted_events=%d deleted_rollups=%d "
"upserted_rollups=%d",
result.deleted_events,
result.deleted_rollups,
result.upserted_rollups,
)
except asyncio.CancelledError:
raise
except Exception:
logger.exception("관리자 헬스 보존 정책 scheduler cycle 실패")
await asyncio.sleep(settings.admin_health_retention_interval_seconds)
def schedule_admin_health_retention() -> asyncio.Task[None] | None:
global _RETENTION_TASK
if not settings.admin_health_retention_enabled:
return None
if _RETENTION_TASK is not None and not _RETENTION_TASK.done():
return _RETENTION_TASK
_RETENTION_TASK = asyncio.create_task(
_retention_loop(),
name="admin-health-retention",
)
return _RETENTION_TASK
async def stop_admin_health_retention() -> None:
global _RETENTION_TASK
task = _RETENTION_TASK
_RETENTION_TASK = None
if task is None or task.done():
return
task.cancel()
try:
await task
except asyncio.CancelledError:
pass