| 13 | const lifecycleEnabled = process.env.PG_LIFECYCLE_DIAGNOSTICS === "1" |
| 14 | const lifecycleFile = process.env.PG_LIFECYCLE_LOG |
| 15 | const lifecycleLog = (id: string, step: string, state: string, ms: number, error?: unknown) => { |
| 16 | if (!lifecycleEnabled) return |
| 17 | const line = `[pg-lifecycle] ${new Date().toISOString()} pid=${process.pid} ${id} ${step} ${state} ${ms}ms${ |
| 18 | error === undefined ? "" : ` error=${inspect(error)}` |
| 19 | }\n` |
| 20 | // Bypass the reporter, which can drop console output from hooks. Diagnostics |
| 21 | // must not turn successful cleanup into a failure or replace its original error. |
| 22 | try { |
| 23 | process.stderr.write(line) |
| 24 | } catch {} |
| 25 | if (lifecycleFile !== undefined) { |
| 26 | try { |
| 27 | Fs.appendFileSync(lifecycleFile, line) |
| 28 | } catch (error) { |
| 29 | try { |
| 30 | process.stderr.write(`[pg-lifecycle] unable to append diagnostics: ${inspect(error)}\n`) |
| 31 | } catch {} |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | const lifecycle = async <A>(id: string, step: string, run: () => Promise<A>, signal?: AbortSignal): Promise<A> => { |
| 37 | if (!lifecycleEnabled) return run() |