Skip to Content
ArchitectureProcess Lifecycle

Process lifecycle

What the two long-running processes do between node starting them and the process ending: the order of the startup steps, which step can stop a process and with what exit code, when the Follow Up Boss webhooks are registered and removed, how the worker sets up and keeps its consume loop, and what a signal does to each process. The failure of a single request is on Failure & degradation; terms are in the Glossary.

Architecture overview · Configuration resolution · Follow Up Boss: the NATS worker · Deployment · Troubleshooting

D10 — Startup and shutdown lanes

Each box is one step in the order the entry module runs it. Dashed arrows are the branches the configuration chooses; the outcome of a failing step is in the table below.

Two orderings follow from the web lane. The port is bound before the webhooks are installed, so requests are served while installHooks is still running; and the signal handlers exist only after installHooks has resolved. Observed with webhooks disabled: the Webhooks are disabled line is logged before Server is running on port ..., because the listen callback runs after the synchronous part of installHooks.

What stops a step

StepProcessOn failureExitSource
Configuration importbothAn assert or a thrown URL-assembly error at import (startup prerequisites). It happens before uncaughtExceptionMonitor is registered, so only Node’s own stack trace reaches stderr1src/config.ts, src/lib/settings.ts (constructEventsCollectionUrls)
JWT key fileswebfs.readFileSync throws while a route module resolves middleware.jwt at import; the worker never reads the keys1src/providers/middleware/jwt/config.keys.ts, module-level container.resolve in src/routes/*.ts
Port bindwebThe server has no error listener, so a bind error (for example the port in use) is an uncaught exception1src/index.ts
NATS connectwebWith NATS_ENABLED=true, the connection promise is created by resolving BossWebhooksService and stored unawaited; when the connection is refused it is an unhandled rejection. Observed: logged as Fatal with origin: "unhandledRejection", then Node exits, the HTTP server already listening1src/index.ts, src/services/boss/webhook.ts (constructor), src/providers/nats.ts
Webhook installwebinstallHooks is awaited at the top level; a FUB error (or an ngrok error) rejects that await. Old webhooks may already be deleted and some new ones created; the signal handlers are not registered yet1src/index.ts, src/services/boss/webhook.ts (installHooks, removeOldHooks)
NATS connectworkerpRetry with its defaults (10 retries, delays 1 s, 2 s, 4 s and so on). instanceCachingFactory keeps the first promise, so every attempt awaits the same rejected promise and no new connection is made. Observed: attempts logged at 0 s, 1 s and 3 s as Establishing NATS connection, attempt <n> failed. There are <m> retries left.; after the last retry the top-level await rejects1src/worker.ts, src/providers/nats.ts
Consumer lookupworkerjsc.consumers.get rejects (for example an unknown consumer); the provider created the durable consumer when it was absent1src/worker.ts, src/providers/nats.ts
Worker classworkerError("Worker class not found for stream ...") when no stream in src/streams/index.ts has the configured name1src/worker.ts
consume()workerThe call sits outside the try, so a rejection ends the loop and the module1src/worker.ts
Message iterationworkerCaught: the error message (err.message) is logged and consume() is called again at oncenonesrc/worker.ts
One messageworkerCaught inside StreamWorker.process (nak, or term for an unknown subject); the loop continues. Outcomes per subject are on Follow Up Bossnonesrc/streams/worker.ts

A rejected top-level await ends the process even while the HTTP server is listening (observed with Node’s ESM loader, exit code 1).

Process-level errors

Both entry points register the same observer after their imports: process.on("uncaughtExceptionMonitor", ...) writes one fatal line, Fatal, with err and origin, through logger.global. It only observes: Node then prints the stack to stderr and exits with code 1.

EventBehaviourSource
Error thrown inside a requestAnswered by the error handler in src/app.ts; the process keeps running (error handling)src/app.ts
Uncaught exceptionFatal line with origin: "uncaughtException", then exit 1src/index.ts, src/worker.ts
Unhandled rejectionNode’s default throw mode turns it into an uncaught exception: Fatal line with origin: "unhandledRejection", then exit 1 (observed)same
Rejection inside a detached second handlerCaught by the .catch in middleware.eventsCollection, logged as Events collection failed (D06)src/providers/middleware/eventsCollection.ts

Shutdown

WebWorker
HandlersSIGINT and SIGTERM, registered after installHooksnone
On the signallog Shutting down; uninstallHooks(): returns at once when webhooks are disabled, else deletes each id in runningWebhooksIds with FUB DELETE /webhooks/{id}, one after the other, and stops at the first failure; process.exit(0) in finally, whatever the outcomeNode’s default action: the process ends without running any code
HTTP servernot closed; requests in flight end with the processn/a
Message in flightn/anot acknowledged; redelivery is up to the server’s ack wait (consumer settings). Repliers and FUB writes it already made stay
Database pool, Keyv clients, NATS connectionnot closed or drained by the codenot closed or drained by the code

A signal that arrives before the web process has registered its handlers (during installHooks) gets Node’s default action, so uninstallHooks does not run.

State each process holds

StateWebWorkerCreatedSource
HTTP serveryesnoat startupsrc/index.ts
NATS connection, stream update or add, durable consumer addwhen NATS_ENABLED=truewhen NATS_ENABLED=truewhen nats is first resolved: by BossWebhooksService in the web process, by the retry wrapper in the workersrc/providers/nats.ts
Knex pool (min: 1, max: 5)yesyesKnex instance when db is first resolved; no connection is attempted at startup (Persistence)src/providers/db.ts
Keyv storesyesthose of the modules it imports@cached namespaces when the decorated class is defined (module load); keyv.otp and keyv.blocklist when first resolved (Caching)src/lib/decorators/cached.ts, src/providers/keyv.*.ts
Repliers throttlesyesyes, used by the worker’s Repliers clientson first resolve; process-widesrc/providers/throttler/*.ts
Remembered webhook idsyes, runningWebhooksIds of the instance created at startupnoby installHookssrc/services/boss/webhook.ts

There is no health or readiness route under /api (src/routes/index.ts); a bound port is the only sign the web process gives that it started, and it is given before the webhooks are installed.

Variation

  • NATS disabled (the default): the nats token is DummyNats; the web process holds no connection, and the worker logs NATS is disabled, not starting worker and ends with exit code 0 (observed); see Deployment for how to run it then.
  • Webhooks disabled (the default): installHooks logs and returns, uninstallHooks returns at once; the webhook route stays mounted.
  • Webhooks enabled with BOSS_ENABLED=false: installHooks logs an error and returns; nothing is installed and the process starts.
  • ngrok (BOSS_WEBHOOK_USE_NGROK=true): installHooks opens a listener on the app port and uses its URL as the callback origin; the code does not close it.
  • npm run dev: nodemon restarts the processes with its default signal SIGUSR2 (nodemon 3.1.14 default), for which no handler is registered, so a restart does not run uninstallHooks. The next start’s removeOldHooks deletes what FUB still lists.
  • Process name: the NATS connection name is suffixed with backend for an entry file named index, worker for worker and unknown for anything else (getProcessName in src/lib/introspection.ts).

Evidence

  • Source: src/index.ts, src/worker.ts, src/providers/nats.ts, src/providers/index.ts, src/services/boss/webhook.ts (installHooks, uninstallHooks), src/streams/worker.ts, src/lib/dummy.nats.ts. Libraries read in node_modules: p-retry 8.0.1 defaults, tsyringe 4.10.0 instanceCachingFactory, @nats-io/nats-core 3.4.0 connection options, nodemon 3.1.14 defaults.
  • Executed on 2026-10-20 (Node 26.10.0; package.json requires ^24.21.0), each entry module run with placeholder values for the four required variables, no env file, PORT=0 and no upstream reachable: the web process with NATS_ENABLED=true and a closed NATS port (exit 1 after about 4 s, Fatal line with origin: "unhandledRejection"); the worker with NATS disabled (exit 0 after the warning); the worker with a closed NATS port (three attempts in 12 s, then stopped by the probe); a plain ESM module whose top-level await rejects while a server listens (exit 1).
  • Not executed: signals (the probes ran on Windows, where SIGTERM cannot be delivered to a Node process from outside), webhook installation against FUB, a NATS server that goes away after the connection was made. What the NATS client and the consume iterator do during a reconnect is library behaviour, unverified; the client options are the library defaults (reconnect on, 10 attempts, 2 s apart, read in @nats-io/nats-core).

Gotchas

  1. The worker’s startup retry never reconnects. Each attempt awaits the promise cached by the nats provider. Source: src/worker.ts, src/providers/nats.ts.
  2. Import-time failures bypass the Fatal log line. The monitor is registered after the imports. Source: src/index.ts, src/worker.ts.

Verified against portal-backend 997ae3e, 2026-10-20.

Last updated on