refactor(block-h): knowledge retention job lives with plugin; compliance via contract
This commit is contained in:
+6
-47
@@ -445,51 +445,9 @@ async def cleanup_trash_job(ctx: dict[str, Any]) -> None:
|
||||
register_job("cleanup_trash", cleanup_trash_job)
|
||||
|
||||
|
||||
# ── Knowledge retention cleanup job ─────────────────────────────────────────
|
||||
|
||||
async def cleanup_knowledge_job(ctx: dict[str, Any]) -> None:
|
||||
"""Delete old knowledge extractions (rejected or auto_created) older than 90 days.
|
||||
|
||||
Runs daily. Keeps approved extractions indefinitely.
|
||||
Iterates per-tenant for RLS compliance.
|
||||
"""
|
||||
from sqlalchemy import text as sa_text, delete as sa_delete
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.core.db import get_worker_session_factory
|
||||
from app.plugins.builtins.knowledge.models import KnowledgeExtraction
|
||||
|
||||
factory = get_worker_session_factory()
|
||||
async with factory() as db:
|
||||
try:
|
||||
tenant_result = await db.execute(sa_text("SELECT id FROM tenants"))
|
||||
tenant_ids = [row[0] for row in tenant_result]
|
||||
|
||||
cutoff = datetime.utcnow() - timedelta(days=90)
|
||||
total_deleted = 0
|
||||
for tenant_id in tenant_ids:
|
||||
await db.execute(
|
||||
sa_text("SELECT set_config('app.current_tenant_id', :tid, true)"),
|
||||
{"tid": str(tenant_id)},
|
||||
)
|
||||
# Delete rejected and auto_created extractions older than 90 days
|
||||
result = await db.execute(
|
||||
sa_delete(KnowledgeExtraction).where(
|
||||
KnowledgeExtraction.status.in_(["rejected", "auto_created"]),
|
||||
KnowledgeExtraction.created_at < cutoff,
|
||||
)
|
||||
)
|
||||
total_deleted += result.rowcount
|
||||
await db.commit()
|
||||
|
||||
if total_deleted:
|
||||
logger.info("Knowledge retention: cleaned up %d old extractions", total_deleted)
|
||||
except Exception:
|
||||
logger.error("Knowledge retention cleanup failed", exc_info=True)
|
||||
await db.rollback()
|
||||
|
||||
|
||||
register_job("cleanup_knowledge", cleanup_knowledge_job)
|
||||
# Note: knowledge retention cleanup ("cleanup_knowledge") lives with the
|
||||
# knowledge plugin (app/plugins/builtins/knowledge/jobs.py) and is discovered
|
||||
# via the plugin job-module mechanism — no core→plugin import.
|
||||
|
||||
|
||||
class WorkerSettings:
|
||||
@@ -531,9 +489,10 @@ class WorkerSettings:
|
||||
_wrap_cron_with_lock("cleanup_trash", cleanup_trash_job, ttl_seconds=300),
|
||||
hour=4, minute=0,
|
||||
),
|
||||
# Knowledge retention cleanup — daily at 05:00 (90 days, keeps approved)
|
||||
# Knowledge retention cleanup — daily at 05:00 (90 days, keeps approved).
|
||||
# Function comes from the knowledge plugin via the job registry.
|
||||
cron(
|
||||
_wrap_cron_with_lock("cleanup_knowledge", cleanup_knowledge_job, ttl_seconds=300),
|
||||
_wrap_cron_with_lock("cleanup_knowledge", get_job("cleanup_knowledge"), ttl_seconds=300),
|
||||
hour=5, minute=0,
|
||||
),
|
||||
# Scheduled backup — daily at 02:00 (guarded by distributed lock)
|
||||
|
||||
Reference in New Issue
Block a user