Skip to content

feat(preview): let each PR preview sleep when idle, after CI - #1186

Merged
Ardnived merged 1 commit into
mainfrom
claude/feat-preview-sleep
Nov 17, 2026
Merged

Ardnived merged 1 commit into
mainfrom
claude/feat-preview-sleep

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Why

PR previews were about $39 of last month's $61 Railway bill: ~5 previews running around the clock, each ~0.5 GB app plus ~0.25 GB Postgres. #1178 removed the job crons that kept an idle preview awake. What remains is turning Railway's Serverless (sleep) on for each preview.

Railway has no surviving way to give PR environments their own settings. A PR environment copies production's settings once, when it is created, and production must never sleep. Config as Code's environments.pr override did exactly this, but Railway stops reading Config as Code on 2026-12-01. Its replacement, .railway/railway.ts, has no sleep setting, cannot target PR environments, and is never read on deploy. So this changes each preview's own copy after the fact.

What

  • .github/workflows/preview-sleep.yml:
    • When it runs: on workflow_run each time CI completes for a PR (skipping cancelled runs), plus a manual dispatch for one PR or all.
    • How it runs: it installs a pinned Railway CLI (5.71.0) and runs the script with --apply.
    • ⚠ Why workflow_run: RAILWAY_API_TOKEN is a workspace token that can also change production. workflow_run runs this file and the script from main and checks nothing out from the PR, so no branch can change what runs with the token.
  • scripts/preview-sleep.ts, with its decision half in scripts/preview-sleep-plan.ts:
    • App: it turns on Serverless.
    • Postgres: it turns on Serverless and turns off point-in-time recovery, which uploads every minute. It does this only once the commit the app runs carries the wake retry from feat(preview): retry a connection while a preview's Postgres wakes #1185. Otherwise the first request after a wake would fail.
    • Fresh deploys: each service whose running deployment lacks the setting gets a fresh deploy (serviceInstanceDeployV2), never a Redeploy, which reuses the old settings snapshot. The app redeploys the commit it already runs, never the PR head, so the script cannot roll a preview back.
    • Skips: an app that is still deploying is left to the CI run that follows. A failed one has nothing to sleep.
    • Running twice: a second run changes nothing.
    • Default: a dry run unless --apply (or --force) is passed.
    • ⚠ Production guard: only an ephemeral SahajCloud-pr-<n> environment forked from production is touched. Railway's settings-update call, given anything other than such a fork, applies to every non-fork environment, production included. Production's sleep flags are re-read before and after every write, and the run fails loudly if either is on.
  • Docs:
    • DEPLOYMENT.md: Serverless as the one setting only previews get, a row in the preview table, and the token: what it can do, why it is a workspace token, and how to rotate it.
    • scripts/AGENTS.md: rows for both scripts.

Verification

  • Tier 2 lean gate: passed (lint, format, typecheck, test typecheck, unit lane).
  • tests/unit/preview-sleep-plan.spec.ts:
    • a fresh preview gets every change, Postgres first;
    • Postgres stays awake behind an app with no retry;
    • a preview already sleeping gets no changes;
    • a setting that is on but not yet in the running snapshot still gets a fresh deploy;
    • turning point-in-time recovery off alone still redeploys Postgres;
    • a building, deploying, failed or crashed app is skipped, and so is a missing one.
  • Live run on feat(preview): retry a connection while a preview's Postgres wakes #1185's preview, once its CI had passed (17 Nov):
    • The first --apply made all five changes. Afterwards production was unchanged (sleep off on both services, point-in-time recovery on). A second dry run reported already sleeps when idle.
    • Sleep: the app slept 8 minutes after its fresh deploy, and Postgres slept 18 minutes after its own.
    • Wake: with both asleep, /api/health answered 200 in 3.3 s. The fixture reported loaded 4 s later. The app log shows the refused connection, AggregateError (Node's dual-stack refusal), followed by Postgres refused a connection; retrying while it wakes and [fixture] loaded … 211 unchanged.
    • Not yet run: the workflow file itself. workflow_run and dispatch only work once the file is on main.

After merge

  • The workflow first runs on the next CI completion. To bring existing previews in, dispatch it once with all: gh workflow run preview-sleep.yml -f pr=all.

🤖 Generated with Claude Code

A PR environment copies production's settings once, when Railway
creates it, and production must never sleep. Config as Code's
`environments.pr` block was the way to give previews their own settings,
and Railway stops reading it on 2026-12-01, so each preview's own copy
is changed after the fact.

`preview-sleep.yml` runs on `workflow_run` each time CI finishes with a
PR — from main, checking nothing out from the PR, because
RAILWAY_API_TOKEN is a workspace token that can change production — and
runs `scripts/preview-sleep.ts --pr <n> --apply`. It also takes a manual
dispatch for one PR or `all`.

The script turns on Serverless for the preview's app and, once the
commit the app runs carries the Postgres wake retry, for its Postgres
too, with point-in-time recovery off (it uploads every minute). Each
service whose running deployment lacks the setting is deployed fresh —
a redeploy reuses the old settings snapshot. The app redeploys the
commit it already runs, and an app still deploying is left to the CI
run that follows. Every check on the target environment keeps production
out, and production's settings are re-read before and after each write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@railway-app

railway-app Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

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

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

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-1186 November 17, 2026 18:30 Destroyed
@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 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🤖 no session running

recent dispatches
  • 18:44Z review-pr · attempt 1 · session · done
  • 18:44Z review-pr · attempt 1 · session

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

@sydevs-bot sydevs-bot moved this from Awaiting to Working in SYDevelopers Technical Roadmap Nov 17, 2026
@Ardnived
Ardnived merged commit c70081c into main Nov 17, 2026
9 checks passed
@Ardnived
Ardnived deleted the claude/feat-preview-sleep branch November 17, 2026 18:51
@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

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-1186 — 67ebeba5 Deployed Nov 17, 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