Repository navigation
chore(jobs): run no job crons on a PR preview, so an idle one can sleep - #1178
Conversation
Every autoRun tick queries the database, even when nothing is due. Railway sleeps a service only after 5 minutes without outbound traffic, so with ticks at most 4 minutes apart no preview's app or Postgres ever slept. Previews were about $39 of the $61 Railway bill for 16 Oct - 16 Nov 2026. `jobAutoRun()` returns `JOB_AUTO_RUN` everywhere except a Railway PR preview, which gets none (undefined, so loginPlugin appends no invitations tick either). A queue a write kicks through runQueueSoon still runs on a preview. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🚅 Deployed to the SahajCloud-pr-1178 environment in sahajcloud
1 service not affected by this PR
|
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
|
Red check is the preview seed, not the tests: 109 int files, 1975 tests passed.
The app never restarted — So the diff is not what is red. CI's seed assumes a preview whose database answers at once, 13 minutes after the deploy. Two ways out, both yours:
I could not confirm the sleep in Railway's logs: the loop's token answers Pushed nothing. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
…1185) * feat(preview): retry a connection while a preview's Postgres wakes A Railway service asleep under Serverless wakes on the first private-network connection to it, and that connection is refused. `@payloadcms/db-postgres` does not retry, so the request behind it failed — and `request-magic-link` answers every failure as a 200 with no token, which is how CI's fixture fill broke when a preview's Postgres was allowed to sleep (#1178). On a PR preview the adapter's own `pg` is wrapped so its pool retries a connection for up to 30 s, 1 s apart, on the network errors a waking service answers with and on 57P03. Both connect forms retry, since the pool's `query()` — what Drizzle calls — connects through the callback form. Production's adapter is untouched: its Postgres never sleeps. This is the prerequisite for letting a preview's Postgres sleep; the setting itself comes with the preview-sleep workflow. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(preview): retry the wake through p-retry The wrapper's own loop, its deadline arithmetic and the exported WAKE_BUDGET_MS reimplemented p-retry, which this repo already depends on and calls in src/lib/mapbox/geocoder.ts and src/jobs/SyncLectureMetadata. p-retry's maxRetryTime is the budget, shouldRetry is isPostgresWaking, and factor: 1 with minTimeout is the flat interval. Two of its edges are load-bearing and now carried by comments: the budget, not a count, is what ends the retrying, so retries must be Infinity; and onFailedAttempt runs on an error nothing retries, so the one-time log repeats isPostgresWaking rather than assuming a retry followed. maxRetryTime exhaustion rethrows the original error, so a caller still sees the last refusal. WAKE_BUDGET_MS had no importer, so it is module-private now. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYSqZCw6hTAAaKMa6U * fix(preview): drop the pointer to an unlanded script The ⚠ asked a reader to keep `scripts/preview-sleep.ts` in sync with this folder, and no commit on this branch adds that file, so nobody could act on it. The coupling it names is real but belongs beside the script that turns sleep on, in the pull request that lands it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYSqZCw6hTAAaKMa6U * test(preview): check the adapter's own connect seam The wrapper rests on the adapter reading `db.pg` after `init` returns, and nothing stated or checked it: the existing case asserted against a fake adapter, and no sleeping Postgres had been observed. `@payloadcms/[email protected]`'s compiled `dist/connect.js` builds the pool inside `connect()`, as `new this.pg.Pool(this.poolOptions)`, while `dist/index.js` leaves `pool: undefined` at init — so `init` is the seam, and the docblock now says so. The new case drives the real adapter at a refused port, which is what a sleeping Postgres looks like to a first connection. Watched fail with the wrap removed: the adapter gave up in 10 ms and reported no wake. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYSqZCw6hTAAaKMa6U * docs(tests): map the postgres-wake plugin in COVERAGE.md The plugin and its spec landed with no row, and that section already tracks a plugin's own wiring (`sentryPlugin`), so the map had a hole where a reader looks for one. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYSqZCw6hTAAaKMa6U --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Why
PR previews were about $39 of the $61 Railway bill for 16 Oct – 16 Nov 2026. 124 PRs had a preview in that period, for 3,889 preview-hours — about five previews running at all times, each holding ~0.48 GB (app) plus ~0.23 GB (its own Postgres) while doing nothing (~0.003 vCPU).
Railway can sleep an idle service (Serverless), but only after 5 minutes with no outbound traffic. An hour of network flow logs from the PR 1165 preview showed the app querying Postgres at exactly the
autoRunminutes, never more than 4 minutes apart. So no preview could ever sleep, whatever the setting.What changes
jobAutoRun()insrc/jobs/queues.tsreturnsJOB_AUTO_RUNeverywhere except a Railway PR preview (isPreviewEnvironmentName, the same allowlist every other preview decision uses), which gets none. It returnsundefinedrather than[], sologinPluginappends noinvitationstick either.payload.config.tspassesjobAutoRun()tojobs.autoRun.runQueueSoon(screening, class import, transcription) still runs. Schedules never fire and a lost kick is not retried. To exercise a scheduled job on a preview, sign in and open/api/payload-jobs/run?queue=<queue>. This is documented insrc/jobs/AGENTS.md.DEPLOYMENT.md→ Cost Monitoring: the CLI commands that read the bill, the new $40 soft limit, and the preview cost note.Not in this PR (Railway settings, no code)
Removing the crons only makes sleep possible. Sleep itself is a Railway service setting (Serverless), and the preview's Postgres also uploads to the point-in-time-recovery bucket every minute, which it inherits from production. Both are switched on/off on this PR's preview only to check that it sleeps and wakes. Making that the default for every preview (a dedicated base environment for PRs) is a follow-up once that test passes.
Sleep test on this PR's preview (16–17 Nov)
Railway settings on
SahajCloud-pr-1178only. Production was not touched; its PITR and sleep settings were re-read after each change.cannot connect to Postgres), and the request after it succeeded.request-magic-linkswallows that error into its uniform200 {"ok":true}, sopnpm seed preview-fixturefailed twice that way. Letting Postgres sleep needs a connect retry first, so it is kept awake on this preview now.serviceInstanceDeployV2). Railway's "Redeploy" reuses the previous deployment's settings snapshot, so the first app redeploy here never slept.Verification
tests/unit/job-schedules.spec.ts: newjobAutoRuncases (preview → none; production, an unrecognised name, and off-Railway → every entry), and the config-wiring case now readsautoRun: jobAutoRun().🤖 Generated with Claude Code