Skip to content

perf(worker): consolidate worker processes - #1566

Merged
realcodesiman merged 9 commits into
mainfrom
bugfix/worker-memory-growth
Oct 29, 2026
Merged

realcodesiman merged 9 commits into
mainfrom
bugfix/worker-memory-growth

Conversation

@realcodesiman

@realcodesiman realcodesiman commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Changes

  • Add consolidated core and standalone worker entrypoints with coordinated graceful shutdown.
  • Make shutdown deadlines and all-mode heap caps opt-in outside consolidated modes.
  • Size consolidated database and shard pools for shared worker concurrency.
  • New environment variables: WORKER_SHUTDOWN_TIMEOUT_MS, WORKER_MAX_OLD_SPACE_MB, SHARD_POOL_MAX.
  • Prune third-party source-map segments and document worker process modes.

Test plan

  • pnpm lint
  • pnpm --filter worker check-types
  • pnpm --filter builder check-types
  • pnpm --filter @chatbotx.io/logger check-types
  • pnpm --filter worker build
  • pnpm --filter worker exec vitest run tests/worker-shutdown.test.ts tests/docker-entrypoint.test.ts
  • pnpm --filter @chatbotx.io/logger test
  • pnpm --filter builder exec vitest run tests/edge-instrumentation-graph.test.ts
  • pnpm --filter worker test: 329 suites passed; ai-agent-worker-audit-context and heavy-worker cannot resolve BullMQ through Vitest (/@fs/.../index.js&v=...).
  • manual verification (not run)

@github-actions github-actions Bot added the improvement Refactor or performance improvement label Oct 27, 2026
@realcodesiman
realcodesiman force-pushed the bugfix/worker-memory-growth branch from 0dbfb99 to cc19684 Compare October 28, 2026 03:03
…idated modes

Drop the implicit 256MB heap cap for legacy all-mode and make
WORKER_SHUTDOWN_TIMEOUT_MS optional, defaulting it in the entrypoint only for
standalone/core. Raise SHARD_POOL_MAX alongside DATABASE_POOL_MAX, and install
source-mapped stacks explicitly from the worker so the logger no longer pulls
node:module into every consumer.
Worker tests mock the logger with fatal and stub the business realtime
flush so the affected suites pass.
…nsumers

Add createQueueWorker so every consumer handles the Worker `error` event
(an unhandled one restarts all queues in a consolidated process), logs
failures and drains on shutdown. Share one QueueEvents per queue instead of
per-queue singletons, and document the shutdown grace period and job lock.
…tack logging

createQueueWorker now refuses to start once shutdown has begun, and catches
rejections from async onFailed handlers (BullMQ never awaits listeners, so
they would surface as unhandledRejection and drain every queue in a
consolidated process).

Also drops the source-mapped stack mapper from the logger, removes the
getHeavyQueueEvents wrapper in favour of getQueueEvents, dedupes the
consolidated-mode env defaults in the docker entrypoint, and downgrades the
per-job trigger completion log to debug.
Lock durations now cover the full heavy/integration job budgets so slow
provider calls are not redelivered mid-run. Fatal-error shutdown is bounded
at 30s and sets exitCode, the sequence consumer skips startup after
shutdown began, and consolidated DB pool defaults are raised to 80.
…ration failure

A shutdown that begins while a worker is still starting could leak the
workers, event listeners or scheduler ticks started afterwards. runWorker now
skips or tolerates starts that race shutdown, and the events and sequence
scheduler entries re-check the shutdown flag after their async startup.
A schedule registration failure no longer takes down the shared process.
Docs and the pool-ceiling test follow the env-driven pool and heap defaults.
@realcodesiman
realcodesiman force-pushed the bugfix/worker-memory-growth branch from b624706 to 9758072 Compare October 28, 2026 23:24
@realcodesiman
realcodesiman merged commit 01aef8d into main Oct 29, 2026
14 checks passed
@realcodesiman
realcodesiman deleted the bugfix/worker-memory-growth branch October 29, 2026 03:05
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
* perf(worker): consolidate worker processes

* fix(worker): bound consolidated worker memory growth

* fix(worker): make heap cap and shutdown timeout opt-in outside consolidated modes

Drop the implicit 256MB heap cap for legacy all-mode and make
WORKER_SHUTDOWN_TIMEOUT_MS optional, defaulting it in the entrypoint only for
standalone/core. Raise SHARD_POOL_MAX alongside DATABASE_POOL_MAX, and install
source-mapped stacks explicitly from the worker so the logger no longer pulls
node:module into every consumer.

* fix(worker): drop source-mapped stack install and update logger mocks

Worker tests mock the logger with fatal and stub the business realtime
flush so the affected suites pass.

* refactor(worker): share queue worker setup and queue events across consumers

Add createQueueWorker so every consumer handles the Worker `error` event
(an unhandled one restarts all queues in a consolidated process), logs
failures and drains on shutdown. Share one QueueEvents per queue instead of
per-queue singletons, and document the shutdown grace period and job lock.

* fix(worker): harden queue worker lifecycle and remove source-mapped stack logging

createQueueWorker now refuses to start once shutdown has begun, and catches
rejections from async onFailed handlers (BullMQ never awaits listeners, so
they would surface as unhandledRejection and drain every queue in a
consolidated process).

Also drops the source-mapped stack mapper from the logger, removes the
getHeavyQueueEvents wrapper in favour of getQueueEvents, dedupes the
consolidated-mode env defaults in the docker entrypoint, and downgrades the
per-job trigger completion log to debug.

* fix(worker): size job locks to job budgets and bound fatal shutdown

Lock durations now cover the full heavy/integration job budgets so slow
provider calls are not redelivered mid-run. Fatal-error shutdown is bounded
at 30s and sets exitCode, the sequence consumer skips startup after
shutdown began, and consolidated DB pool defaults are raised to 80.

* fix(worker): close startup/shutdown races and isolate schedule registration failure

A shutdown that begins while a worker is still starting could leak the
workers, event listeners or scheduler ticks started afterwards. runWorker now
skips or tolerates starts that race shutdown, and the events and sequence
scheduler entries re-check the shutdown flag after their async startup.
A schedule registration failure no longer takes down the shared process.
Docs and the pool-ceiling test follow the env-driven pool and heap defaults.

* fix(worker): isolate marketing message worker process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Refactor or performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant