SPB Git

Repository Public

chaos-worker.ts 1.5 KB 50 lines typescript

1/**2 * Child process of the chaos suite. Runs a worker loop on DATABASE_URL and SIGKILLs ITSELF when step3 * KILL_STEP reaches KILL_BOUNDARY. Without a kill it exits 0 once RUN_ID is terminal or waiting.4 */5import { createKernel, runWorkerLoop } from '../../src/index.js';6import { makeChaosLogic } from './chaos-logic.js';78const env = process.env;9const k = createKernel({ databaseUrl: env.DATABASE_URL, poolMax: 4 });10const killStep = Number(env.KILL_STEP ?? -1);11const killBoundary = env.KILL_BOUNDARY ?? '';12const runId = env.RUN_ID ?? '';13const ac = new AbortController();1415const loop = runWorkerLoop({16  kernel: k,17  logic: makeChaosLogic({ assumed: env.CHAOS_ASSUMED !== '0' }),18  signal: ac.signal,19  concurrency: 1,20  leaseMs: Number(env.LEASE_MS ?? 700),21  heartbeatMs: 150,22  pollMs: 40,23  janitorMs: false,24  dailyBudgetUsd: null,25  workerId: `chaos-${process.pid}`,26  hooks: {27    boundary: (i) => {28      if (i.seq === killStep && i.boundary === killBoundary) process.kill(process.pid, 'SIGKILL');29    },30  },31});3233const DONE = ['waiting', 'done', 'partial', 'blocked', 'failed', 'cancelled', 'budget_exhausted'];34const timer = setInterval(() => {35  k.pool.query('SELECT status FROM runs WHERE id = $1', [runId]).then(36    (r) => {37      const status = (r.rows[0] as { status: string } | undefined)?.status;38      if (status && DONE.includes(status)) {39        clearInterval(timer);40        ac.abort();41      }42    },43    () => undefined,44  );45}, 40);4647await loop;48await k.close();49process.exit(0);50
Files main
Menu

Theme