Repository navigation
feat: let a scheduler start the audit event purge instead of the server's timer - #2397
Conversation
…er's timer The hosted cloud's purge never ran: a Vercel function runs only while it serves a request, so the in-process loop's first purge started at each cold start, outside the request that started the instance, and failed while connecting to PgBouncer. - events.retention.trigger_secret: set, the server keeps no timer and purges once per GET or POST to /cron/events-retention that carries the secret as a bearer token. Unset, the default, a server purges hourly in process as before. Purges in one process wait for each other. - The cloud: a Vercel Cron Job in vercel.json calls the path hourly, and the launcher turns CRON_SECRET into the trigger secret; a deployment's build fails without it. - ADR 049 amended: who starts the purge, and that it runs as the server's role rather than a dedicated one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 6ceeb8e The changes in this PR will be included in the next version bump. This PR includes changesets to release 24 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Revert wording the package does not need, so the change stays clear of the open PRs that edit the same files: the README's build.sh row (beside the row #2397 changes), two runbook phrasings, the root turbo.json header and the turborepo.md bullet on ignored files in input globs, which the rework of #2420 covers. The cloud task's own comment keeps why it excludes .vercel/ and the other ignored trees. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o task The server service's build command now installs (the Go preset runs no installCommand) and runs `turbo run deploy --filter=@zitadel/cloud`. @zitadel/cloud's new `deploy` task depends on `build` (the launcher, from the cache when nothing it compiles changed) and runs deploy.sh, the former build.sh without its install and turbo call: copy the binary to $VERCEL_OUTPUT_FILE, then migrate. It is never cached, and it passes the whole environment through (`passThroughEnv: ["*"]`): the launcher checks every variable the function needs at build time, so a variable strict mode hid (CRON_SECRET, which #2397 adds, or any later one) would fail every deployment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…chimedes-f4763f Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The runbook's Errors bullet (#2396) gave a failed event retention run as an error outside any request. On the cloud the purge now runs in its cron request and a failed run answers 500, so the example is the request audit event flush instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
🟡 Changes recommended
An unresolved active finding remains, flagging that the runbook's Preview CRON_SECRET row omits the build requirement and could mislead operators into skipping a secret needed for the preview build.
1 open issue
What changed in this PR
The hosted cloud runs as a Vercel function that only executes while serving a request, so the server's in-process hourly RetentionJob timer never reliably runs the audit-event purge (production logged only purge failed, no successful runs). This PR lets an external scheduler own the purge schedule without changing self-hosted behavior: a new events.retention.trigger_secret setting switches the server from its in-process timer to an HTTP trigger at /cron/events-retention (bearer-token authenticated), which Vercel Cron calls hourly. When the secret is unset (the default), the hourly in-process purge is unchanged.
Changes:
- Server: adds
TriggerSecretconfig with validation (≥16 printable ASCII chars), aTrigger()HTTP handler (GET/POST + constant-time bearer check, 401/405/500, write-deadline extension to outlastWriteTimeout, per-process serialization via apurgingtoken), andstartJobSpannow takes a ctx withWithNewRoot(); the trigger is mounted outside the API with a minimal middleware chain. - Cloud:
vercel.jsoncron job, launcher mapsCRON_SECRETinto the config file and removes it from the environment, failing deployed builds without it. - Docs/changeset: ADR 049 dated amendment, runbook + quick-start config sections, and a
@zitadel/serverminor changeset.
| File | Description |
|---|---|
internal/audit/retention.go |
Core: TriggerSecret config, Validate/CheckTriggerSecret, serialized deleteExpired, bearer-authenticated Trigger() handler |
internal/audit/tracing.go |
startJobSpan takes ctx + WithNewRoot() so a request-started run is still a job root |
internal/audit/shipper.go |
Pass context.Background() to the new startJobSpan signature |
internal/audit/retention_test.go / tracing_test.go |
Thorough tests: validation, auth, method, 500, serialization, timeouts, tracing |
cmd/server/server.go |
Wire retentionJob.Trigger() into buildHTTPMux, default + mount the trigger path |
cmd/server/config.go |
Add Events.Retention.Validate to startup validation |
cmd/server/*_test.go |
Update buildHTTPMux callers; config-load and mount tests |
apps/cloud/launcher/main.go |
Read/validate CRON_SECRET, render trigger_secret into config, unset env |
apps/cloud/launcher/main_test.go |
Launcher + vercel.json cron routing tests |
vercel.json |
Add hourly cron on /cron/events-retention |
docs/*, .changeset/*, apps/cloud/README.md |
ADR amendment, runbook/config docs, changeset |
I raised one minor documentation comment: the Preview CRON_SECRET runbook row omits that the build also fails without it, while the launcher enforces it identically for production and preview deployments. The implementation itself is correct and well-tested — the middleware chain preserves the write-deadline extension through Unwrap(), the bearer check is constant-time and length-independent, per-process purge serialization is handled, and all buildHTTPMux/startJobSpan call sites are updated.
One item is deferred to a human: whether Vercel Cron actually reaches the endpoint past Deployment Protection can only be confirmed by the first production run after merge (the author flagged this uncertainty).
Review effort: Balanced
The launcher refuses every deployment without it, preview included, so the runbook's Preview row no longer reads as if the secret were optional. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary
The hosted cloud's audit event purge has not been running. In the 48 hours to 2026-10-27 14:31 UTC, production logged 14
event retention: purge failedlines and nopurged eventsline. Every failure is a connection failure to PlanetScale's PgBouncer:tls error: EOF,unexpected EOF,client_login_timeoutor a deadline. Each was logged during whichever request had cold-started the instance.RetentionJobstarts a purge as the server starts, from a goroutine outside any request, and a Vercel function runs only while it serves one.This PR lets something outside the process own the schedule, without changing a self-hosted server:
events.retention.trigger_secret. When it is set, the server runs no purge loop. Instead, eachGETorPOSTto/cron/events-retentionthat carries the secret as a bearer token runs one purge and answers{"deleted": n}. Any other request to the path gets 401, and a failed purge gets 500. When it is unset (the default), the server purges hourly in process as before. The path is mounted outside the API: it is not in the OpenAPI document, and its request goes through only the request-ID, request-log and recover middleware.DeleteEventsOlderThanthe loop calls, so it is idempotent. A run gets 4½ minutes, under Vercel's 300 s default, and extends its own write deadline past the server's 30 sWriteTimeout. Two runs in one process wait for each other; across instances, Postgres's row locks keep an overlap safe. Every triggered run logs a line, including one that deletes nothing.vercel.jsongets a Cron Job that calls the path at minute 17 of every hour. The launcher writesCRON_SECRETintonextgen.yamlas the trigger secret and takes it out of the environment. A production or preview build fails without it, so a deployment that would never purge cannot replace the one serving. Vercel's docs pair top-levelcronswithservices(their Render migration guide), andvercel.jsonvalidates against Vercel's published schema.DELETEonevents. The production failures connect aspscale_api_…, the role behindCLOUD_DATABASE_URL. Per the runbook, that role inheritspg_write_all_data, so it may delete from every table. No deployment has a dedicated role: the server opens one pool. ADR 049 gets a dated amendment that records both decisions: who starts the purge, and that it runs as the server's role until a deployment needs the split.docs/quick-start/configuration.md, and the cloud README's launcher row.Validation
go test -race -count=1 ./internal/audit/ ./cmd/server/ ./apps/cloud/launcher/: pass.go run ./tools/analyzers/cmd/analyzerson those packages, plain and withpostgres_integrationandsqlite_integration: pass.gofmt -lis clean;go build ./...passes.internal/audit): 401, 405, 500 and the response shape. Run against a realhttp.Serverwith 50 ms timeouts, it outlasts the write timeout; dropping the deadline extension fails with EOF. Purges in one process run one after the other; dropping that guard fails. A triggered run is its own job root even under an incoming trace context; droppingWithNewRootfails.cmd/server: the mux mounts the path only when given a trigger, with the proxy's request ID. Config loads the key from the file and from the environment and refuses a short secret.CRON_SECRET, and refuses a deployment without it, in serving and at build time. Avercel.jsontest pins the cron's path to the server's constant and checks that no redirect or earlier rewrite captures it. A wrong path fails it, and so does a capturing rewrite.vercel.jsonchecked withajv-cliagainsthttps://openapi.vercel.sh/vercel.json: valid.cloud-qg447hb01-zitadel.vercel.app, commitef362837a):401withWWW-Authenticate: Bearer.200 {"deleted":0}in 6 ms, and oneevent retention: purged eventsline under the request's ID.purge failed. The count is 0 because this preview's database was created by its own build.main(2026-10-28, 47 commits, among them the turbo-built cloud, build(deploy): build every cloud service through one turbo graph #2425): the same Go tests,gofmt, the analyzers and the schema check pass. Themigratetask passes the whole environment through, so the build still seesCRON_SECRET.Release notes / changeset
.changeset/events-retention-trigger.md:@zitadel/serverminor. A server that runs only while serving a request can leave the purge to an outside scheduler throughevents.retention.trigger_secret; without it, the hourly purge is unchanged.Notes
CRON_SECRETis set (2026-10-27) as a sensitive variable for Production and, with a different value, for Preview (all branches). Both values are owner-only in~/.zitadel/cloud/<env>/cron-secreton the machine that minted them, and still need to go into 1Password.all_except_custom_domains). Vercel calls a cron on the production deployment URL, and its docs don't say whether those calls pass Vercel Authentication. Check the first run after merge under Settings → Cron Jobs → View Logs. A 401 from Vercel's login page, rather than the server's, means the run is blocked.DELETEwith no index leading oncreated_at. If a run times out, the runbook gives the batchedDELETEto clear the backlog by hand.events_pkeyon retry) and the master key migration (client_login_timeout) also run outside a request. This PR doesn't fix them; the fix isn't shared.🤖 Generated with Claude Code