Skip to Content
DevelopersHow Koomerce worksWorker and jobs

4 min read

Worker and jobs

apps/worker is one Bun process with two sides: an HTTP side (Hono) that takes webhooks, and a job side (BullMQ on Dragonfly) that runs everything that should not happen inside a page request. Its entry point is apps/worker/src/index.ts.

Why a separate process: emails, PDFs, courier calls and imports are slow and can fail; a shopper or merchant should never wait for them, and a retry must still happen when nobody has a page open.

The outbox: how work reaches the worker

  1. A change (order paid, return approved, product saved) writes an outbox_event row in the same transaction (packages/core/src/outbox/write.ts).
  2. {outbox}-relay runs every 5 seconds (and an admin action can nudge its own store’s rows out at once). It claims undispatched rows store by store (relayStoreOutbox, packages/core/src/outbox/relay.ts).
  3. Each row fans out to its consumers: a shopper message on {notifications}-send, one {apps}-events job per app with a hook for it, automation triggers, pixel conversions, restock alerts, domain routes, Meilisearch index syncs.

If the relay or Redis is down, rows wait in Postgres and go out when it is back. Pattern from Trigger.dev’s event log and Temporal’s history (credited in outbox/write.ts).

Queues that run on demand

QueueWhat one job isPer store at once
{notifications}-sendOne shopper or staff message (email, WhatsApp, SMS)3
{apps}-eventsOne app’s hook for one event (Apps)2
{apps}-jobsAn app’s own job: an invoice PDF batch, a Shopify import run2
{marketing}-sendOne campaign, sent in batches1
{media}-processOne uploaded image: variants, ThumbHash, metadata stripped1
{back-in-stock}-restockAlerts for a variant back in stock1
{store}-exportA full store data export1
{ai}-bulk-writeOne product of a bulk “Write with AI” run1
{ai}-purchase-documentsAI read of a supplier invoice or Bill of Entry1
{automations}-trigger, {automations}-stepOne workflow trigger, one run step4
{pixels}-order, {pixels}-sendOrder events to Meta and Google conversions2, 3
{inbox}-ai-replyOne AI reply to a shopper message2
{themes}-screenshotA theme thumbnail when Customize could not take one—
{domains}-routeAdd or remove a custom domain’s HTTPS route (platform work, one at a time)—
{channels}-syncCatalogue sync to Meta and Google (also every 6 hours)—
{reviews}-summariesRefresh a product’s AI review summary—

The per-store caps are QUEUE_LIMITS (packages/queue/src/index.ts), applied by fairProcessor (packages/queue/src/fair.ts): a store at its cap has its job put back for 1.5 to 3 seconds and the worker takes another store’s job. Why: one store’s 5,000-image import must not hold up every other store’s order emails.

Scheduled work

Most schedules run as named jobs on one queue, {system}-sweeps (apps/worker/src/sweeps.ts), so they share one Redis connection; their old queue names are cleaned up on boot. Each finds the stores with work through a SECURITY DEFINER function, then opens one withStore per store.

JobEveryDoes
outbox relay ({outbox}-relay)5 sAbove
heartbeat ({system}-heartbeat)1 minLogs that the worker is alive
unpaid-order-expiry1 minCancels unpaid online orders after the store’s window, asking the gateway first; transfer follow-ups
preorder-balance1 minAsks for, reminds and applies the rule for pre-order balances
domain-certificate1 minChecks store addresses and custom domains over TLS, retries missing certificates
domain-verify2 minRe-checks domains waiting on DNS
rollouts-tick1 minStarts and ends scheduled rollouts and A/B tests
marketing-tick, automations-tick30 sStarts due campaigns; advances waiting automation runs
order-push5 minSends new orders to Shiprocket and RapidShyp as pending orders
sender-email-verify5 minChecks sender email domains (verified ones daily)
review-requests10 minQueues review request and reminder emails
member-daily-summary10 minSends each member their summary at the hour they chose
bug-report-sync15 minRe-reads open bug reports from GitHub (when a token is set)
review-summaries15 minRefreshes review summaries with new reviews
shipping-tracker ({shipping}-track)30 minPolls tracking for every open shipment
payment-fees30 minAsks gateways for fees their webhook did not carry
courier-charges1 hourReads back what couriers billed: an empty charge (actualCharge, daily for 7 days, never over a typed one) and, for couriers with billedCharges (Shiprocket), the full bill with the charged weight, 3 h after booking then daily for 30 days, so re-weighs reach profit and Weight disputes (core billed-charges.ts)
store-credit-expiry, import-snapshot-cleanup, affiliate-commissions, home-cards, search-console-sweep, billing-sweep1 hExpire credit; delete week-old import previews; approve commissions past their hold; rebuild Home cards; queue Search Console syncs; bill plans, mark due, pause after grace
refund-sync, api-activity-prune, store-activity-prune, automations-prune, pixels-prune, shopify-token-sweep1 daySettle late refunds; delete old MCP/API activity (90 days), activity log (1 year), automation and pixel logs; swap Shopify tokens
retention-prunedaily scheduleDeletes data past each table’s retention
fx-rates21:30 IST dailyExchange rates for markets

Abandoned checkout and cart reminders are automations now, run by the marketing tick; the old hourly {abandoned}-sweep is removed on boot (retireAbandonedSweep).

Edge purge

apps/worker/src/edge-purge.ts listens on the database’s storefront_cache channel (the same one the storefront uses to drop its own cache) and purges that store’s hostnames from Cloudflare about 5 seconds after its writes settle, at most every 30 seconds per store; after a storefront deploy it purges the whole zone twice. Off unless CLOUDFLARE_CACHE_PURGE_TOKEN is set. See Performance and caching.

Webhooks in

PathFrom
POST /webhooks/payments/:provider/:storeIdPayment gateways
POST /webhooks/shipments/:provider/:storeId, /webhooks/shipments/c/:code/:storeIdCouriers
/webhooks/platform/meta, /webhooks/platform/shopifyMeta (WhatsApp, catalogue) and the Shopify migration app
SES feedbackBounces and complaints (email-feedback.ts)
GET /healthDocker health check and the monitor

Webhooks are rate limited per IP and verified with the store’s own secret before anything is stored. See Platform apps.

Retries, failure and memory

  • Jobs retry (with exponential backoff where a job sets it). A job that fails every attempt is kept 14 days for inspection; finished jobs are trimmed (KEEP_FINISHED) so Dragonfly never fills (it did once, in a week, before the cap).
  • An app job that fails for good shows on the Installed apps list; app jobs can declare a jobFailed handler.
  • memory-guard.ts watches the worker’s own memory against its container limit: near it, bulk queues pause and the rest slow down; close to it, everything but payments, order emails and the outbox pauses; a long stay there restarts the process cleanly. Why: the worker shares a 4 GB server with the other apps and Postgres, and an out-of-memory kill could take any of them down.
Was this helpful?
Last updated on