gate: worker resilient to RLS errors during plugin activation, use crm_worker engine

This commit is contained in:
Agent Zero
2026-07-31 09:12:42 +02:00
parent 5ce85f4324
commit ec0cf6f588
+8 -12
View File
@@ -111,11 +111,13 @@ async def on_startup(ctx: dict[str, Any]) -> None:
from sqlalchemy.ext.asyncio import async_sessionmaker
registry = get_registry()
registry.initialize(get_engine(), app=None)
from app.core.db import get_worker_engine
worker_engine = get_worker_engine()
registry.initialize(worker_engine, app=None)
registry.discover_builtins()
event_bus = get_event_bus()
async_session = async_sessionmaker(get_engine(), expire_on_commit=False)
async_session = async_sessionmaker(worker_engine, expire_on_commit=False)
# Activate plugins that are marked active in DB (register event handlers)
# RLS fail-closed requires tenant context for tenant-table writes
@@ -146,17 +148,11 @@ async def on_startup(ctx: dict[str, Any]) -> None:
await set_tenant_context(db, tenant_id)
await plugin.on_activate(db, container, event_bus)
except Exception as exc:
logger.error(f"Worker: failed to activate plugin {name} for tenant {tenant_id}: {exc}")
# RLS may block duplicate cron job inserts — rollback and continue
# The cron jobs are already registered from the API container startup
logger.warning(f"Worker: plugin {name} activation failed for tenant {tenant_id}: {exc}")
await db.rollback()
activation_failed = True
try:
from app.plugins.builtins.forgejo_error_reporter.service import report_error_to_forgejo
await report_error_to_forgejo({
"message": f"[Worker] Plugin activation failed: {name}: {exc}",
"stack": traceback.format_exc(),
"context": {"plugin": name, "source": "worker_startup"},
})
except Exception:
pass
break
if not activation_failed:
logger.info(f"Worker: activated plugin {name}")