4a25ac1379
Check Cross-Plugin Imports / check (push) Has been cancelled
Verifikation: Alle 17 Audit-Findings gegen den Code geprueft — alle bestaetigt. Backend-Lifecycle-Fixes umgesetzt; 4 Frontend-Plugin-Architektur-Punkte als Phase Q in die Roadmap eingeplant. - P1 list_workspaces: Module + User-Counts gebuendelt laden (Editor-Overwrite-Bug) - P1 active-manifests: Tenant-Deaktivierung (tenant_plugin_activation) filtern - P1 uninstall: volle Service-Deactivation VOR registry.uninstall() - P1 ContractRegistry: DB-Aktivstatus-Guard (Restart-Edge-Case) + Re-Activate - P1/P2 Field-Definitions: voller Lifecycle (register/unregister) im Service - P1/P2 Contact-Felddefinitionen (39) ins ContactsPlugin-Manifest verschoben - P1 12 fehlende Permission-Keys registriert (AST-Scan: 0 fehlend) - P2 contact_folder -> ContactsPlugin; ENTITY_PLUGIN_OWNERS wird befuellt - P2 Entity-Permission-Fallback fail-closed statt contacts:read - P2 forgejo_error_reporter is_core=False; DMS is_core=True (ADR-020) - P2 Worker: Contacts-Trash-Cleanup ins Plugin (get_job_modules-Discovery) - P1/P2 DSGVO-Export delegiert an DSAR-Collector (kein Core->Contacts) - P2 False-green Tests korrigiert (or True, veraltete Route-Count-Assertion) Verifikation: tests/test_audit_architecture_fixes.py 17/17; Regressionen gruen (contacts_lifecycle, entity_registry, workspace_scopes, rbac, lifecycle_service); Combo-Order-Test 35/35; Cross-Plugin-Checker 497/0; compileall sauber; ruff auf 7-Error-Baseline. Doku: PROGRESS.md Audit-Section, PLATFORM_ROADMAP.md Phase Q (Q1-Q4), plugin-development-guide.md Lifecycle, permissions.md Katalog.
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
"""ARQ background jobs for the contacts plugin.
|
|
|
|
Registered via ``register_job()`` at import time; the worker discovers this
|
|
module through ``ContactsPlugin.get_job_modules()`` — the core worker must
|
|
not import contact models directly (audit P2: hidden core->contacts
|
|
coupling in the trash cleanup).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Any
|
|
|
|
from sqlalchemy import delete as sa_delete
|
|
from sqlalchemy import text as sa_text
|
|
|
|
from app.core.job_registry import register_job
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_TRASH_RETENTION_DAYS = 90
|
|
|
|
|
|
async def cleanup_contacts_trash_job(ctx: dict[str, Any]) -> None:
|
|
"""Permanently delete soft-deleted contacts older than the retention window.
|
|
|
|
Runs daily. Iterates per-tenant for RLS compliance.
|
|
Moved from app.core.worker.cleanup_trash_job (audit P2) so the core
|
|
worker only handles core-owned entities (entity_attachments).
|
|
"""
|
|
from app.core.db import get_worker_session_factory
|
|
from app.models.contact import Contact
|
|
|
|
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.now(UTC) - timedelta(days=_TRASH_RETENTION_DAYS)
|
|
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)},
|
|
)
|
|
result = await db.execute(
|
|
sa_delete(Contact).where(
|
|
Contact.deleted_at.is_not(None),
|
|
Contact.deleted_at < cutoff,
|
|
)
|
|
)
|
|
total_deleted += result.rowcount
|
|
await db.commit()
|
|
|
|
if total_deleted:
|
|
logger.info("Contacts trash cleanup: permanently deleted %d old contacts", total_deleted)
|
|
except Exception:
|
|
logger.error("Contacts trash cleanup failed", exc_info=True)
|
|
await db.rollback()
|
|
|
|
|
|
register_job("cleanup_contacts_trash", cleanup_contacts_trash_job)
|