Seeds: guardSeedExit() — a run that ends early exits 1 with the reason, never a silent 0
payload run starts the CLI with `void start()` and imports the script; when the script's top-level await is left without live handles, Node exits with code 0 and no output — a success in Coolify's Terminal. guardSeedExit() (@intecion/ipal-kit/seed, dependency-free) listens for beforeExit: until summary() runs it prints "[seed] ended before Payload initialized" / "… before the seed finished" and exits 1. createSeeder marks the run initialized, summary() finished; process.exit paths (normal, errors, the production refusals) are untouched. Wired into the ipal-site and ipal-form seed templates and the reference seed, so ipal-kit create sites get it. Tests include a real child process started like payload run. Docs: seeding.md, deployment.md §4.7, AGENT.md, ipal-reference (+ api.md), CHANGELOG Unreleased. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
55dc69b69f
commit
f789e36636
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* A seed run that ends early must fail loudly. `payload run` starts the CLI with `void start()` and then
|
||||
* `await import(script)`: when a seed script's top-level `await getPayload()` (or a later await) is left with
|
||||
* nothing that keeps Node's event loop alive, Node simply exits — code 0, no output. In Coolify's Terminal
|
||||
* that looks exactly like a successful run. The guard turns it into exit 1 with the reason.
|
||||
*
|
||||
* Dependency-free on purpose: no Payload, no Node imports — safe to import first in any seed script.
|
||||
*/
|
||||
|
||||
type ExitListener = () => void
|
||||
|
||||
/** The bits of `process` the guard uses — injectable for tests. */
|
||||
export type SeedExitProcess = {
|
||||
exit: (code: number) => void
|
||||
exitCode?: null | number | string
|
||||
off: (event: 'beforeExit', listener: ExitListener) => unknown
|
||||
on: (event: 'beforeExit', listener: ExitListener) => unknown
|
||||
}
|
||||
|
||||
export type SeedExitGuardOptions = {
|
||||
/** Prefix of the message. Default '[seed]'. */
|
||||
label?: string
|
||||
/** Where the message goes. Default stderr. */
|
||||
log?: (line: string) => void
|
||||
/** Default the real `process`. */
|
||||
process?: SeedExitProcess
|
||||
}
|
||||
|
||||
export type SeedExitGuard = {
|
||||
/** Stops watching (the run decides its exit code itself). */
|
||||
dispose: () => void
|
||||
/** The seed ended: `summary()` calls it. */
|
||||
finished: () => void
|
||||
/** Payload is up: `createSeeder` calls it. */
|
||||
initialized: () => void
|
||||
readonly state: 'finished' | 'initialized' | 'waiting'
|
||||
}
|
||||
|
||||
let active: null | SeedExitGuard = null
|
||||
|
||||
/** The guard of the current run, if the script started one (`createSeeder` and `summary()` report to it). */
|
||||
export function activeSeedExitGuard(): null | SeedExitGuard {
|
||||
return active
|
||||
}
|
||||
|
||||
/**
|
||||
* Call it first in a seed script, before `getPayload()`. Until `summary()` runs, an exit through an empty
|
||||
* event loop prints "[seed] ended before Payload initialized" (or "… before the seed finished") and exits 1.
|
||||
* A normal `process.exit(code)` — after the summary, on an error, or the guard's own refusal — is untouched.
|
||||
*/
|
||||
export function guardSeedExit(options: SeedExitGuardOptions = {}): SeedExitGuard {
|
||||
const proc: SeedExitProcess = options.process ?? process
|
||||
const label = options.label ?? '[seed]'
|
||||
const log = options.log ?? ((line: string) => {process.stderr.write(`${line}\n`)})
|
||||
let state: SeedExitGuard['state'] = 'waiting'
|
||||
|
||||
const onBeforeExit = () => {
|
||||
if (state === 'finished') {return}
|
||||
const when = state === 'waiting' ? 'before Payload initialized' : 'before the seed finished'
|
||||
log(
|
||||
`${label} ended ${when} — the run stopped early and nothing after that point was written. ` +
|
||||
'Exit code 1. Run it again; if it repeats, check the database connection and the seed script.',
|
||||
)
|
||||
proc.exitCode = 1
|
||||
proc.exit(1)
|
||||
}
|
||||
|
||||
const guard: SeedExitGuard = {
|
||||
dispose: () => {
|
||||
proc.off('beforeExit', onBeforeExit)
|
||||
if (active === guard) {active = null}
|
||||
},
|
||||
finished: () => {
|
||||
state = 'finished'
|
||||
},
|
||||
initialized: () => {
|
||||
if (state === 'waiting') {state = 'initialized'}
|
||||
},
|
||||
get state() {
|
||||
return state
|
||||
},
|
||||
}
|
||||
active?.dispose()
|
||||
proc.on('beforeExit', onBeforeExit)
|
||||
active = guard
|
||||
return guard
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
export { activeSeedExitGuard, guardSeedExit } from './exitGuard.js'
|
||||
export type { SeedExitGuard, SeedExitGuardOptions, SeedExitProcess } from './exitGuard.js'
|
||||
export { isLocalDatabase } from './guard.js'
|
||||
export { createSeeder } from './seeder.js'
|
||||
export type { Seeder, SeederOptions, SeedResult, SeedStatus, SeedSummary, UpsertOptions } from './seeder.js'
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { BasePayload } from 'payload'
|
||||
|
||||
import type { SeedTarget } from './values.js'
|
||||
|
||||
import { activeSeedExitGuard } from './exitGuard.js'
|
||||
import { isLocalDatabase, seedDatabaseUrl } from './guard.js'
|
||||
import {
|
||||
defaultHashField,
|
||||
@@ -178,6 +179,8 @@ const hasRef = (v: unknown): boolean =>
|
||||
: !!v && typeof v === 'object' && (typeof (v as { $page?: unknown }).$page === 'string' || formRef(v) !== undefined || Object.values(v).some(hasRef))
|
||||
|
||||
export function createSeeder(payload: BasePayload, options: SeederOptions = {}): Seeder {
|
||||
// Payload is up (the caller passed it in) — the run's exit guard, if any, now waits for summary()
|
||||
activeSeedExitGuard()?.initialized()
|
||||
const env = process.env
|
||||
const dryRun = options.dryRun ?? (process.argv.includes('--dry-run') || env.SEED_DRY_RUN === '1')
|
||||
const update = options.update ?? env.SEED_UPDATE === '1'
|
||||
@@ -459,6 +462,7 @@ export function createSeeder(payload: BasePayload, options: SeederOptions = {}):
|
||||
}
|
||||
|
||||
const summary = (): SeedSummary => {
|
||||
activeSeedExitGuard()?.finished()
|
||||
const result: SeedSummary = {
|
||||
created: counts.create,
|
||||
errors: counts.error,
|
||||
|
||||
Reference in New Issue
Block a user