Skip to content

chore(jobs): run no job crons on a PR preview, so an idle one can sleep - #1178

Merged
Ardnived merged 1 commit into
mainfrom
claude/chore-preview-idle-jobs
Nov 17, 2026
Merged

Ardnived merged 1 commit into
mainfrom
claude/chore-preview-idle-jobs

Conversation

@Ardnived

@Ardnived Ardnived commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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 autoRun minutes, never more than 4 minutes apart. So no preview could ever sleep, whatever the setting.

What changes

  • jobAutoRun() in src/jobs/queues.ts returns JOB_AUTO_RUN everywhere except a Railway PR preview (isPreviewEnvironmentName, the same allowlist every other preview decision uses), which gets none. It returns undefined rather than [], so loginPlugin appends no invitations tick either.
  • payload.config.ts passes jobAutoRun() to jobs.autoRun.
  • On a preview, a queue kicked through 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 in src/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-1178 only. Production was not touched; its PITR and sleep settings were re-read after each change.

  • App asleep, Postgres awake: works. The app slept about 9 minutes after its last request. The next request woke it in 2.5 s with a 200 (no 502), and the CI rerun then ran discovery, fixture fill and smoke against the woken app — all green.
  • Postgres asleep: breaks the first DB request. Postgres slept 6–8 minutes after its last connection. The next query was refused while it woke (cannot connect to Postgres), and the request after it succeeded. request-magic-link swallows that error into its uniform 200 {"ok":true}, so pnpm seed preview-fixture failed twice that way. Letting Postgres sleep needs a connect retry first, so it is kept awake on this preview now.
  • PITR on a preview's Postgres uploads every minute and would keep it awake. It is off on this preview, but it matters only if Postgres is to sleep.
  • ⚠ A setting change needs a fresh deploy (serviceInstanceDeployV2). Railway's "Redeploy" reuses the previous deployment's settings snapshot, so the first app redeploy here never slept.

Verification

  • Tier 2 lean gate: lint, format, typecheck, test typecheck, unit lane (283 files) — passed.
  • tests/unit/job-schedules.spec.ts: new jobAutoRun cases (preview → none; production, an unrecognised name, and off-Railway → every entry), and the config-wiring case now reads autoRun: jobAutoRun().

🤖 Generated with Claude Code

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]>
@railway-app

railway-app Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-1178 environment in sahajcloud

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Nov 17, 2026 at 12:28 am UTC
1 service not affected by this PR
  • mailpit

@sydevs-bot

sydevs-bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

🤖 no session running
CI fix 1
CI re-run 1 on dccd146 — infrastructure, not the change

recent dispatches
  • 00:29Z review-pr · attempt 1 · session · done
  • 00:29Z review-pr · attempt 1 · session
  • 23:22Z fix-ci · attempt 1 · session · done
  • 23:22Z fix-ci · attempt 1 · session

Written by the sydevs dispatcher. Sessions never edit this comment.

@sydevs-bot sydevs-bot moved this from Awaiting to Pending in SYDevelopers Technical Roadmap Nov 16, 2026
@sydevs-bot sydevs-bot moved this from Pending to Awaiting in SYDevelopers Technical Roadmap Nov 16, 2026
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 16, 2026
@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 16, 2026
@sydevs-bot

Copy link
Copy Markdown
Contributor

Red check is the preview seed, not the tests: 109 int files, 1975 tests passed.

pnpm seed preview-fixture got {"ok":true} and no token from /api/managers/request-magic-link. Sentry holds one event from this preview, release dccd146, at 23:21:55: cannot connect to Postgres, unhandled, on GET /api/managers/me. So issueMagicLink's read threw, requestMagicLink answered its uniform 200, and the seed read that as "signs nobody in".

The app never restarted — /api/health reports 1011s uptime, from the 23:10 deploy. Its Postgres had been idle since then, which is what this PR makes possible and what Serverless on this preview then acts on.

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:

  • keep the preview's Postgres awake while the PR is open, or
  • let CI wake it, by retrying the sign-in in seeds/run.ts.

I could not confirm the sleep in Railway's logs: the loop's token answers Not Authorized for deploymentLogs.

Pushed nothing.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 16, 2026
@sydevs-bot sydevs-bot moved this from Working to Awaiting in SYDevelopers Technical Roadmap Nov 16, 2026
@sydevs-bot sydevs-bot added the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 17, 2026
@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 17, 2026
@Ardnived
Ardnived merged commit 05f5de3 into main Nov 17, 2026
18 of 21 checks passed
@Ardnived
Ardnived deleted the claude/chore-preview-idle-jobs branch November 17, 2026 00:33
@sydevs-bot sydevs-bot moved this from Working to Done in SYDevelopers Technical Roadmap Nov 17, 2026
@sydevs-bot sydevs-bot removed the bot:working Lock: a cloud session is running on this item. Removed when it ends. label Nov 17, 2026
Ardnived added a commit that referenced this pull request Nov 17, 2026
…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]>

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-1178 — dccd1460 Deployed Nov 16, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants