Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,27 @@ HEALTH_PORT=3005 # Health check port — read by FIVE services (wor
# (discord 3001, slack 3002, teams 3003, worker 3005).
# Running several locally needs a per-process override.

# OUTPOST_ALLOW_SCHEMA_DRIFT=1 # Incident release valve for apps/worker/start.sh.
# The worker refuses to boot when the live database does
# not match schema.prisma, or when that check cannot run
# at all. Setting this (1/true/yes/on) starts it anyway,
# with warnings. Use it for transient skew while a sibling
# service is mid-rollout, or to get the worker up during
# OUTPOST_ALLOW_SCHEMA_DRIFT=1 # Incident release valve for the schema-drift guard
# that apps/web/start.sh and apps/worker/start.sh share
# (packages/outpost/db/schema-drift-guard.sh). Both
# refuse to boot when the live database does not match
# schema.prisma, or when that check cannot run at all.
# Setting this (1/true/yes/on) starts them anyway, with
# warnings. Use it for transient skew while a sibling
# service is mid-rollout, or to get a service up during
# an incident; unset it once the schema is repaired.
# TRACKER_SYNC may write wrong statuses to Linear while
# this is set.

# OUTPOST_STARTUP_STEP_TIMEOUT=180 # Seconds the worker's schema-drift check may run
# before it is abandoned (apps/worker/start.sh). Bounds
# only the read-only check; `migrate deploy` is left
# unbounded on purpose, since killing it mid-migration
# creates the P3009 failed-migration state the guard
# exists to report. Keep it below the platform's
# healthcheck timeout, or a slow check is killed as an
# unexplained deploy failure instead of logging a reason.
# TRACKER_SYNC may write wrong statuses to Linear, and
# the web app keeps serving pages, while this is set.

# OUTPOST_STARTUP_STEP_TIMEOUT=180 # Seconds a schema-drift check may run before it
# is abandoned (apps/web/start.sh, apps/worker/start.sh).
# Bounds only the read-only check; `migrate deploy` is
# left unbounded on purpose, since killing it
# mid-migration creates the P3009 failed-migration state
# the guard exists to report. Keep it below the
# platform's healthcheck timeout, or a slow check is
# killed as an unexplained deploy failure instead of
# logging a reason.

# ─── Railway ─────────────────────────────────────────────────────────────────
# Railway auto-deploys from GitHub; no deploy hook needed.
Expand Down
141 changes: 88 additions & 53 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,75 +225,110 @@ jobs:
$PRISMA migrate deploy --schema packages/outpost/db/prisma/schema.prisma
echo "CI database restored."

- name: Verify the worker startup guard
- name: Verify the startup guard
run: |
set -e
# Execute start.sh itself against a stub CLI. Nothing previously ran
# this script -- CI re-implemented its prisma calls in bash -- which is
# why a busybox-vs-GNU timeout exit-code difference sat in it unnoticed.
# Every branch that decides whether the worker may boot is asserted here.
# Every branch that decides whether a service may boot is asserted here.
#
# Both services are asserted, because they migrate the SAME database
# and share one guard (packages/outpost/db/schema-drift-guard.sh). A
# guard that only the worker ran would leave web free to serve
# traffic against a schema the worker refuses to run against.
stub=$(mktemp); chmod +x "$stub"
printf '#!/bin/sh\nif [ "$1" = migrate ] && [ "$2" = diff ]; then exit "${DIFF_RC:-0}"; fi\nexit "${DEPLOY_RC:-0}"\n' > "$stub"
script=$(mktemp)
sed 's|^exec node apps/worker/dist/index.js|echo STARTED|' apps/worker/start.sh > "$script"

check() { # label expect_exit expect_started env...
label=$1 want_rc=$2 want_started=$3; shift 3
# set +e around the call: half these cases are SUPPOSED to exit
# non-zero, and this step runs under `bash -e`, so the assignment
# would abort the whole step on the first expected failure.
guard="$PWD/packages/outpost/db/schema-drift-guard.sh"

for target in worker web; do
echo "== $target =="
script=$(mktemp)
# The last line of each start.sh execs the service; replace it so
# the script can run to completion without a built server.
sed 's|^exec node .*|echo STARTED|' "apps/$target/start.sh" > "$script"

check() { # label expect_exit expect_started env...
label=$1 want_rc=$2 want_started=$3; shift 3
# set +e around the call: half these cases are SUPPOSED to exit
# non-zero, and this step runs under `bash -e`, so the assignment
# would abort the whole step on the first expected failure.
set +e
out=$(env OUTPOST_PRISMA_CMD="$stub" OUTPOST_SCHEMA_GUARD_SCRIPT="$guard" "$@" sh "$script" 2>&1)
rc=$?
set -e
case "$out" in *STARTED*) got=yes ;; *) got=no ;; esac
if [ "$rc" != "$want_rc" ] || [ "$got" != "$want_started" ]; then
echo "FATAL: $target: $label -> exit=$rc started=$got (wanted exit=$want_rc started=$want_started)"
echo "$out"
exit 1
fi
echo " ok: $label"
}

check "clean schema boots" 0 yes DIFF_RC=0
check "drift blocks the boot" 1 no DIFF_RC=2
check "drift + override boots" 0 yes DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=1
check "drift + override=on boots" 0 yes DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=on
check "drift + unknown override blocks" 1 no DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=banana
check "drift + override=0 blocks" 1 no DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=0
# A check that could not RUN must be overridable too, or a CLI-level
# breakage is an unbreakable crash loop under restartPolicyType=ALWAYS.
check "tool failure blocks" 1 no DIFF_RC=1
check "tool failure + override boots" 0 yes DIFF_RC=1 OUTPOST_ALLOW_SCHEMA_DRIFT=1
check "migrate deploy failure blocks" 1 no DEPLOY_RC=1
# A bad timeout value must not fail the step closed.
check "non-numeric timeout still boots" 0 yes DIFF_RC=0 OUTPOST_STARTUP_STEP_TIMEOUT=abc
check "timeout=0 means unbounded" 0 yes DIFF_RC=0 OUTPOST_STARTUP_STEP_TIMEOUT=0

# Diagnostics must reach stderr, and an overridden boot must not page.
set +e
out=$(env OUTPOST_PRISMA_CMD="$stub" "$@" sh "$script" 2>&1)
rc=$?
env OUTPOST_PRISMA_CMD="$stub" OUTPOST_SCHEMA_GUARD_SCRIPT="$guard" DIFF_RC=2 sh "$script" 2>/dev/null | grep -q FATAL
leaked=$?
env OUTPOST_PRISMA_CMD="$stub" OUTPOST_SCHEMA_GUARD_SCRIPT="$guard" DIFF_RC=1 OUTPOST_ALLOW_SCHEMA_DRIFT=1 sh "$script" 2>&1 >/dev/null | grep -q FATAL
paged=$?
set -e
case "$out" in *STARTED*) got=yes ;; *) got=no ;; esac
if [ "$rc" != "$want_rc" ] || [ "$got" != "$want_started" ]; then
echo "FATAL: $label -> exit=$rc started=$got (wanted exit=$want_rc started=$want_started)"
echo "$out"
if [ "$leaked" -eq 0 ]; then
echo "FATAL: $target: diagnostics leaked to stdout."
exit 1
fi
echo " ok: $label"
}

check "clean schema boots" 0 yes DIFF_RC=0
check "drift blocks the boot" 1 no DIFF_RC=2
check "drift + override boots" 0 yes DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=1
check "drift + override=on boots" 0 yes DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=on
check "drift + unknown override blocks" 1 no DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=banana
check "drift + override=0 blocks" 1 no DIFF_RC=2 OUTPOST_ALLOW_SCHEMA_DRIFT=0
# A check that could not RUN must be overridable too, or a CLI-level
# breakage is an unbreakable crash loop under restartPolicyType=ALWAYS.
check "tool failure blocks" 1 no DIFF_RC=1
check "tool failure + override boots" 0 yes DIFF_RC=1 OUTPOST_ALLOW_SCHEMA_DRIFT=1
check "migrate deploy failure blocks" 1 no DEPLOY_RC=1
# A bad timeout value must not fail the step closed.
check "non-numeric timeout still boots" 0 yes DIFF_RC=0 OUTPOST_STARTUP_STEP_TIMEOUT=abc
check "timeout=0 means unbounded" 0 yes DIFF_RC=0 OUTPOST_STARTUP_STEP_TIMEOUT=0

# Diagnostics must reach stderr, and an overridden boot must not page.
set +e
env OUTPOST_PRISMA_CMD="$stub" DIFF_RC=2 sh "$script" 2>/dev/null | grep -q FATAL
leaked=$?
env OUTPOST_PRISMA_CMD="$stub" DIFF_RC=1 OUTPOST_ALLOW_SCHEMA_DRIFT=1 sh "$script" 2>&1 >/dev/null | grep -q FATAL
paged=$?
set -e
if [ "$leaked" -eq 0 ]; then
echo "FATAL: diagnostics leaked to stdout."
exit 1
fi
if [ "$paged" -eq 0 ]; then
echo "FATAL: an overridden boot emitted FATAL and would page."
exit 1
fi
if [ "$paged" -eq 0 ]; then
echo "FATAL: $target: an overridden boot emitted FATAL and would page."
exit 1
fi
done

# A sourced library must not change its caller's errexit setting.
OUTPOST_PRISMA_CMD="$stub" sh -c '
set +e
. "$1"
outpost_migrate_and_verify
case $- in *e*) exit 1 ;; esac
' sh "$guard"

# Exercise the actual BusyBox timeout used in the images. Its TERM
# timeout can return 0 if the child catches TERM and exits cleanly.
docker run --rm -v "$PWD:/src:ro" node:24-alpine sh -c '
printf "#!/bin/sh\nif [ \"\$2\" = diff ]; then trap '\''exit 0'\'' TERM; while :; do :; done; fi\n" > /tmp/prisma-stub
chmod +x /tmp/prisma-stub
for target in web worker; do
if OUTPOST_PRISMA_CMD=/tmp/prisma-stub OUTPOST_STARTUP_STEP_TIMEOUT=1 sh "/src/apps/$target/start.sh" > /tmp/guard-out 2>&1; then
printf "FATAL: %s accepted a timed-out check\n" "$target"
exit 1
fi
grep -q "timed out" /tmp/guard-out || { cat /tmp/guard-out; exit 1; }
done
'
echo "Startup guard behaves correctly across all branches."

- name: Verify the pinned Prisma CLI matches the lockfile
run: |
set -e
# apps/worker/start.sh gates the worker's boot on this CLI's `migrate
# diff` exit code, so a CLI that has drifted from @prisma/client can
# report false drift and block every deploy with a message pointing at
# the database. Keep the Dockerfile pin and the lockfile in lockstep.
# apps/web/start.sh and apps/worker/start.sh gate their boot on this
# CLI's `migrate diff` exit code, so a CLI that has drifted from
# @prisma/client can report false drift and block every deploy with
# a message pointing at the database. Keep the Dockerfile pin and the
# lockfile in lockstep.
set -o pipefail
resolved=$(node "$(cd packages/outpost && node -p "require.resolve('prisma/build/index.js')")" --version | sed -n 's/^prisma *: *\([0-9][^ ]*\).*/\1/p' | head -1)
if [ -z "$resolved" ]; then
Expand Down
16 changes: 14 additions & 2 deletions apps/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ COPY --from=installer --chown=outpost:outpost /app/node_modules/.pnpm/@prisma+cl
COPY --from=installer --chown=outpost:outpost /app/node_modules/.pnpm/@prisma+client*/node_modules/.prisma/client/schema.prisma ./apps/web/.next/server/
# Prisma schema and migrations for migrate deploy on startup
COPY --from=installer --chown=outpost:outpost /app/packages/outpost/db/prisma ./packages/outpost/db/prisma
# The schema-drift guard, shared with apps/worker/start.sh. start.sh sources it
# and refuses to serve traffic against a database that does not match
# schema.prisma. The worker's image already copies the whole packages/ tree, so
# it needs no equivalent line here.
COPY --from=installer --chown=outpost:outpost /app/packages/outpost/db/schema-drift-guard.sh ./packages/outpost/db/schema-drift-guard.sh
# Prisma CLI for running migrations at startup (installed with all transitive deps)
# Pinned in lockstep with apps/worker/Dockerfile. web and worker run migrate deploy
# against the SAME database, and the worker's start.sh now fails its deploy on any
# against the SAME database, and both start.sh files now fail their deploy on any
# schema difference -- so if web applies migrations with a different 6.x CLI than the
# worker verifies with, an unrelated web-side bump can hard-fail worker deploys with a
# drift message describing drift that does not exist. A CI step asserts both literals
Expand All @@ -86,7 +91,14 @@ EXPOSE 3000

# Use 127.0.0.1, not localhost: the standalone server binds IPv4 (0.0.0.0) and
# `localhost` can resolve to IPv6 ::1 inside the container, which is refused.
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
#
# start-period covers the whole pre-listen window, which is now two full prisma
# CLI invocations (migrate deploy, then the schema-drift check) before anything
# binds the port. At the old 10s an orchestrator honouring this status could
# restart the container mid-check — and killing it mid-migration manufactures
# the failed-migration (P3009) state the guard exists to diagnose. The value
# matches apps/worker/Dockerfile, which guards the same way.
HEALTHCHECK --interval=30s --timeout=5s --start-period=120s --retries=3 \
CMD wget -qO- http://127.0.0.1:3000/api/health || exit 1

COPY --from=installer --chown=outpost:outpost /app/apps/web/start.sh ./apps/web/start.sh
Expand Down
28 changes: 26 additions & 2 deletions apps/web/start.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
#!/bin/sh
set -e

# Run pending database migrations before starting
node /opt/prisma/node_modules/prisma/build/index.js migrate deploy --schema packages/outpost/db/prisma/schema.prisma
# The schema-drift guard is shared with apps/worker/start.sh. web and worker
# migrate the SAME database, so they have to agree about what that database looks
# like: a guard only one of them applies leaves the other free to serve traffic
# against a schema the guarded service refuses to run against.
#
# The path is resolved relative to this script, so the image can place the guard
# wherever it likes. OUTPOST_SCHEMA_GUARD_SCRIPT overrides it, which is how CI
# runs a copy of this script from a temporary directory.
OUTPOST_SERVICE=web
OUTPOST_DRIFT_WARNING="the web app keeps serving traffic against a schema it does not match until this is repaired"
OUTPOST_SCHEMA_GUARD_SCRIPT="${OUTPOST_SCHEMA_GUARD_SCRIPT:-$(dirname "$0")/../../packages/outpost/db/schema-drift-guard.sh}"
if [ ! -f "$OUTPOST_SCHEMA_GUARD_SCRIPT" ]; then
echo "[web] FATAL: the schema-drift guard is missing at ${OUTPOST_SCHEMA_GUARD_SCRIPT}." >&2
exit 1
fi
# shellcheck source=packages/outpost/db/schema-drift-guard.sh
. "$OUTPOST_SCHEMA_GUARD_SCRIPT"

# Apply the pending migrations, then check that the database really matches
# schema.prisma. `migrate deploy` alone only compares the migrations directory
# against the _prisma_migrations bookkeeping table and never looks at the actual
# schema, so a migration recorded as applied but never executed is invisible to
# it — and web would then serve every page against a schema it does not match.
# The guard's own comments explain the rest; the CI step "Verify the startup
# guard" asserts every branch of it against a stub CLI.
outpost_migrate_and_verify

# Start Next.js
# Bind all interfaces. Docker sets HOSTNAME to the container id, which Next's
Expand Down
2 changes: 1 addition & 1 deletion apps/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ try {
`[Worker] /health on ${port} reports 503 ("${boot.error}") for ${BOOT_FAILURE_LINGER_MS}ms, ` +
`then this process exits 1 so Railway's restart policy retries. ` +
`A missing table or column here means the database does not match schema.prisma — ` +
`check the schema-drift guard in apps/worker/start.sh.`,
`check the schema-drift guard in packages/outpost/db/schema-drift-guard.sh.`,
);
setTimeout(() => {
console.error(
Expand Down
Loading