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
# Activate plugin with tenant context set for each tenant
# (RLS fail-closed requires app.current_tenant_id to be set for tenant-table writes)
activation_failed = False
for tenant_id in all_tenant_ids:
try:
await set_tenant_context(db, tenant_id)
await plugin.on_activate(db, container, event_bus)
# Flush to detect any RLS errors that were swallowed by the plugin
await db.flush()
except Exception as exc:
# RLS may block duplicate cron job inserts — rollback and continue
# The cron jobs are already registered from previous startups
logger.warning(f"[STARTUP] Plugin {name} activation issue for tenant {tenant_id}: {exc}")
await db.rollback()
# Don't set activation_failed — the plugin's event handlers are still registered
# and the cron jobs already exist in the DB from previous startups
break
# RLS fail-closed requires app.current_tenant_id for tenant-table writes.
# Plugin activation may fail on duplicate cron job inserts — this is
# harmless since cron jobs already exist from previous startups.
try:
for tenant_id in all_tenant_ids:
try:
await set_tenant_context(db, tenant_id)
await plugin.on_activate(db, container, event_bus)
await db.flush()
except Exception as exc:
logger.warning(f"[STARTUP] Plugin {name} activation issue for tenant {tenant_id}: {exc}")
await db.rollback()
break
except Exception as exc:
logger.warning(f"[STARTUP] Plugin {name} activation failed: {exc}")
await db.rollback()
if not activation_failed:
plugin_record.status = "active"
logger.info(f"[STARTUP] Activated plugin: {name}")
else:
plugin_record.active = False
plugin_record.status = "activation_failed"
plugin_record.status = "active"
logger.info(f"[STARTUP] Activated plugin: {name}")
await db.commit()