Skip to content

refactor(worker): move coexist jobs to dedicated heavy worker - #1067

Draft
viethung0o0 wants to merge 2 commits into
mainfrom
refactor/bulk-worker-coexist-split
Draft

refactor(worker): move coexist jobs to dedicated heavy worker#1067
viethung0o0 wants to merge 2 commits into
mainfrom
refactor/bulk-worker-coexist-split

Conversation

@viethung0o0

Copy link
Copy Markdown
Contributor

Summary

  • Moves the 5 coexist historical-sync jobs (coexistWhatsappBuffer/Flush, coexistMessengerSync, coexistInstagramSync, coexistAttachmentDownload) off the integration queue onto a new workload-class heavy queue with its own worker, so long-running Graph-API sync chunks no longer occupy the concurrency slots that real-time chat jobs (incomingMessage, sendFlow) need.
  • The heavy queue is a generic tier for long-lock, throughput-oriented jobs — coexist is its first tenant; future heavy workloads (e.g. import backfills) join it instead of spawning new workers.
  • The integration worker keeps a small forward-only shim for legacy coexist jobs still in bull:integration after deploy (forwards them to the heavy queue preserving jobId and options); scripts/check-coexist-drain.mts gates a follow-up PR that deletes the shim once the old queue is clean. Its stalledInterval override is also removed so crashed-worker jobs are re-picked in seconds.

Changes

  • packages/worker-config/src/queues/heavy/ — new heavy queue module: HeavyJobAction, HeavyJobData (+ runtime heavyJobDataSchema), heavyQueue; the 5 actions removed from the integration queue module.
  • apps/worker/src/heavy/ — new worker (blocked-owner guard, audit context, 10-min lock sized for sync chunks, HEAVY_WORKER_CONCURRENCY env) with the coexist handlers moved under handlers/coexist/ (git renames, history preserved); all coexist follow-up enqueues retarget heavyQueue (updateContactAvatar fan-out stays on integration).
  • apps/worker/src/integration/worker.ts — coexist cases/handler imports removed; forward-only legacy shim added; lock comment re-derived from CHAT_JOB_WAIT_TIMEOUT_MS; stalledInterval override dropped.
  • apps/worker/src/schedule/handlers/scan-coexist-runs.ts, packages/business/src/coexist/service.ts — enqueue/strategy types retarget the heavy queue (jobId schemes unchanged).
  • packages/sdk + integrations/whatsappHandleRequestProps gains a generic optional heavyQueue; the WhatsApp webhook enqueues the coexist buffer through it and logs an error if it is missing; both builder webhook routes inject it.
  • apps/builder/src/app/developer/queues/** — heavy queue registered in the queues dashboard.
  • scripts/check-coexist-drain.mts — read-only drain gate for removing the legacy shim later.
  • Tests — suites updated for the move plus new coverage: heavy-worker boot, legacy-shim forwarding (jobId/opts preservation, malformed-payload fall-through), route-level heavyQueue wiring, heavyJobDataSchema, build-phase fake queue, docker entrypoint roster.

Test plan

  • pnpm lint
  • check-types for worker, builder, @chatbotx.io/worker-config, @chatbotx.io/business, @chatbotx.io/sdk, @chatbotx.io/integration-whatsapp
  • pnpm --filter worker test (194 files, 1841 tests)
  • pnpm --filter builder test (312 files, 2027 tests)
  • pnpm --filter @chatbotx.io/business test (1527 tests), @chatbotx.io/worker-config (25), @chatbotx.io/integration-whatsapp (215)
  • apps/worker tsdown build produces dist/heavy/worker.mjs (docker entrypoint auto-discovers it)
  • scripts/check-coexist-drain.mts runs clean against local Redis
  • Manual: after deploy, WhatsApp coexist webhook → buffer → flush lands on bull:heavy and history imports complete
  • Manual: ~1 day after deploy, run pnpm tsx scripts/check-coexist-drain.mts; when clean, open the follow-up PR removing the legacy shim + script

@github-actions github-actions Bot added the improvement Refactor or performance improvement label Aug 30, 2026
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.

2 participants