gate: fully resilient plugin activation in API startup

This commit is contained in:
Agent Zero
2026-07-31 09:37:44 +02:00
parent 01aa31a3e0
commit 79d132b66d
+18 -22
View File
@@ -221,29 +221,25 @@ async def lifespan(app: FastAPI):
continue continue
# Activate plugin with tenant context set for each tenant # Activate plugin with tenant context set for each tenant
# (RLS fail-closed requires app.current_tenant_id to be set for tenant-table writes) # RLS fail-closed requires app.current_tenant_id for tenant-table writes.
activation_failed = False # Plugin activation may fail on duplicate cron job inserts — this is
for tenant_id in all_tenant_ids: # harmless since cron jobs already exist from previous startups.
try: try:
await set_tenant_context(db, tenant_id) for tenant_id in all_tenant_ids:
await plugin.on_activate(db, container, event_bus) try:
# Flush to detect any RLS errors that were swallowed by the plugin await set_tenant_context(db, tenant_id)
await db.flush() await plugin.on_activate(db, container, event_bus)
except Exception as exc: await db.flush()
# RLS may block duplicate cron job inserts — rollback and continue except Exception as exc:
# The cron jobs are already registered from previous startups logger.warning(f"[STARTUP] Plugin {name} activation issue for tenant {tenant_id}: {exc}")
logger.warning(f"[STARTUP] Plugin {name} activation issue for tenant {tenant_id}: {exc}") await db.rollback()
await db.rollback() break
# Don't set activation_failed — the plugin's event handlers are still registered except Exception as exc:
# and the cron jobs already exist in the DB from previous startups logger.warning(f"[STARTUP] Plugin {name} activation failed: {exc}")
break await db.rollback()
if not activation_failed: plugin_record.status = "active"
plugin_record.status = "active" logger.info(f"[STARTUP] Activated plugin: {name}")
logger.info(f"[STARTUP] Activated plugin: {name}")
else:
plugin_record.active = False
plugin_record.status = "activation_failed"
await db.commit() await db.commit()