@open-mercato/queue 0.6.7-develop.6827.1.a575bebce9 → 0.6.7-develop.6834.1.e76f4b8cbc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- [build:queue] found 8 entry points
1
+ [build:queue] found 9 entry points
2
2
  [build:queue] built successfully
@@ -1,4 +1,6 @@
1
1
  import { getRedisUrlOrThrow } from "@open-mercato/shared/lib/redis/connection";
2
+ import { getTelemetryRuntime } from "@open-mercato/shared/lib/telemetry/runtime";
3
+ import { attachTraceMetadata, runJobInTrace } from "../tracing.js";
2
4
  import { createLogger } from "@open-mercato/shared/lib/logger";
3
5
  const packageLogger = createLogger("queue");
4
6
  const REMOVABLE_JOB_STATES = ["waiting", "delayed", "prioritized", "paused", "waiting-children"];
@@ -40,6 +42,7 @@ function createAsyncQueue(name, options) {
40
42
  let bullQueue = null;
41
43
  let bullWorker = null;
42
44
  let bullmqModule = null;
45
+ let telemetryPromise = null;
43
46
  async function getBullMQ() {
44
47
  if (!bullmqModule) {
45
48
  try {
@@ -52,19 +55,38 @@ function createAsyncQueue(name, options) {
52
55
  }
53
56
  return bullmqModule;
54
57
  }
58
+ async function getQueueTelemetry() {
59
+ if (!telemetryPromise) {
60
+ telemetryPromise = (async () => {
61
+ if (!getTelemetryRuntime()?.canUseGlobalTracePropagation()) return void 0;
62
+ try {
63
+ const mod = await import("bullmq-otel");
64
+ return new mod.BullMQOtel("open-mercato");
65
+ } catch {
66
+ packageLogger.warn("bullmq-otel not available; using built-in trace carrier", { queue: name });
67
+ return void 0;
68
+ }
69
+ })();
70
+ }
71
+ return telemetryPromise;
72
+ }
55
73
  async function getQueue() {
56
74
  if (!bullQueue) {
57
75
  const { Queue: BullQueueClass } = await getBullMQ();
58
- bullQueue = new BullQueueClass(name, { connection });
76
+ const telemetry = await getQueueTelemetry();
77
+ bullQueue = new BullQueueClass(name, { connection, ...telemetry ? { telemetry } : {} });
59
78
  }
60
79
  return bullQueue;
61
80
  }
62
81
  async function enqueue(data, options2) {
63
82
  const queue = await getQueue();
83
+ const telemetry = await getQueueTelemetry();
84
+ const metadata = telemetry ? void 0 : attachTraceMetadata(void 0);
64
85
  const jobData = {
65
86
  id: crypto.randomUUID(),
66
87
  payload: data,
67
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
88
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
89
+ ...metadata ? { metadata } : {}
68
90
  };
69
91
  const job = await queue.add(jobData.id, jobData, {
70
92
  delay: options2?.delayMs && options2.delayMs > 0 ? options2.delayMs : void 0,
@@ -77,19 +99,26 @@ function createAsyncQueue(name, options) {
77
99
  }
78
100
  async function process(handler) {
79
101
  const { Worker } = await getBullMQ();
102
+ const telemetry = await getQueueTelemetry();
80
103
  bullWorker = new Worker(
81
104
  name,
82
105
  async (job) => {
83
106
  const jobData = job.data;
84
- await handler(jobData, {
107
+ const ctx = {
85
108
  jobId: job.id ?? jobData.id,
86
109
  attemptNumber: job.attemptsMade + 1,
87
110
  queueName: name
88
- });
111
+ };
112
+ if (telemetry) {
113
+ await handler(jobData, ctx);
114
+ } else {
115
+ await runJobInTrace(name, jobData.metadata, () => handler(jobData, ctx));
116
+ }
89
117
  },
90
118
  {
91
119
  connection,
92
120
  concurrency,
121
+ ...telemetry ? { telemetry } : {},
93
122
  ...lockDuration !== void 0 ? { lockDuration } : {},
94
123
  ...maxStalledCount !== void 0 ? { maxStalledCount } : {}
95
124
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/strategies/async.ts"],
4
- "sourcesContent": ["import type { Queue, QueuedJob, JobHandler, AsyncQueueOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'\nimport { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst packageLogger = createLogger('queue')\n\n// BullMQ interface types - we define the shape we use to maintain type safety\n// while keeping bullmq as an optional peer dependency\ntype ConnectionOptions = {\n url?: string\n host?: string\n port?: number\n username?: string\n password?: string\n db?: number\n tls?: Record<string, unknown>\n}\n\ninterface BullQueueInterface<T> {\n add: (\n name: string,\n data: T,\n opts?: {\n removeOnComplete?: boolean\n removeOnFail?: number\n delay?: number\n attempts?: number\n backoff?: { type: string; delay: number }\n },\n ) => Promise<{ id?: string }>\n obliterate: (opts?: { force?: boolean }) => Promise<void>\n close: () => Promise<void>\n getJobCounts: (...states: string[]) => Promise<Record<string, number>>\n getJobs: (types: string[], start?: number, end?: number) => Promise<Array<{\n data?: T\n remove: () => Promise<void>\n }>>\n}\n\ninterface BullWorkerInterface {\n on: (event: string, handler: (...args: unknown[]) => void) => void\n close: () => Promise<void>\n}\n\ninterface BullMQModule {\n Queue: new <T>(name: string, opts: { connection: ConnectionOptions }) => BullQueueInterface<T>\n Worker: new <T>(\n name: string,\n processor: (job: { id?: string; data: T; attemptsMade: number }) => Promise<void>,\n opts: { connection: ConnectionOptions; concurrency: number; lockDuration?: number; maxStalledCount?: number }\n ) => BullWorkerInterface\n}\n\nconst REMOVABLE_JOB_STATES = ['waiting', 'delayed', 'prioritized', 'paused', 'waiting-children']\n\nfunction payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {\n if (!payload || typeof payload !== 'object') return false\n const scopedPayload = payload as { tenantId?: unknown; organizationId?: unknown; jobType?: unknown }\n if (scopedPayload.tenantId !== scope.tenantId) return false\n if (scope.organizationId !== undefined) {\n if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false\n }\n if (scope.jobTypes?.length) {\n return typeof scopedPayload.jobType === 'string' && scope.jobTypes.includes(scopedPayload.jobType)\n }\n return true\n}\n\n/**\n * Resolves Redis connection options from various sources.\n *\n * BullMQ expects an ioredis-compatible connection object. Preserve the full\n * Redis URL under the `url` key so rediss://, username, database, and query\n * params are not lost in translation.\n */\nfunction resolveConnection(options?: AsyncQueueOptions['connection']): ConnectionOptions {\n if (options?.url) {\n return { url: options.url }\n }\n\n if (options?.host) {\n return {\n host: options.host,\n port: options.port ?? 6379,\n username: options.username,\n password: options.password,\n db: options.db,\n tls: options.tls,\n }\n }\n\n return { url: getRedisUrlOrThrow('QUEUE') }\n}\n\n/**\n * Creates a BullMQ-based async queue.\n *\n * This strategy provides:\n * - Persistent job storage in Redis\n * - Automatic retries with exponential backoff\n * - Concurrent job processing\n * - Job prioritization and scheduling\n *\n * @template T - The payload type for jobs\n * @param name - Queue name\n * @param options - Async queue options\n */\nexport function createAsyncQueue<T = unknown>(\n name: string,\n options?: AsyncQueueOptions\n): Queue<T> {\n const connection = resolveConnection(options?.connection)\n const concurrency = options?.concurrency ?? 1\n const attempts = options?.attempts ?? 3\n const lockDuration = options?.lockDuration\n const maxStalledCount = options?.maxStalledCount\n const logger = packageLogger.child({ queue: name })\n\n let bullQueue: BullQueueInterface<QueuedJob<T>> | null = null\n let bullWorker: BullWorkerInterface | null = null\n let bullmqModule: BullMQModule | null = null\n\n // -------------------------------------------------------------------------\n // Lazy BullMQ initialization\n // -------------------------------------------------------------------------\n\n async function getBullMQ(): Promise<BullMQModule> {\n if (!bullmqModule) {\n try {\n bullmqModule = await import('bullmq') as unknown as BullMQModule\n } catch {\n throw new Error(\n 'BullMQ is required for async queue strategy. Install it with: npm install bullmq'\n )\n }\n }\n return bullmqModule\n }\n\n async function getQueue(): Promise<BullQueueInterface<QueuedJob<T>>> {\n if (!bullQueue) {\n const { Queue: BullQueueClass } = await getBullMQ()\n bullQueue = new BullQueueClass<QueuedJob<T>>(name, { connection })\n }\n return bullQueue\n }\n\n // -------------------------------------------------------------------------\n // Queue Implementation\n // -------------------------------------------------------------------------\n\n async function enqueue(data: T, options?: EnqueueOptions): Promise<string> {\n const queue = await getQueue()\n const jobData: QueuedJob<T> = {\n id: crypto.randomUUID(),\n payload: data,\n createdAt: new Date().toISOString(),\n }\n\n const job = await queue.add(jobData.id, jobData, {\n delay: options?.delayMs && options.delayMs > 0 ? options.delayMs : undefined,\n removeOnComplete: true,\n removeOnFail: 1000,\n attempts,\n backoff: { type: 'exponential', delay: 1000 },\n })\n\n return job.id ?? jobData.id\n }\n\n async function process(handler: JobHandler<T>): Promise<ProcessResult> {\n const { Worker } = await getBullMQ()\n\n // Create worker that processes jobs\n bullWorker = new Worker<QueuedJob<T>>(\n name,\n async (job) => {\n const jobData = job.data\n await handler(jobData, {\n jobId: job.id ?? jobData.id,\n attemptNumber: job.attemptsMade + 1,\n queueName: name,\n })\n },\n {\n connection,\n concurrency,\n ...(lockDuration !== undefined ? { lockDuration } : {}),\n ...(maxStalledCount !== undefined ? { maxStalledCount } : {}),\n }\n )\n\n // Set up event handlers\n bullWorker.on('completed', (job) => {\n const jobWithId = job as { id?: string }\n logger.info('Job completed', { jobId: jobWithId.id })\n })\n\n bullWorker.on('failed', (job, err) => {\n const jobWithId = job as { id?: string } | undefined\n const error = err as Error\n logger.error('Job failed', { jobId: jobWithId?.id, err: error })\n })\n\n // A stalled job is redelivered under the same id while the previous worker\n // may still be running it, so this is the signal that a handler is about to\n // be executed twice. BullMQ's docs require surfacing it: without this line\n // duplicate processing is invisible.\n bullWorker.on('stalled', (jobId) => {\n logger.warn('Job stalled and will be redelivered \u2014 the handler may run concurrently with a previous delivery', {\n jobId: typeof jobId === 'string' ? jobId : null,\n })\n })\n\n bullWorker.on('error', (err) => {\n const error = err as Error\n logger.error('Worker error', { err: error })\n })\n\n logger.info('Worker started', { concurrency })\n\n // For async strategy, return a sentinel result indicating worker mode\n // processed=-1 signals that this is a continuous worker, not a batch process\n return { processed: -1, failed: -1, lastJobId: undefined }\n }\n\n async function clear(): Promise<{ removed: number }> {\n const queue = await getQueue()\n\n // Obliterate removes all jobs from the queue\n await queue.obliterate({ force: true })\n\n return { removed: -1 } // BullMQ obliterate doesn't return count\n }\n\n async function removeQueuedJobsByScope(scope: QueueJobScope): Promise<{ removed: number }> {\n const queue = await getQueue()\n const jobs = await queue.getJobs(REMOVABLE_JOB_STATES, 0, -1)\n let removed = 0\n\n for (const job of jobs) {\n if (!payloadMatchesScope(job.data?.payload, scope)) continue\n try {\n await job.remove()\n removed++\n } catch {\n // The job may have started between enumeration and removal. In-flight\n // cancellation is handled by the caller's lock/heartbeat contract.\n }\n }\n\n return { removed }\n }\n\n async function close(): Promise<void> {\n if (bullWorker) {\n await bullWorker.close()\n bullWorker = null\n }\n if (bullQueue) {\n await bullQueue.close()\n bullQueue = null\n }\n }\n\n async function getJobCounts(): Promise<{\n waiting: number\n active: number\n completed: number\n failed: number\n }> {\n const queue = await getQueue()\n const counts = await queue.getJobCounts('waiting', 'active', 'completed', 'failed')\n return {\n waiting: counts.waiting ?? 0,\n active: counts.active ?? 0,\n completed: counts.completed ?? 0,\n failed: counts.failed ?? 0,\n }\n }\n\n return {\n name,\n strategy: 'async',\n enqueue,\n process,\n clear,\n removeQueuedJobsByScope,\n close,\n getJobCounts,\n }\n}\n"],
5
- "mappings": "AACA,SAAS,0BAA0B;AACnC,SAAS,oBAAoB;AAE7B,MAAM,gBAAgB,aAAa,OAAO;AAiD1C,MAAM,uBAAuB,CAAC,WAAW,WAAW,eAAe,UAAU,kBAAkB;AAE/F,SAAS,oBAAoB,SAAkB,OAA+B;AAC5E,MAAI,CAAC,WAAW,OAAO,YAAY,SAAU,QAAO;AACpD,QAAM,gBAAgB;AACtB,MAAI,cAAc,aAAa,MAAM,SAAU,QAAO;AACtD,MAAI,MAAM,mBAAmB,QAAW;AACtC,SAAK,cAAc,kBAAkB,UAAU,MAAM,eAAgB,QAAO;AAAA,EAC9E;AACA,MAAI,MAAM,UAAU,QAAQ;AAC1B,WAAO,OAAO,cAAc,YAAY,YAAY,MAAM,SAAS,SAAS,cAAc,OAAO;AAAA,EACnG;AACA,SAAO;AACT;AASA,SAAS,kBAAkB,SAA8D;AACvF,MAAI,SAAS,KAAK;AAChB,WAAO,EAAE,KAAK,QAAQ,IAAI;AAAA,EAC5B;AAEA,MAAI,SAAS,MAAM;AACjB,WAAO;AAAA,MACL,MAAM,QAAQ;AAAA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,iBAAe,QAAsC;AACnD,UAAM,QAAQ,MAAM,SAAS;AAG7B,UAAM,MAAM,WAAW,EAAE,OAAO,KAAK,CAAC;AAEtC,WAAO,EAAE,SAAS,GAAG;AAAA,EACvB;AAEA,iBAAe,wBAAwB,OAAoD;AACzF,UAAM,QAAQ,MAAM,SAAS;AAC7B,UAAM,OAAO,MAAM,MAAM,QAAQ,sBAAsB,GAAG,EAAE;AAC5D,QAAI,UAAU;AAEd,eAAW,OAAO,MAAM;AACtB,UAAI,CAAC,oBAAoB,IAAI,MAAM,SAAS,KAAK,EAAG;AACpD,UAAI;AACF,cAAM,IAAI,OAAO;AACjB;AAAA,MACF,QAAQ;AAAA,MAGR;AAAA,IACF;AAEA,WAAO,EAAE,QAAQ;AAAA,EACnB;AAEA,iBAAe,QAAuB;AACpC,QAAI,YAAY;AACd,YAAM,WAAW,MAAM;AACvB,mBAAa;AAAA,IACf;AACA,QAAI,WAAW;AACb,YAAM,UAAU,MAAM;AACtB,kBAAY;AAAA,IACd;AAAA,EACF;AAEA,iBAAe,eAKZ;AACD,UAAM,QAAQ,MAAM,SAAS;AAC7B,UAAM,SAAS,MAAM,MAAM,aAAa,WAAW,UAAU,aAAa,QAAQ;AAClF,WAAO;AAAA,MACL,SAAS,OAAO,WAAW;AAAA,MAC3B,QAAQ,OAAO,UAAU;AAAA,MACzB,WAAW,OAAO,aAAa;AAAA,MAC/B,QAAQ,OAAO,UAAU;AAAA,IAC3B;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import type { Queue, QueuedJob, JobHandler, AsyncQueueOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'\nimport { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'\nimport { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'\nimport { attachTraceMetadata, runJobInTrace } from '../tracing'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst packageLogger = createLogger('queue')\n\n// BullMQ interface types - we define the shape we use to maintain type safety\n// while keeping bullmq as an optional peer dependency\ntype ConnectionOptions = {\n url?: string\n host?: string\n port?: number\n username?: string\n password?: string\n db?: number\n tls?: Record<string, unknown>\n}\n\ninterface BullQueueInterface<T> {\n add: (\n name: string,\n data: T,\n opts?: {\n removeOnComplete?: boolean\n removeOnFail?: number\n delay?: number\n attempts?: number\n backoff?: { type: string; delay: number }\n },\n ) => Promise<{ id?: string }>\n obliterate: (opts?: { force?: boolean }) => Promise<void>\n close: () => Promise<void>\n getJobCounts: (...states: string[]) => Promise<Record<string, number>>\n getJobs: (types: string[], start?: number, end?: number) => Promise<Array<{\n data?: T\n remove: () => Promise<void>\n }>>\n}\n\ninterface BullWorkerInterface {\n on: (event: string, handler: (...args: unknown[]) => void) => void\n close: () => Promise<void>\n}\n\ninterface BullMQModule {\n Queue: new <T>(name: string, opts: { connection: ConnectionOptions; telemetry?: unknown }) => BullQueueInterface<T>\n Worker: new <T>(\n name: string,\n processor: (job: { id?: string; data: T; attemptsMade: number }) => Promise<void>,\n opts: {\n connection: ConnectionOptions\n concurrency: number\n telemetry?: unknown\n lockDuration?: number\n maxStalledCount?: number\n }\n ) => BullWorkerInterface\n}\n\n/** The `bullmq-otel` package (optional). Loaded only when an OTLP backend is active. */\ntype BullMQOtelModule = { BullMQOtel: new (tracerName: string) => object }\n\nconst REMOVABLE_JOB_STATES = ['waiting', 'delayed', 'prioritized', 'paused', 'waiting-children']\n\nfunction payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {\n if (!payload || typeof payload !== 'object') return false\n const scopedPayload = payload as { tenantId?: unknown; organizationId?: unknown; jobType?: unknown }\n if (scopedPayload.tenantId !== scope.tenantId) return false\n if (scope.organizationId !== undefined) {\n if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false\n }\n if (scope.jobTypes?.length) {\n return typeof scopedPayload.jobType === 'string' && scope.jobTypes.includes(scopedPayload.jobType)\n }\n return true\n}\n\n/**\n * Resolves Redis connection options from various sources.\n *\n * BullMQ expects an ioredis-compatible connection object. Preserve the full\n * Redis URL under the `url` key so rediss://, username, database, and query\n * params are not lost in translation.\n */\nfunction resolveConnection(options?: AsyncQueueOptions['connection']): ConnectionOptions {\n if (options?.url) {\n return { url: options.url }\n }\n\n if (options?.host) {\n return {\n host: options.host,\n port: options.port ?? 6379,\n username: options.username,\n password: options.password,\n db: options.db,\n tls: options.tls,\n }\n }\n\n return { url: getRedisUrlOrThrow('QUEUE') }\n}\n\n/**\n * Creates a BullMQ-based async queue.\n *\n * This strategy provides:\n * - Persistent job storage in Redis\n * - Automatic retries with exponential backoff\n * - Concurrent job processing\n * - Job prioritization and scheduling\n *\n * @template T - The payload type for jobs\n * @param name - Queue name\n * @param options - Async queue options\n */\nexport function createAsyncQueue<T = unknown>(\n name: string,\n options?: AsyncQueueOptions\n): Queue<T> {\n const connection = resolveConnection(options?.connection)\n const concurrency = options?.concurrency ?? 1\n const attempts = options?.attempts ?? 3\n const lockDuration = options?.lockDuration\n const maxStalledCount = options?.maxStalledCount\n const logger = packageLogger.child({ queue: name })\n\n let bullQueue: BullQueueInterface<QueuedJob<T>> | null = null\n let bullWorker: BullWorkerInterface | null = null\n let bullmqModule: BullMQModule | null = null\n // Resolved once: a BullMQOtel instance (delegate async tracing to BullMQ) or\n // undefined (use our own metadata._trace carrier instead). Memoized as the\n // in-flight promise so concurrent first-time callers share one resolution.\n let telemetryPromise: Promise<object | undefined> | null = null\n\n // -------------------------------------------------------------------------\n // Lazy BullMQ initialization\n // -------------------------------------------------------------------------\n\n async function getBullMQ(): Promise<BullMQModule> {\n if (!bullmqModule) {\n try {\n bullmqModule = await import('bullmq') as unknown as BullMQModule\n } catch {\n throw new Error(\n 'BullMQ is required for async queue strategy. Install it with: npm install bullmq'\n )\n }\n }\n return bullmqModule\n }\n\n /**\n * When an OTLP backend is active, delegate async-queue tracing to `bullmq-otel`\n * (richer BullMQ-internal spans: add / process / wait / attempts). Returns\n * `undefined` \u2014 meaning \"use our own `metadata._trace` carrier\" \u2014 when telemetry\n * is off, a non-OTEL backend is selected, or `bullmq-otel` isn't installed. (The\n * `local` strategy always uses our carrier; it isn't BullMQ, so `bullmq-otel`\n * cannot instrument it.)\n */\n async function getQueueTelemetry(): Promise<object | undefined> {\n if (!telemetryPromise) {\n telemetryPromise = (async () => {\n if (!getTelemetryRuntime()?.canUseGlobalTracePropagation()) return undefined\n try {\n const mod = (await import('bullmq-otel')) as unknown as BullMQOtelModule\n return new mod.BullMQOtel('open-mercato')\n } catch {\n packageLogger.warn('bullmq-otel not available; using built-in trace carrier', { queue: name })\n return undefined\n }\n })()\n }\n return telemetryPromise\n }\n\n async function getQueue(): Promise<BullQueueInterface<QueuedJob<T>>> {\n if (!bullQueue) {\n const { Queue: BullQueueClass } = await getBullMQ()\n const telemetry = await getQueueTelemetry()\n bullQueue = new BullQueueClass<QueuedJob<T>>(name, { connection, ...(telemetry ? { telemetry } : {}) })\n }\n return bullQueue\n }\n\n // -------------------------------------------------------------------------\n // Queue Implementation\n // -------------------------------------------------------------------------\n\n async function enqueue(data: T, options?: EnqueueOptions): Promise<string> {\n const queue = await getQueue()\n // When bullmq-otel handles propagation, don't also attach our carrier.\n const telemetry = await getQueueTelemetry()\n const metadata = telemetry ? undefined : attachTraceMetadata(undefined)\n const jobData: QueuedJob<T> = {\n id: crypto.randomUUID(),\n payload: data,\n createdAt: new Date().toISOString(),\n ...(metadata ? { metadata } : {}),\n }\n\n const job = await queue.add(jobData.id, jobData, {\n delay: options?.delayMs && options.delayMs > 0 ? options.delayMs : undefined,\n removeOnComplete: true,\n removeOnFail: 1000,\n attempts,\n backoff: { type: 'exponential', delay: 1000 },\n })\n\n return job.id ?? jobData.id\n }\n\n async function process(handler: JobHandler<T>): Promise<ProcessResult> {\n const { Worker } = await getBullMQ()\n const telemetry = await getQueueTelemetry()\n\n // Create worker that processes jobs\n bullWorker = new Worker<QueuedJob<T>>(\n name,\n async (job) => {\n const jobData = job.data\n const ctx = {\n jobId: job.id ?? jobData.id,\n attemptNumber: job.attemptsMade + 1,\n queueName: name,\n }\n // With bullmq-otel active, BullMQ owns the process span and active\n // context (the handler's pg/undici spans nest under it). Otherwise\n // continue the trace from our own carrier.\n if (telemetry) {\n await handler(jobData, ctx)\n } else {\n await runJobInTrace(name, jobData.metadata, () => handler(jobData, ctx))\n }\n },\n {\n connection,\n concurrency,\n ...(telemetry ? { telemetry } : {}),\n ...(lockDuration !== undefined ? { lockDuration } : {}),\n ...(maxStalledCount !== undefined ? { maxStalledCount } : {}),\n }\n )\n\n // Set up event handlers\n bullWorker.on('completed', (job) => {\n const jobWithId = job as { id?: string }\n logger.info('Job completed', { jobId: jobWithId.id })\n })\n\n bullWorker.on('failed', (job, err) => {\n const jobWithId = job as { id?: string } | undefined\n const error = err as Error\n logger.error('Job failed', { jobId: jobWithId?.id, err: error })\n })\n\n // A stalled job is redelivered under the same id while the previous worker\n // may still be running it, so this is the signal that a handler is about to\n // be executed twice. BullMQ's docs require surfacing it: without this line\n // duplicate processing is invisible.\n bullWorker.on('stalled', (jobId) => {\n logger.warn('Job stalled and will be redelivered \u2014 the handler may run concurrently with a previous delivery', {\n jobId: typeof jobId === 'string' ? jobId : null,\n })\n })\n\n bullWorker.on('error', (err) => {\n const error = err as Error\n logger.error('Worker error', { err: error })\n })\n\n logger.info('Worker started', { concurrency })\n\n // For async strategy, return a sentinel result indicating worker mode\n // processed=-1 signals that this is a continuous worker, not a batch process\n return { processed: -1, failed: -1, lastJobId: undefined }\n }\n\n async function clear(): Promise<{ removed: number }> {\n const queue = await getQueue()\n\n // Obliterate removes all jobs from the queue\n await queue.obliterate({ force: true })\n\n return { removed: -1 } // BullMQ obliterate doesn't return count\n }\n\n async function removeQueuedJobsByScope(scope: QueueJobScope): Promise<{ removed: number }> {\n const queue = await getQueue()\n const jobs = await queue.getJobs(REMOVABLE_JOB_STATES, 0, -1)\n let removed = 0\n\n for (const job of jobs) {\n if (!payloadMatchesScope(job.data?.payload, scope)) continue\n try {\n await job.remove()\n removed++\n } catch {\n // The job may have started between enumeration and removal. In-flight\n // cancellation is handled by the caller's lock/heartbeat contract.\n }\n }\n\n return { removed }\n }\n\n async function close(): Promise<void> {\n if (bullWorker) {\n await bullWorker.close()\n bullWorker = null\n }\n if (bullQueue) {\n await bullQueue.close()\n bullQueue = null\n }\n }\n\n async function getJobCounts(): Promise<{\n waiting: number\n active: number\n completed: number\n failed: number\n }> {\n const queue = await getQueue()\n const counts = await queue.getJobCounts('waiting', 'active', 'completed', 'failed')\n return {\n waiting: counts.waiting ?? 0,\n active: counts.active ?? 0,\n completed: counts.completed ?? 0,\n failed: counts.failed ?? 0,\n }\n }\n\n return {\n name,\n strategy: 'async',\n enqueue,\n process,\n clear,\n removeQueuedJobsByScope,\n close,\n getJobCounts,\n }\n}\n"],
5
+ "mappings": "AACA,SAAS,0BAA0B;AACnC,SAAS,2BAA2B;AACpC,SAAS,qBAAqB,qBAAqB;AACnD,SAAS,oBAAoB;AAE7B,MAAM,gBAAgB,aAAa,OAAO;AA0D1C,MAAM,uBAAuB,CAAC,WAAW,WAAW,eAAe,UAAU,kBAAkB;AAE/F,SAAS,oBAAoB,SAAkB,OAA+B;AAC5E,MAAI,CAAC,WAAW,OAAO,YAAY,SAAU,QAAO;AACpD,QAAM,gBAAgB;AACtB,MAAI,cAAc,aAAa,MAAM,SAAU,QAAO;AACtD,MAAI,MAAM,mBAAmB,QAAW;AACtC,SAAK,cAAc,kBAAkB,UAAU,MAAM,eAAgB,QAAO;AAAA,EAC9E;AACA,MAAI,MAAM,UAAU,QAAQ;AAC1B,WAAO,OAAO,cAAc,YAAY,YAAY,MAAM,SAAS,SAAS,cAAc,OAAO;AAAA,EACnG;AACA,SAAO;AACT;AASA,SAAS,kBAAkB,SAA8D;AACvF,MAAI,SAAS,KAAK;AAChB,WAAO,EAAE,KAAK,QAAQ,IAAI;A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pD,UAAI;AACF,cAAM,IAAI,OAAO;AACjB;AAAA,MACF,QAAQ;AAAA,MAGR;AAAA,IACF;AAEA,WAAO,EAAE,QAAQ;AAAA,EACnB;AAEA,iBAAe,QAAuB;AACpC,QAAI,YAAY;AACd,YAAM,WAAW,MAAM;AACvB,mBAAa;AAAA,IACf;AACA,QAAI,WAAW;AACb,YAAM,UAAU,MAAM;AACtB,kBAAY;AAAA,IACd;AAAA,EACF;AAEA,iBAAe,eAKZ;AACD,UAAM,QAAQ,MAAM,SAAS;AAC7B,UAAM,SAAS,MAAM,MAAM,aAAa,WAAW,UAAU,aAAa,QAAQ;AAClF,WAAO;AAAA,MACL,SAAS,OAAO,WAAW;AAAA,MAC3B,QAAQ,OAAO,UAAU;AAAA,MACzB,WAAW,OAAO,aAAa;AAAA,MAC/B,QAAQ,OAAO,UAAU;AAAA,IAC3B;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
6
6
  "names": ["options"]
7
7
  }
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import crypto from "node:crypto";
4
4
  import { createLogger } from "@open-mercato/shared/lib/logger";
5
+ import { attachTraceMetadata, runJobInTrace } from "../tracing.js";
5
6
  const packageLogger = createLogger("queue");
6
7
  function payloadMatchesScope(payload, scope) {
7
8
  if (!payload || typeof payload !== "object") return false;
@@ -118,11 +119,13 @@ function createLocalQueue(name, options) {
118
119
  }
119
120
  async function enqueue(data, options2) {
120
121
  const availableAt = options2?.delayMs && options2.delayMs > 0 ? new Date(Date.now() + options2.delayMs).toISOString() : void 0;
122
+ const metadata = attachTraceMetadata(void 0);
121
123
  const job = {
122
124
  id: generateId(),
123
125
  payload: data,
124
126
  createdAt: (/* @__PURE__ */ new Date()).toISOString(),
125
- ...availableAt ? { availableAt } : {}
127
+ ...availableAt ? { availableAt } : {},
128
+ ...metadata ? { metadata } : {}
126
129
  };
127
130
  await withFileLock(async () => {
128
131
  const jobs = await readQueue();
@@ -155,12 +158,16 @@ function createLocalQueue(name, options) {
155
158
  for (const job of jobsToProcess) {
156
159
  const attemptNumber = (job.attemptCount ?? 0) + 1;
157
160
  try {
158
- await Promise.resolve(
159
- handler(job, {
160
- jobId: job.id,
161
- attemptNumber,
162
- queueName: name
163
- })
161
+ await runJobInTrace(
162
+ name,
163
+ job.metadata,
164
+ () => Promise.resolve(
165
+ handler(job, {
166
+ jobId: job.id,
167
+ attemptNumber,
168
+ queueName: name
169
+ })
170
+ )
164
171
  );
165
172
  processed++;
166
173
  lastJobId = job.id;
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/strategies/local.ts"],
4
- "sourcesContent": ["import fs from 'node:fs'\nimport path from 'node:path'\nimport crypto from 'node:crypto'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'\n\nconst packageLogger = createLogger('queue')\n\ntype LocalState = {\n lastProcessedId?: string\n completedCount?: number\n failedCount?: number\n}\n\ntype StoredJob<T> = QueuedJob<T> & {\n availableAt?: string\n attemptCount?: number\n}\n\nfunction payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {\n if (!payload || typeof payload !== 'object') return false\n const scopedPayload = payload as { tenantId?: unknown; organizationId?: unknown; jobType?: unknown }\n if (scopedPayload.tenantId !== scope.tenantId) return false\n if (scope.organizationId !== undefined) {\n if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false\n }\n if (scope.jobTypes?.length) {\n return typeof scopedPayload.jobType === 'string' && scope.jobTypes.includes(scopedPayload.jobType)\n }\n return true\n}\n\n/** Default polling interval in milliseconds */\nconst DEFAULT_POLL_INTERVAL = 1000\nconst DEFAULT_LOCAL_QUEUE_BASE_DIR = '.mercato/queue'\nconst DEFAULT_MAX_ATTEMPTS = 3\nconst RETRY_BACKOFF_BASE_MS = 1000\n\nconst fsp = fs.promises\n\n/**\n * Creates a file-based local queue.\n *\n * Jobs are stored in JSON files within a directory structure:\n * - `.mercato/queue/<name>/queue.json` - Array of queued jobs\n * - `.mercato/queue/<name>/state.json` - Processing state (last processed ID)\n *\n * **Limitations:**\n * - Jobs are processed sequentially (concurrency option is for logging/compatibility only)\n * - Not suitable for production or multi-process environments\n *\n * Failed jobs are retried up to `DEFAULT_MAX_ATTEMPTS` times with exponential backoff.\n * **This strategy keeps no failed-job store**: once attempts are exhausted the job is\n * removed from `queue.json` and only counted in `state.failedCount`, so the payload is\n * lost and the failure survives solely as an error log line. The `async` strategy keeps\n * only a bounded inspection window: `removeOnFail: 1000` retains the most recent 1000\n * failures and removes older ones as later failures arrive. Workflows that require\n * no-loss persistence must write their own durable record before enqueueing, regardless\n * of strategy.\n *\n * `DEFAULT_MAX_ATTEMPTS` is a module constant, not a per-job option \u2014 callers cannot\n * request a different attempt count. (`async` likewise hard-codes `attempts: 3`.)\n * See the retry handling in `process()` below.\n *\n * All file I/O is asynchronous (`fs.promises.*`) so queue operations do not\n * block the Node.js event loop. A per-queue promise chain serializes\n * read-modify-write sequences to preserve the atomicity guarantees the\n * previous synchronous implementation relied on.\n *\n * @template T - The payload type for jobs\n * @param name - Queue name (used for directory naming)\n * @param options - Local queue options\n */\nexport function createLocalQueue<T = unknown>(\n name: string,\n options?: LocalQueueOptions\n): Queue<T> {\n const nodeProcess = (globalThis as typeof globalThis & { process?: NodeJS.Process }).process\n const queueBaseDirFromEnv = nodeProcess?.env?.QUEUE_BASE_DIR\n const baseDir = options?.baseDir\n ?? path.resolve(queueBaseDirFromEnv || DEFAULT_LOCAL_QUEUE_BASE_DIR)\n const queueDir = path.join(baseDir, name)\n const queueFile = path.join(queueDir, 'queue.json')\n const stateFile = path.join(queueDir, 'state.json')\n const logger = packageLogger.child({ queue: name })\n // Note: concurrency is stored for logging/compatibility but jobs are processed sequentially\n const concurrency = options?.concurrency ?? 1\n const pollInterval = options?.pollInterval ?? DEFAULT_POLL_INTERVAL\n\n // Worker state for continuous polling\n let pollingTimer: ReturnType<typeof setInterval> | null = null\n let isProcessing = false\n let activeHandler: JobHandler<T> | null = null\n const inFlightJobIds = new Set<string>()\n\n // Per-queue mutex. Serializes read-modify-write segments so async fs calls\n // cannot interleave and clobber each other's writes.\n let fileOpChain: Promise<unknown> = Promise.resolve()\n function withFileLock<R>(fn: () => Promise<R>): Promise<R> {\n const run = fileOpChain.then(() => fn(), () => fn())\n fileOpChain = run.then(\n () => undefined,\n () => undefined,\n )\n return run\n }\n\n // -------------------------------------------------------------------------\n // File Operations\n // -------------------------------------------------------------------------\n\n async function ensureDir(): Promise<void> {\n try {\n await fsp.mkdir(queueDir, { recursive: true })\n } catch (e: unknown) {\n const error = e as NodeJS.ErrnoException\n if (error.code !== 'EEXIST') throw error\n }\n\n // Initialize queue file with exclusive create flag\n try {\n await fsp.writeFile(queueFile, '[]', { encoding: 'utf8', flag: 'wx' })\n } catch (e: unknown) {\n const error = e as NodeJS.ErrnoException\n if (error.code !== 'EEXIST') throw error\n }\n\n // Initialize state file with exclusive create flag\n try {\n await fsp.writeFile(stateFile, '{}', { encoding: 'utf8', flag: 'wx' })\n } catch (e: unknown) {\n const error = e as NodeJS.ErrnoException\n if (error.code !== 'EEXIST') throw error\n }\n }\n\n async function backupCorruptedQueueFile(content: string): Promise<string> {\n const backupFile = path.join(queueDir, `queue.corrupted.${Date.now()}.json`)\n await fsp.writeFile(backupFile, content, 'utf8')\n await fsp.writeFile(queueFile, '[]', 'utf8')\n return backupFile\n }\n\n async function readQueue(): Promise<StoredJob<T>[]> {\n await ensureDir()\n let content: string\n\n try {\n content = await fsp.readFile(queueFile, 'utf8')\n } catch (error: unknown) {\n const readError = error as NodeJS.ErrnoException\n if (readError.code === 'ENOENT') {\n return []\n }\n logger.error('Failed to read queue file', { err: readError })\n throw new Error(`Queue file unreadable: ${readError.message}`)\n }\n\n try {\n const parsed = JSON.parse(content) as unknown\n\n if (!Array.isArray(parsed)) {\n throw new Error('Queue file must contain a JSON array')\n }\n\n return parsed as StoredJob<T>[]\n } catch (error: unknown) {\n const parseError = error as Error\n logger.error('Failed to parse queue file', { err: parseError })\n const backupFile = await backupCorruptedQueueFile(content)\n logger.error('Backed up corrupted queue file and recreated queue.json', { backupFile })\n return []\n }\n }\n\n async function writeQueue(jobs: StoredJob<T>[]): Promise<void> {\n await ensureDir()\n await fsp.writeFile(queueFile, JSON.stringify(jobs, null, 2), 'utf8')\n }\n\n async function readState(): Promise<LocalState> {\n await ensureDir()\n try {\n const content = await fsp.readFile(stateFile, 'utf8')\n return JSON.parse(content) as LocalState\n } catch {\n return {}\n }\n }\n\n async function writeState(state: LocalState): Promise<void> {\n await ensureDir()\n await fsp.writeFile(stateFile, JSON.stringify(state, null, 2), 'utf8')\n }\n\n function generateId(): string {\n return crypto.randomUUID()\n }\n\n // -------------------------------------------------------------------------\n // Queue Implementation\n // -------------------------------------------------------------------------\n\n async function enqueue(data: T, options?: EnqueueOptions): Promise<string> {\n const availableAt = options?.delayMs && options.delayMs > 0\n ? new Date(Date.now() + options.delayMs).toISOString()\n : undefined\n const job: StoredJob<T> = {\n id: generateId(),\n payload: data,\n createdAt: new Date().toISOString(),\n ...(availableAt ? { availableAt } : {}),\n }\n await withFileLock(async () => {\n const jobs = await readQueue()\n jobs.push(job)\n await writeQueue(jobs)\n })\n return job.id\n }\n\n /**\n * Process pending jobs in a single batch (internal helper).\n */\n async function processBatch(\n handler: JobHandler<T>,\n options?: ProcessOptions\n ): Promise<ProcessResult> {\n const { state, jobs } = await withFileLock(async () => {\n const stateRead = await readState()\n const jobsRead = await readQueue()\n return { state: stateRead, jobs: jobsRead }\n })\n\n const pendingJobs = jobs.filter((job) => {\n if (!job.availableAt) return true\n return new Date(job.availableAt).getTime() <= Date.now()\n })\n const jobsToProcess = options?.limit\n ? pendingJobs.slice(0, options.limit)\n : pendingJobs\n\n for (const job of jobsToProcess) {\n inFlightJobIds.add(job.id)\n }\n\n let processed = 0\n let failed = 0\n let lastJobId: string | undefined\n const completedJobIds = new Set<string>()\n const deadJobIds = new Set<string>()\n const retryUpdates = new Map<string, StoredJob<T>>()\n\n try {\n for (const job of jobsToProcess) {\n const attemptNumber = (job.attemptCount ?? 0) + 1\n try {\n await Promise.resolve(\n handler(job, {\n jobId: job.id,\n attemptNumber,\n queueName: name,\n })\n )\n processed++\n lastJobId = job.id\n completedJobIds.add(job.id)\n logger.info('Job completed', { jobId: job.id })\n } catch (error) {\n logger.error('Job failed', { jobId: job.id, attemptNumber, maxAttempts: DEFAULT_MAX_ATTEMPTS, err: error })\n failed++\n lastJobId = job.id\n if (attemptNumber >= DEFAULT_MAX_ATTEMPTS) {\n logger.error('Job exhausted all attempts; dropping it (no dead-letter store)', { jobId: job.id, maxAttempts: DEFAULT_MAX_ATTEMPTS })\n deadJobIds.add(job.id)\n } else {\n const backoffMs = RETRY_BACKOFF_BASE_MS * Math.pow(2, attemptNumber - 1)\n retryUpdates.set(job.id, {\n ...job,\n attemptCount: attemptNumber,\n availableAt: new Date(Date.now() + backoffMs).toISOString(),\n })\n }\n }\n }\n\n const hasChanges = completedJobIds.size > 0 || deadJobIds.size > 0 || retryUpdates.size > 0\n if (hasChanges) {\n await withFileLock(async () => {\n // Re-read so jobs enqueued during handler execution are preserved.\n const currentJobs = await readQueue()\n const updatedJobs = currentJobs\n .filter((j) => !completedJobIds.has(j.id) && !deadJobIds.has(j.id))\n .map((j) => retryUpdates.get(j.id) ?? j)\n await writeQueue(updatedJobs)\n\n const newState: LocalState = {\n lastProcessedId: lastJobId,\n completedCount: (state.completedCount ?? 0) + processed,\n failedCount: (state.failedCount ?? 0) + deadJobIds.size,\n }\n await writeState(newState)\n })\n }\n\n return { processed, failed, lastJobId }\n } finally {\n for (const job of jobsToProcess) {\n inFlightJobIds.delete(job.id)\n }\n }\n }\n\n /**\n * Poll for and process new jobs.\n */\n async function pollAndProcess(): Promise<void> {\n // Skip if already processing to avoid concurrent file access\n if (isProcessing || !activeHandler) return\n\n isProcessing = true\n try {\n await processBatch(activeHandler)\n } catch (error) {\n logger.error('Polling error', { err: error })\n } finally {\n isProcessing = false\n }\n }\n\n async function process(\n handler: JobHandler<T>,\n options?: ProcessOptions\n ): Promise<ProcessResult> {\n // If limit is specified, do a single batch (backward compatibility)\n if (options?.limit) {\n return processBatch(handler, options)\n }\n\n // Start continuous polling mode (like BullMQ Worker)\n activeHandler = handler\n\n // Process any pending jobs immediately\n await processBatch(handler)\n\n // Start polling interval for new jobs\n pollingTimer = setInterval(() => {\n pollAndProcess().catch((err) => {\n logger.error('Poll cycle error', { err })\n })\n }, pollInterval)\n\n logger.info('Worker started', { concurrency })\n\n // Return sentinel value indicating continuous worker mode (like async strategy)\n return { processed: -1, failed: -1, lastJobId: undefined }\n }\n\n async function clear(): Promise<{ removed: number }> {\n return withFileLock(async () => {\n const jobs = await readQueue()\n const removed = jobs.length\n await writeQueue([])\n // Reset state but preserve counts for historical tracking\n const state = await readState()\n await writeState({\n completedCount: state.completedCount,\n failedCount: state.failedCount,\n })\n return { removed }\n })\n }\n\n async function removeQueuedJobsByScope(scope: QueueJobScope): Promise<{ removed: number }> {\n return withFileLock(async () => {\n const jobs = await readQueue()\n const retainedJobs = jobs.filter((job) => inFlightJobIds.has(job.id) || !payloadMatchesScope(job.payload, scope))\n const removed = jobs.length - retainedJobs.length\n if (removed > 0) {\n await writeQueue(retainedJobs)\n }\n return { removed }\n })\n }\n\n async function close(): Promise<void> {\n // Stop polling timer\n if (pollingTimer) {\n clearInterval(pollingTimer)\n pollingTimer = null\n }\n activeHandler = null\n\n // Wait for any in-progress processing to complete (with timeout)\n const SHUTDOWN_TIMEOUT = 5000\n const startTime = Date.now()\n\n while (isProcessing) {\n if (Date.now() - startTime > SHUTDOWN_TIMEOUT) {\n logger.warn('Force closing after shutdown timeout', { timeoutMs: SHUTDOWN_TIMEOUT })\n break\n }\n await new Promise((resolve) => setTimeout(resolve, 50))\n }\n }\n\n async function getJobCounts(): Promise<{\n waiting: number\n active: number\n completed: number\n failed: number\n }> {\n return withFileLock(async () => {\n const state = await readState()\n const jobs = await readQueue()\n\n return {\n waiting: jobs.length, // All jobs in queue are waiting (processed ones are removed)\n active: 0, // Local strategy doesn't track active jobs\n completed: state.completedCount ?? 0,\n failed: state.failedCount ?? 0,\n }\n })\n }\n\n return {\n name,\n strategy: 'local',\n enqueue,\n process,\n clear,\n removeQueuedJobsByScope,\n close,\n getJobCounts,\n }\n}\n"],
5
- "mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,YAAY;AACnB,SAAS,oBAAoB;AAG7B,MAAM,gBAAgB,aAAa,OAAO;AAa1C,SAAS,oBAAoB,SAAkB,OAA+B;AAC5E,MAAI,CAAC,WAAW,OAAO,YAAY,SAAU,QAAO;AACpD,QAAM,gBAAgB;AACtB,MAAI,cAAc,aAAa,MAAM,SAAU,QAAO;AACtD,MAAI,MAAM,mBAAmB,QAAW;AACtC,SAAK,cAAc,kBAAkB,UAAU,MAAM,eAAgB,QAAO;AAAA,EAC9E;AACA,MAAI,MAAM,UAAU,QAAQ;AAC1B,WAAO,OAAO,cAAc,YAAY,YAAY,MAAM,SAAS,SAAS,cAAc,OAAO;AAAA,EACnG;AACA,SAAO;AACT;AAGA,MAAM,wBAAwB;AAC9B,MAAM,+BAA+B;AACrC,MAAM,uBAAuB;AAC7B,MAAM,wBAAwB;AAE9B,MAAM,MAAM,GAAG;AAmCR,SAAS,iBACd,MACA,SACU;AACV,QAAM,cAAe,WAAgE;AACrF,QAAM,sBAAsB,aAAa,KAAK;AAC9C,QAAM,UAAU,SAAS,WACpB,KAAK,QAAQ,uBAAuB,4BAA4B;AACrE,QAAM,WAAW,KAAK,KAAK,SAAS,IAAI;AACxC,QAAM,YAAY,KAAK,KAAK,UAAU,YAAY;AAClD,QAAM,YAAY,KAAK,KAAK,UAAU,YAAY;AAClD,QAAM,SAAS,cAA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,KAAK,SAAS,aAAa;AAC3C,UAAI,UAAU,GAAG;AACf,cAAM,WAAW,YAAY;AAAA,MAC/B;AACA,aAAO,EAAE,QAAQ;AAAA,IACnB,CAAC;AAAA,EACH;AAEA,iBAAe,QAAuB;AAEpC,QAAI,cAAc;AAChB,oBAAc,YAAY;AAC1B,qBAAe;AAAA,IACjB;AACA,oBAAgB;AAGhB,UAAM,mBAAmB;AACzB,UAAM,YAAY,KAAK,IAAI;AAE3B,WAAO,cAAc;AACnB,UAAI,KAAK,IAAI,IAAI,YAAY,kBAAkB;AAC7C,eAAO,KAAK,wCAAwC,EAAE,WAAW,iBAAiB,CAAC;AACnF;AAAA,MACF;AACA,YAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AAAA,IACxD;AAAA,EACF;AAEA,iBAAe,eAKZ;AACD,WAAO,aAAa,YAAY;AAC9B,YAAM,QAAQ,MAAM,UAAU;AAC9B,YAAM,OAAO,MAAM,UAAU;AAE7B,aAAO;AAAA,QACL,SAAS,KAAK;AAAA;AAAA,QACd,QAAQ;AAAA;AAAA,QACR,WAAW,MAAM,kBAAkB;AAAA,QACnC,QAAQ,MAAM,eAAe;AAAA,MAC/B;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import fs from 'node:fs'\nimport path from 'node:path'\nimport crypto from 'node:crypto'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'\nimport { attachTraceMetadata, runJobInTrace } from '../tracing'\n\nconst packageLogger = createLogger('queue')\n\ntype LocalState = {\n lastProcessedId?: string\n completedCount?: number\n failedCount?: number\n}\n\ntype StoredJob<T> = QueuedJob<T> & {\n availableAt?: string\n attemptCount?: number\n}\n\nfunction payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {\n if (!payload || typeof payload !== 'object') return false\n const scopedPayload = payload as { tenantId?: unknown; organizationId?: unknown; jobType?: unknown }\n if (scopedPayload.tenantId !== scope.tenantId) return false\n if (scope.organizationId !== undefined) {\n if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false\n }\n if (scope.jobTypes?.length) {\n return typeof scopedPayload.jobType === 'string' && scope.jobTypes.includes(scopedPayload.jobType)\n }\n return true\n}\n\n/** Default polling interval in milliseconds */\nconst DEFAULT_POLL_INTERVAL = 1000\nconst DEFAULT_LOCAL_QUEUE_BASE_DIR = '.mercato/queue'\nconst DEFAULT_MAX_ATTEMPTS = 3\nconst RETRY_BACKOFF_BASE_MS = 1000\n\nconst fsp = fs.promises\n\n/**\n * Creates a file-based local queue.\n *\n * Jobs are stored in JSON files within a directory structure:\n * - `.mercato/queue/<name>/queue.json` - Array of queued jobs\n * - `.mercato/queue/<name>/state.json` - Processing state (last processed ID)\n *\n * **Limitations:**\n * - Jobs are processed sequentially (concurrency option is for logging/compatibility only)\n * - Not suitable for production or multi-process environments\n *\n * Failed jobs are retried up to `DEFAULT_MAX_ATTEMPTS` times with exponential backoff.\n * **This strategy keeps no failed-job store**: once attempts are exhausted the job is\n * removed from `queue.json` and only counted in `state.failedCount`, so the payload is\n * lost and the failure survives solely as an error log line. The `async` strategy keeps\n * only a bounded inspection window: `removeOnFail: 1000` retains the most recent 1000\n * failures and removes older ones as later failures arrive. Workflows that require\n * no-loss persistence must write their own durable record before enqueueing, regardless\n * of strategy.\n *\n * `DEFAULT_MAX_ATTEMPTS` is a module constant, not a per-job option \u2014 callers cannot\n * request a different attempt count. (`async` likewise hard-codes `attempts: 3`.)\n * See the retry handling in `process()` below.\n *\n * All file I/O is asynchronous (`fs.promises.*`) so queue operations do not\n * block the Node.js event loop. A per-queue promise chain serializes\n * read-modify-write sequences to preserve the atomicity guarantees the\n * previous synchronous implementation relied on.\n *\n * @template T - The payload type for jobs\n * @param name - Queue name (used for directory naming)\n * @param options - Local queue options\n */\nexport function createLocalQueue<T = unknown>(\n name: string,\n options?: LocalQueueOptions\n): Queue<T> {\n const nodeProcess = (globalThis as typeof globalThis & { process?: NodeJS.Process }).process\n const queueBaseDirFromEnv = nodeProcess?.env?.QUEUE_BASE_DIR\n const baseDir = options?.baseDir\n ?? path.resolve(queueBaseDirFromEnv || DEFAULT_LOCAL_QUEUE_BASE_DIR)\n const queueDir = path.join(baseDir, name)\n const queueFile = path.join(queueDir, 'queue.json')\n const stateFile = path.join(queueDir, 'state.json')\n const logger = packageLogger.child({ queue: name })\n // Note: concurrency is stored for logging/compatibility but jobs are processed sequentially\n const concurrency = options?.concurrency ?? 1\n const pollInterval = options?.pollInterval ?? DEFAULT_POLL_INTERVAL\n\n // Worker state for continuous polling\n let pollingTimer: ReturnType<typeof setInterval> | null = null\n let isProcessing = false\n let activeHandler: JobHandler<T> | null = null\n const inFlightJobIds = new Set<string>()\n\n // Per-queue mutex. Serializes read-modify-write segments so async fs calls\n // cannot interleave and clobber each other's writes.\n let fileOpChain: Promise<unknown> = Promise.resolve()\n function withFileLock<R>(fn: () => Promise<R>): Promise<R> {\n const run = fileOpChain.then(() => fn(), () => fn())\n fileOpChain = run.then(\n () => undefined,\n () => undefined,\n )\n return run\n }\n\n // -------------------------------------------------------------------------\n // File Operations\n // -------------------------------------------------------------------------\n\n async function ensureDir(): Promise<void> {\n try {\n await fsp.mkdir(queueDir, { recursive: true })\n } catch (e: unknown) {\n const error = e as NodeJS.ErrnoException\n if (error.code !== 'EEXIST') throw error\n }\n\n // Initialize queue file with exclusive create flag\n try {\n await fsp.writeFile(queueFile, '[]', { encoding: 'utf8', flag: 'wx' })\n } catch (e: unknown) {\n const error = e as NodeJS.ErrnoException\n if (error.code !== 'EEXIST') throw error\n }\n\n // Initialize state file with exclusive create flag\n try {\n await fsp.writeFile(stateFile, '{}', { encoding: 'utf8', flag: 'wx' })\n } catch (e: unknown) {\n const error = e as NodeJS.ErrnoException\n if (error.code !== 'EEXIST') throw error\n }\n }\n\n async function backupCorruptedQueueFile(content: string): Promise<string> {\n const backupFile = path.join(queueDir, `queue.corrupted.${Date.now()}.json`)\n await fsp.writeFile(backupFile, content, 'utf8')\n await fsp.writeFile(queueFile, '[]', 'utf8')\n return backupFile\n }\n\n async function readQueue(): Promise<StoredJob<T>[]> {\n await ensureDir()\n let content: string\n\n try {\n content = await fsp.readFile(queueFile, 'utf8')\n } catch (error: unknown) {\n const readError = error as NodeJS.ErrnoException\n if (readError.code === 'ENOENT') {\n return []\n }\n logger.error('Failed to read queue file', { err: readError })\n throw new Error(`Queue file unreadable: ${readError.message}`)\n }\n\n try {\n const parsed = JSON.parse(content) as unknown\n\n if (!Array.isArray(parsed)) {\n throw new Error('Queue file must contain a JSON array')\n }\n\n return parsed as StoredJob<T>[]\n } catch (error: unknown) {\n const parseError = error as Error\n logger.error('Failed to parse queue file', { err: parseError })\n const backupFile = await backupCorruptedQueueFile(content)\n logger.error('Backed up corrupted queue file and recreated queue.json', { backupFile })\n return []\n }\n }\n\n async function writeQueue(jobs: StoredJob<T>[]): Promise<void> {\n await ensureDir()\n await fsp.writeFile(queueFile, JSON.stringify(jobs, null, 2), 'utf8')\n }\n\n async function readState(): Promise<LocalState> {\n await ensureDir()\n try {\n const content = await fsp.readFile(stateFile, 'utf8')\n return JSON.parse(content) as LocalState\n } catch {\n return {}\n }\n }\n\n async function writeState(state: LocalState): Promise<void> {\n await ensureDir()\n await fsp.writeFile(stateFile, JSON.stringify(state, null, 2), 'utf8')\n }\n\n function generateId(): string {\n return crypto.randomUUID()\n }\n\n // -------------------------------------------------------------------------\n // Queue Implementation\n // -------------------------------------------------------------------------\n\n async function enqueue(data: T, options?: EnqueueOptions): Promise<string> {\n const availableAt = options?.delayMs && options.delayMs > 0\n ? new Date(Date.now() + options.delayMs).toISOString()\n : undefined\n const metadata = attachTraceMetadata(undefined)\n const job: StoredJob<T> = {\n id: generateId(),\n payload: data,\n createdAt: new Date().toISOString(),\n ...(availableAt ? { availableAt } : {}),\n ...(metadata ? { metadata } : {}),\n }\n await withFileLock(async () => {\n const jobs = await readQueue()\n jobs.push(job)\n await writeQueue(jobs)\n })\n return job.id\n }\n\n /**\n * Process pending jobs in a single batch (internal helper).\n */\n async function processBatch(\n handler: JobHandler<T>,\n options?: ProcessOptions\n ): Promise<ProcessResult> {\n const { state, jobs } = await withFileLock(async () => {\n const stateRead = await readState()\n const jobsRead = await readQueue()\n return { state: stateRead, jobs: jobsRead }\n })\n\n const pendingJobs = jobs.filter((job) => {\n if (!job.availableAt) return true\n return new Date(job.availableAt).getTime() <= Date.now()\n })\n const jobsToProcess = options?.limit\n ? pendingJobs.slice(0, options.limit)\n : pendingJobs\n\n for (const job of jobsToProcess) {\n inFlightJobIds.add(job.id)\n }\n\n let processed = 0\n let failed = 0\n let lastJobId: string | undefined\n const completedJobIds = new Set<string>()\n const deadJobIds = new Set<string>()\n const retryUpdates = new Map<string, StoredJob<T>>()\n\n try {\n for (const job of jobsToProcess) {\n const attemptNumber = (job.attemptCount ?? 0) + 1\n try {\n await runJobInTrace(name, job.metadata, () =>\n Promise.resolve(\n handler(job, {\n jobId: job.id,\n attemptNumber,\n queueName: name,\n })\n )\n )\n processed++\n lastJobId = job.id\n completedJobIds.add(job.id)\n logger.info('Job completed', { jobId: job.id })\n } catch (error) {\n logger.error('Job failed', { jobId: job.id, attemptNumber, maxAttempts: DEFAULT_MAX_ATTEMPTS, err: error })\n failed++\n lastJobId = job.id\n if (attemptNumber >= DEFAULT_MAX_ATTEMPTS) {\n logger.error('Job exhausted all attempts; dropping it (no dead-letter store)', { jobId: job.id, maxAttempts: DEFAULT_MAX_ATTEMPTS })\n deadJobIds.add(job.id)\n } else {\n const backoffMs = RETRY_BACKOFF_BASE_MS * Math.pow(2, attemptNumber - 1)\n retryUpdates.set(job.id, {\n ...job,\n attemptCount: attemptNumber,\n availableAt: new Date(Date.now() + backoffMs).toISOString(),\n })\n }\n }\n }\n\n const hasChanges = completedJobIds.size > 0 || deadJobIds.size > 0 || retryUpdates.size > 0\n if (hasChanges) {\n await withFileLock(async () => {\n // Re-read so jobs enqueued during handler execution are preserved.\n const currentJobs = await readQueue()\n const updatedJobs = currentJobs\n .filter((j) => !completedJobIds.has(j.id) && !deadJobIds.has(j.id))\n .map((j) => retryUpdates.get(j.id) ?? j)\n await writeQueue(updatedJobs)\n\n const newState: LocalState = {\n lastProcessedId: lastJobId,\n completedCount: (state.completedCount ?? 0) + processed,\n failedCount: (state.failedCount ?? 0) + deadJobIds.size,\n }\n await writeState(newState)\n })\n }\n\n return { processed, failed, lastJobId }\n } finally {\n for (const job of jobsToProcess) {\n inFlightJobIds.delete(job.id)\n }\n }\n }\n\n /**\n * Poll for and process new jobs.\n */\n async function pollAndProcess(): Promise<void> {\n // Skip if already processing to avoid concurrent file access\n if (isProcessing || !activeHandler) return\n\n isProcessing = true\n try {\n await processBatch(activeHandler)\n } catch (error) {\n logger.error('Polling error', { err: error })\n } finally {\n isProcessing = false\n }\n }\n\n async function process(\n handler: JobHandler<T>,\n options?: ProcessOptions\n ): Promise<ProcessResult> {\n // If limit is specified, do a single batch (backward compatibility)\n if (options?.limit) {\n return processBatch(handler, options)\n }\n\n // Start continuous polling mode (like BullMQ Worker)\n activeHandler = handler\n\n // Process any pending jobs immediately\n await processBatch(handler)\n\n // Start polling interval for new jobs\n pollingTimer = setInterval(() => {\n pollAndProcess().catch((err) => {\n logger.error('Poll cycle error', { err })\n })\n }, pollInterval)\n\n logger.info('Worker started', { concurrency })\n\n // Return sentinel value indicating continuous worker mode (like async strategy)\n return { processed: -1, failed: -1, lastJobId: undefined }\n }\n\n async function clear(): Promise<{ removed: number }> {\n return withFileLock(async () => {\n const jobs = await readQueue()\n const removed = jobs.length\n await writeQueue([])\n // Reset state but preserve counts for historical tracking\n const state = await readState()\n await writeState({\n completedCount: state.completedCount,\n failedCount: state.failedCount,\n })\n return { removed }\n })\n }\n\n async function removeQueuedJobsByScope(scope: QueueJobScope): Promise<{ removed: number }> {\n return withFileLock(async () => {\n const jobs = await readQueue()\n const retainedJobs = jobs.filter((job) => inFlightJobIds.has(job.id) || !payloadMatchesScope(job.payload, scope))\n const removed = jobs.length - retainedJobs.length\n if (removed > 0) {\n await writeQueue(retainedJobs)\n }\n return { removed }\n })\n }\n\n async function close(): Promise<void> {\n // Stop polling timer\n if (pollingTimer) {\n clearInterval(pollingTimer)\n pollingTimer = null\n }\n activeHandler = null\n\n // Wait for any in-progress processing to complete (with timeout)\n const SHUTDOWN_TIMEOUT = 5000\n const startTime = Date.now()\n\n while (isProcessing) {\n if (Date.now() - startTime > SHUTDOWN_TIMEOUT) {\n logger.warn('Force closing after shutdown timeout', { timeoutMs: SHUTDOWN_TIMEOUT })\n break\n }\n await new Promise((resolve) => setTimeout(resolve, 50))\n }\n }\n\n async function getJobCounts(): Promise<{\n waiting: number\n active: number\n completed: number\n failed: number\n }> {\n return withFileLock(async () => {\n const state = await readState()\n const jobs = await readQueue()\n\n return {\n waiting: jobs.length, // All jobs in queue are waiting (processed ones are removed)\n active: 0, // Local strategy doesn't track active jobs\n completed: state.completedCount ?? 0,\n failed: state.failedCount ?? 0,\n }\n })\n }\n\n return {\n name,\n strategy: 'local',\n enqueue,\n process,\n clear,\n removeQueuedJobsByScope,\n close,\n getJobCounts,\n }\n}\n"],
5
+ "mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,YAAY;AACnB,SAAS,oBAAoB;AAE7B,SAAS,qBAAqB,qBAAqB;AAEnD,MAAM,gBAAgB,aAAa,OAAO;AAa1C,SAAS,oBAAoB,SAAkB,OAA+B;AAC5E,MAAI,CAAC,WAAW,OAAO,YAAY,SAAU,QAAO;AACpD,QAAM,gBAAgB;AACtB,MAAI,cAAc,aAAa,MAAM,SAAU,QAAO;AACtD,MAAI,MAAM,mBAAmB,QAAW;AACtC,SAAK,cAAc,kBAAkB,UAAU,MAAM,eAAgB,Q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AY;AAC9B,YAAM,QAAQ,MAAM,UAAU;AAC9B,YAAM,OAAO,MAAM,UAAU;AAE7B,aAAO;AAAA,QACL,SAAS,KAAK;AAAA;AAAA,QACd,QAAQ;AAAA;AAAA,QACR,WAAW,MAAM,kBAAkB;AAAA,QACnC,QAAQ,MAAM,eAAe;AAAA,MAC/B;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AAAA,IACL;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
6
6
  "names": ["options"]
7
7
  }
@@ -0,0 +1,27 @@
1
+ import { getTelemetryRuntime } from "@open-mercato/shared/lib/telemetry/runtime";
2
+ const TRACE_META_KEY = "_trace";
3
+ function attachTraceMetadata(metadata) {
4
+ const carrier = getTelemetryRuntime()?.captureTraceContext() ?? {};
5
+ if (Object.keys(carrier).length === 0) return metadata;
6
+ return { ...metadata ?? {}, [TRACE_META_KEY]: carrier };
7
+ }
8
+ function runJobInTrace(queueName, metadata, fn) {
9
+ const runtime = getTelemetryRuntime();
10
+ if (!runtime) return fn();
11
+ return runtime.continueTrace(
12
+ readTraceCarrier(metadata),
13
+ `queue.${queueName}`,
14
+ fn,
15
+ { kind: "consumer" }
16
+ );
17
+ }
18
+ function readTraceCarrier(metadata) {
19
+ const raw = metadata?.[TRACE_META_KEY];
20
+ if (!raw || typeof raw !== "object") return void 0;
21
+ return raw;
22
+ }
23
+ export {
24
+ attachTraceMetadata,
25
+ runJobInTrace
26
+ };
27
+ //# sourceMappingURL=tracing.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/tracing.ts"],
4
+ "sourcesContent": ["import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'\n\n/**\n * Distributed-trace propagation across the enqueue \u2192 worker boundary.\n *\n * The W3C trace carrier rides on the job's `metadata._trace` (a first-class\n * metadata channel, not the user payload). Both halves are automatic \u2014 the\n * strategies call `attachTraceMetadata` at enqueue and `runJobInTrace` at\n * dispatch \u2014 so a worker joins the enqueuing request's trace with no per-worker\n * code. Everything here is a cheap no-op when telemetry is off.\n *\n * This also covers anything that rides the queue: persistent event subscribers\n * (the event bus enqueues) and outbound webhook delivery (queued) become part of\n * the originating request's trace for free.\n */\nconst TRACE_META_KEY = '_trace'\n\n/**\n * Attach the active trace context to a job's metadata. Returns `metadata`\n * unchanged when telemetry is off (no active span \u2192 empty carrier), so jobs stay\n * clean unless tracing is active.\n */\nexport function attachTraceMetadata(\n metadata: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n const carrier = getTelemetryRuntime()?.captureTraceContext() ?? {}\n if (Object.keys(carrier).length === 0) return metadata\n return { ...(metadata ?? {}), [TRACE_META_KEY]: carrier }\n}\n\n/**\n * Run a job handler inside a span (`queue.<queueName>`) that continues the\n * producer's trace from the carrier on `metadata`. With no carrier (or telemetry\n * off) it runs `fn` under a fresh root span \u2014 and a no-op when off. The span\n * ends when `fn` settles (sync or async).\n */\nexport function runJobInTrace<T>(\n queueName: string,\n metadata: Record<string, unknown> | undefined,\n fn: () => T,\n): T {\n const runtime = getTelemetryRuntime()\n if (!runtime) return fn()\n return runtime.continueTrace(\n readTraceCarrier(metadata),\n `queue.${queueName}`,\n fn,\n { kind: 'consumer' },\n )\n}\n\nfunction readTraceCarrier(\n metadata: Record<string, unknown> | undefined,\n): Record<string, string> | undefined {\n const raw = metadata?.[TRACE_META_KEY]\n if (!raw || typeof raw !== 'object') return undefined\n return raw as Record<string, string>\n}\n"],
5
+ "mappings": "AAAA,SAAS,2BAA2B;AAepC,MAAM,iBAAiB;AAOhB,SAAS,oBACd,UACqC;AACrC,QAAM,UAAU,oBAAoB,GAAG,oBAAoB,KAAK,CAAC;AACjE,MAAI,OAAO,KAAK,OAAO,EAAE,WAAW,EAAG,QAAO;AAC9C,SAAO,EAAE,GAAI,YAAY,CAAC,GAAI,CAAC,cAAc,GAAG,QAAQ;AAC1D;AAQO,SAAS,cACd,WACA,UACA,IACG;AACH,QAAM,UAAU,oBAAoB;AACpC,MAAI,CAAC,QAAS,QAAO,GAAG;AACxB,SAAO,QAAQ;AAAA,IACb,iBAAiB,QAAQ;AAAA,IACzB,SAAS,SAAS;AAAA,IAClB;AAAA,IACA,EAAE,MAAM,WAAW;AAAA,EACrB;AACF;AAEA,SAAS,iBACP,UACoC;AACpC,QAAM,MAAM,WAAW,cAAc;AACrC,MAAI,CAAC,OAAO,OAAO,QAAQ,SAAU,QAAO;AAC5C,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -1,5 +1,9 @@
1
1
  import { createQueue } from "../factory.js";
2
2
  import { createLogger } from "@open-mercato/shared/lib/logger";
3
+ import {
4
+ getTelemetryRuntime,
5
+ isTelemetryBackendEnabled
6
+ } from "@open-mercato/shared/lib/telemetry/runtime";
3
7
  const logger = createLogger("queue").child({ component: "worker" });
4
8
  const managedQueues = /* @__PURE__ */ new Set();
5
9
  const managedShutdownHooks = /* @__PURE__ */ new Set();
@@ -37,6 +41,11 @@ function registerShutdownHandlers() {
37
41
  managedShutdownHooks.clear();
38
42
  unregisterShutdownHandlers(sigtermHandler, sigintHandler);
39
43
  shutdownInProgress = false;
44
+ try {
45
+ await getTelemetryRuntime()?.shutdown();
46
+ } catch (error) {
47
+ logger.error("Error flushing telemetry during shutdown", { err: error });
48
+ }
40
49
  if (!hasError) {
41
50
  logger.info("Worker closed successfully");
42
51
  }
@@ -68,6 +77,10 @@ async function runWorker(options) {
68
77
  background = false,
69
78
  strategy: strategyOption
70
79
  } = options;
80
+ if (!getTelemetryRuntime() && isTelemetryBackendEnabled()) {
81
+ const { initTelemetry } = await import("@open-mercato/telemetry");
82
+ await initTelemetry();
83
+ }
71
84
  const strategy = strategyOption ?? (process.env.QUEUE_STRATEGY === "async" ? "async" : "local");
72
85
  logger.info("Starting worker for queue", { queueName, strategy });
73
86
  const queue = createQueue(queueName, strategy, {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/worker/runner.ts"],
4
- "sourcesContent": ["import { createQueue } from '../factory'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport type { Queue, JobHandler, AsyncQueueOptions, QueueStrategyType } from '../types'\n\nconst logger = createLogger('queue').child({ component: 'worker' })\n\n/**\n * Options for running a queue worker.\n */\nexport type WorkerRunnerOptions<T = unknown> = {\n /** Name of the queue to process */\n queueName: string\n /** Handler function to process each job */\n handler: JobHandler<T>\n /** Redis connection options (only used for async strategy) */\n connection?: AsyncQueueOptions['connection']\n /** Number of concurrent jobs to process */\n concurrency?: number\n /** How long a job lock is held before the job counts as stalled, in ms. */\n lockDuration?: number\n /** Number of stalled-job recoveries BullMQ permits before failing a job. */\n maxStalledCount?: number\n /** Whether to set up graceful shutdown handlers */\n gracefulShutdown?: boolean\n /** If true, don't block - return immediately after starting processing (for multi-queue mode) */\n background?: boolean\n /** Queue strategy to use. Defaults to QUEUE_STRATEGY env var or 'local' */\n strategy?: QueueStrategyType\n}\n\nconst managedQueues = new Set<Queue<unknown>>()\nconst managedShutdownHooks = new Set<() => Promise<void> | void>()\nlet shutdownHandlersRegistered = false\nlet shutdownInProgress = false\n\nfunction unregisterShutdownHandlers(sigtermHandler: () => void, sigintHandler: () => void): void {\n process.off('SIGTERM', sigtermHandler)\n process.off('SIGINT', sigintHandler)\n shutdownHandlersRegistered = false\n}\n\nfunction registerShutdownHandlers(): void {\n if (shutdownHandlersRegistered) return\n\n const shutdown = async (signal: string) => {\n if (shutdownInProgress) return\n shutdownInProgress = true\n\n logger.info('Received signal, shutting down gracefully', { signal })\n\n let hasError = false\n for (const queue of managedQueues) {\n try {\n await queue.close()\n } catch (error) {\n hasError = true\n logger.error('Error during shutdown', { err: error })\n }\n }\n\n managedQueues.clear()\n for (const hook of managedShutdownHooks) {\n try {\n await hook()\n } catch (error) {\n hasError = true\n logger.error('Error during shutdown hook', { err: error })\n }\n }\n managedShutdownHooks.clear()\n unregisterShutdownHandlers(sigtermHandler, sigintHandler)\n shutdownInProgress = false\n\n if (!hasError) {\n logger.info('Worker closed successfully')\n }\n\n process.exit(hasError ? 1 : 0)\n }\n\n const sigtermHandler = () => {\n void shutdown('SIGTERM')\n }\n\n const sigintHandler = () => {\n void shutdown('SIGINT')\n }\n\n process.on('SIGTERM', sigtermHandler)\n process.on('SIGINT', sigintHandler)\n shutdownHandlersRegistered = true\n}\n\n/**\n * Register a process-local service that must stop before a worker exits.\n * The returned callback removes the hook when the service is stopped early.\n */\nexport function registerWorkerShutdownHook(hook: () => Promise<void> | void): () => void {\n managedShutdownHooks.add(hook)\n return () => managedShutdownHooks.delete(hook)\n}\n\n/**\n * Runs a queue worker that processes jobs continuously.\n *\n * This function:\n * 1. Creates an async queue instance\n * 2. Starts a BullMQ worker\n * 3. Sets up graceful shutdown on SIGTERM/SIGINT\n * 4. Keeps the process running until shutdown\n *\n * @template T - The job payload type\n * @param options - Worker configuration\n *\n * @example\n * ```typescript\n * import { runWorker } from '@open-mercato/queue/worker'\n *\n * await runWorker({\n * queueName: 'events',\n * handler: async (job, ctx) => {\n * console.log(`Processing ${ctx.jobId}:`, job.payload)\n * },\n * connection: { url: process.env.REDIS_URL },\n * concurrency: 5,\n * })\n * ```\n */\nexport async function runWorker<T = unknown>(\n options: WorkerRunnerOptions<T>\n): Promise<void> {\n const {\n queueName,\n handler,\n connection,\n concurrency = 1,\n lockDuration,\n maxStalledCount,\n gracefulShutdown = true,\n background = false,\n strategy: strategyOption,\n } = options\n\n // Determine queue strategy from option, env var, or default to 'local'\n const strategy: QueueStrategyType = strategyOption\n ?? (process.env.QUEUE_STRATEGY === 'async' ? 'async' : 'local')\n\n logger.info('Starting worker for queue', { queueName, strategy })\n\n const queue = createQueue<T>(queueName, strategy, {\n connection,\n concurrency,\n lockDuration,\n maxStalledCount,\n })\n\n // Set up graceful shutdown\n if (gracefulShutdown) {\n managedQueues.add(queue as Queue<unknown>)\n registerShutdownHandlers()\n }\n\n // Start processing\n await queue.process(handler)\n\n logger.info('Worker running', { concurrency })\n\n if (background) {\n // Return immediately for multi-queue mode\n return\n }\n\n logger.info('Press Ctrl+C to stop')\n\n // Keep the process alive (single-queue mode)\n await new Promise(() => {\n // This promise never resolves, keeping the worker running\n })\n}\n\n/**\n * Creates a worker handler that routes jobs to specific handlers based on job type.\n *\n * @template T - Base job payload type (must include a 'type' field)\n * @param handlers - Map of job types to their handlers\n *\n * @example\n * ```typescript\n * const handler = createRoutedHandler({\n * 'user.created': async (job) => { ... },\n * 'order.placed': async (job) => { ... },\n * })\n *\n * await runWorker({ queueName: 'events', handler })\n * ```\n */\nexport function createRoutedHandler<T extends { type: string }>(\n handlers: Record<string, JobHandler<T>>\n): JobHandler<T> {\n return async (job, ctx) => {\n const type = job.payload.type\n const handler = handlers[type]\n\n if (!handler) {\n logger.warn('No handler registered for job type', { type })\n return\n }\n\n await handler(job, ctx)\n }\n}\n"],
5
- "mappings": "AAAA,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAG7B,MAAM,SAAS,aAAa,OAAO,EAAE,MAAM,EAAE,WAAW,SAAS,CAAC;AA0BlE,MAAM,gBAAgB,oBAAI,IAAoB;AAC9C,MAAM,uBAAuB,oBAAI,IAAgC;AACjE,IAAI,6BAA6B;AACjC,IAAI,qBAAqB;AAEzB,SAAS,2BAA2B,gBAA4B,eAAiC;AAC/F,UAAQ,IAAI,WAAW,cAAc;AACrC,UAAQ,IAAI,UAAU,aAAa;AACnC,+BAA6B;AAC/B;AAEA,SAAS,2BAAiC;AACxC,MAAI,2BAA4B;AAEhC,QAAM,WAAW,OAAO,WAAmB;AACzC,QAAI,mBAAoB;AACxB,yBAAqB;AAErB,WAAO,KAAK,6CAA6C,EAAE,OAAO,CAAC;AAEnE,QAAI,WAAW;AACf,eAAW,SAAS,eAAe;AACjC,UAAI;AACF,cAAM,MAAM,MAAM;AAAA,MACpB,SAAS,OAAO;AACd,mBAAW;AACX,eAAO,MAAM,yBAAyB,EAAE,KAAK,MAA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B,EAAE,YAAY,CAAC;AAE7C,MAAI,YAAY;AAEd;AAAA,EACF;AAEA,SAAO,KAAK,sBAAsB;AAGlC,QAAM,IAAI,QAAQ,MAAM;AAAA,EAExB,CAAC;AACH;AAkBO,SAAS,oBACd,UACe;AACf,SAAO,OAAO,KAAK,QAAQ;AACzB,UAAM,OAAO,IAAI,QAAQ;AACzB,UAAM,UAAU,SAAS,IAAI;AAE7B,QAAI,CAAC,SAAS;AACZ,aAAO,KAAK,sCAAsC,EAAE,KAAK,CAAC;AAC1D;AAAA,IACF;AAEA,UAAM,QAAQ,KAAK,GAAG;AAAA,EACxB;AACF;",
4
+ "sourcesContent": ["import { createQueue } from '../factory'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport {\n getTelemetryRuntime,\n isTelemetryBackendEnabled,\n} from '@open-mercato/shared/lib/telemetry/runtime'\nimport type { Queue, JobHandler, AsyncQueueOptions, QueueStrategyType } from '../types'\n\nconst logger = createLogger('queue').child({ component: 'worker' })\n\n/**\n * Options for running a queue worker.\n */\nexport type WorkerRunnerOptions<T = unknown> = {\n /** Name of the queue to process */\n queueName: string\n /** Handler function to process each job */\n handler: JobHandler<T>\n /** Redis connection options (only used for async strategy) */\n connection?: AsyncQueueOptions['connection']\n /** Number of concurrent jobs to process */\n concurrency?: number\n /** How long a job lock is held before the job counts as stalled, in ms. */\n lockDuration?: number\n /** Number of stalled-job recoveries BullMQ permits before failing a job. */\n maxStalledCount?: number\n /** Whether to set up graceful shutdown handlers */\n gracefulShutdown?: boolean\n /** If true, don't block - return immediately after starting processing (for multi-queue mode) */\n background?: boolean\n /** Queue strategy to use. Defaults to QUEUE_STRATEGY env var or 'local' */\n strategy?: QueueStrategyType\n}\n\nconst managedQueues = new Set<Queue<unknown>>()\nconst managedShutdownHooks = new Set<() => Promise<void> | void>()\nlet shutdownHandlersRegistered = false\nlet shutdownInProgress = false\n\nfunction unregisterShutdownHandlers(sigtermHandler: () => void, sigintHandler: () => void): void {\n process.off('SIGTERM', sigtermHandler)\n process.off('SIGINT', sigintHandler)\n shutdownHandlersRegistered = false\n}\n\nfunction registerShutdownHandlers(): void {\n if (shutdownHandlersRegistered) return\n\n const shutdown = async (signal: string) => {\n if (shutdownInProgress) return\n shutdownInProgress = true\n\n logger.info('Received signal, shutting down gracefully', { signal })\n\n let hasError = false\n for (const queue of managedQueues) {\n try {\n await queue.close()\n } catch (error) {\n hasError = true\n logger.error('Error during shutdown', { err: error })\n }\n }\n\n managedQueues.clear()\n for (const hook of managedShutdownHooks) {\n try {\n await hook()\n } catch (error) {\n hasError = true\n logger.error('Error during shutdown hook', { err: error })\n }\n }\n managedShutdownHooks.clear()\n unregisterShutdownHandlers(sigtermHandler, sigintHandler)\n shutdownInProgress = false\n\n // Flush buffered spans/logs before the process dies. A worker never returns\n // from run(), so bin.ts's post-run shutdownTelemetry() is unreachable on this\n // path \u2014 without this, the BatchSpanProcessor's ~5s tail is dropped on every\n // restart/redeploy. Idempotent and a no-op when telemetry is off; a flush\n // failure must not turn a clean shutdown into a failed one.\n try {\n await getTelemetryRuntime()?.shutdown()\n } catch (error) {\n logger.error('Error flushing telemetry during shutdown', { err: error })\n }\n\n if (!hasError) {\n logger.info('Worker closed successfully')\n }\n\n process.exit(hasError ? 1 : 0)\n }\n\n const sigtermHandler = () => {\n void shutdown('SIGTERM')\n }\n\n const sigintHandler = () => {\n void shutdown('SIGINT')\n }\n\n process.on('SIGTERM', sigtermHandler)\n process.on('SIGINT', sigintHandler)\n shutdownHandlersRegistered = true\n}\n\n/**\n * Register a process-local service that must stop before a worker exits.\n * The returned callback removes the hook when the service is stopped early.\n */\nexport function registerWorkerShutdownHook(hook: () => Promise<void> | void): () => void {\n managedShutdownHooks.add(hook)\n return () => managedShutdownHooks.delete(hook)\n}\n\n/**\n * Runs a queue worker that processes jobs continuously.\n *\n * This function:\n * 1. Creates an async queue instance\n * 2. Starts a BullMQ worker\n * 3. Sets up graceful shutdown on SIGTERM/SIGINT\n * 4. Keeps the process running until shutdown\n *\n * @template T - The job payload type\n * @param options - Worker configuration\n *\n * @example\n * ```typescript\n * import { runWorker } from '@open-mercato/queue/worker'\n *\n * await runWorker({\n * queueName: 'events',\n * handler: async (job, ctx) => {\n * console.log(`Processing ${ctx.jobId}:`, job.payload)\n * },\n * connection: { url: process.env.REDIS_URL },\n * concurrency: 5,\n * })\n * ```\n */\nexport async function runWorker<T = unknown>(\n options: WorkerRunnerOptions<T>\n): Promise<void> {\n const {\n queueName,\n handler,\n connection,\n concurrency = 1,\n lockDuration,\n maxStalledCount,\n gracefulShutdown = true,\n background = false,\n strategy: strategyOption,\n } = options\n\n // Worker processes don't run Next's instrumentation hook, so initialize\n // telemetry here \u2014 this is the single bootstrap every standalone worker passes\n // through. Import the telemetry package only for an explicit enabled backend;\n // with the default/unset backend the worker never evaluates the package.\n if (!getTelemetryRuntime() && isTelemetryBackendEnabled()) {\n const { initTelemetry } = await import('@open-mercato/telemetry')\n await initTelemetry()\n }\n\n // Determine queue strategy from option, env var, or default to 'local'\n const strategy: QueueStrategyType = strategyOption\n ?? (process.env.QUEUE_STRATEGY === 'async' ? 'async' : 'local')\n\n logger.info('Starting worker for queue', { queueName, strategy })\n\n const queue = createQueue<T>(queueName, strategy, {\n connection,\n concurrency,\n lockDuration,\n maxStalledCount,\n })\n\n // Set up graceful shutdown\n if (gracefulShutdown) {\n managedQueues.add(queue as Queue<unknown>)\n registerShutdownHandlers()\n }\n\n // Start processing\n await queue.process(handler)\n\n logger.info('Worker running', { concurrency })\n\n if (background) {\n // Return immediately for multi-queue mode\n return\n }\n\n logger.info('Press Ctrl+C to stop')\n\n // Keep the process alive (single-queue mode)\n await new Promise(() => {\n // This promise never resolves, keeping the worker running\n })\n}\n\n/**\n * Creates a worker handler that routes jobs to specific handlers based on job type.\n *\n * @template T - Base job payload type (must include a 'type' field)\n * @param handlers - Map of job types to their handlers\n *\n * @example\n * ```typescript\n * const handler = createRoutedHandler({\n * 'user.created': async (job) => { ... },\n * 'order.placed': async (job) => { ... },\n * })\n *\n * await runWorker({ queueName: 'events', handler })\n * ```\n */\nexport function createRoutedHandler<T extends { type: string }>(\n handlers: Record<string, JobHandler<T>>\n): JobHandler<T> {\n return async (job, ctx) => {\n const type = job.payload.type\n const handler = handlers[type]\n\n if (!handler) {\n logger.warn('No handler registered for job type', { type })\n return\n }\n\n await handler(job, ctx)\n }\n}\n"],
5
+ "mappings": "AAAA,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAGP,MAAM,SAAS,aAAa,OAAO,EAAE,MAAM,EAAE,WAAW,SAAS,CAAC;AA0BlE,MAAM,gBAAgB,oBAAI,IAAoB;AAC9C,MAAM,uBAAuB,oBAAI,IAAgC;AACjE,IAAI,6BAA6B;AACjC,IAAI,qBAAqB;AAEzB,SAAS,2BAA2B,gBAA4B,eAAiC;AAC/F,UAAQ,IAAI,WAAW,cAAc;AACrC,UAAQ,IAAI,UAAU,aAAa;AACnC,+BAA6B;AAC/B;AAEA,SAAS,2BAAiC;AACxC,MAAI,2BAA4B;AAEhC,QAAM,WAAW,OAAO,WAAmB;AACzC,QAAI,mBAAoB;AACxB,yBAAqB;AAErB,WAAO,KAAK,6CAA6C,EAAE,OAAO,CAAC;AAEnE,QAAI,WAAW;AACf,eAAW,SAAS,eAAe;AACjC,UAAI;AACF,cAAM,MAAM,MAAM;AAAA,MACpB,SAAS,OAAO;AACd,mBAAW;AACX,eAAO,MAAM,yBAAyB,EAAE,KAAK,MAAM,CAAC;AAAA,MACtD;AAAA,IACF;AAEA,kBAAc,MAAM;AACpB,eAAW,QAAQ,sBAAsB;AACvC,UAAI;AACF,cAAM,KAAK;AAAA,MACb,SAAS,OAAO;AACd,mBAAW;AACX,eAAO,MAAM,8BAA8B,EAAE,KAAK,MAAM,CAAC;AAAA,MAC3D;AAAA,IACF;AACA,yBAAqB,MAAM;AAC3B,+BAA2B,gBAAgB,aAAa;AACxD,yBAAqB;AAOrB,QAAI;AACF,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AAEd;AAAA,EACF;AAEA,SAAO,KAAK,sBAAsB;AAGlC,QAAM,IAAI,QAAQ,MAAM;AAAA,EAExB,CAAC;AACH;AAkBO,SAAS,oBACd,UACe;AACf,SAAO,OAAO,KAAK,QAAQ;AACzB,UAAM,OAAO,IAAI,QAAQ;AACzB,UAAM,UAAU,SAAS,IAAI;AAE7B,QAAI,CAAC,SAAS;AACZ,aAAO,KAAK,sCAAsC,EAAE,KAAK,CAAC;AAC1D;AAAA,IACF;AAEA,UAAM,QAAQ,KAAK,GAAG;AAAA,EACxB;AACF;",
6
6
  "names": []
7
7
  }
package/jest.config.cjs CHANGED
@@ -26,5 +26,6 @@ module.exports = {
26
26
  passWithNoTests: true,
27
27
  moduleNameMapper: {
28
28
  '^@open-mercato/shared/(.*)$': '<rootDir>/../shared/src/$1',
29
+ '^@open-mercato/telemetry$': '<rootDir>/../telemetry/src/index.ts',
29
30
  },
30
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/queue",
3
- "version": "0.6.7-develop.6827.1.a575bebce9",
3
+ "version": "0.6.7-develop.6834.1.e76f4b8cbc",
4
4
  "license": "MIT",
5
5
  "description": "Multi-strategy job queue with local and BullMQ support",
6
6
  "type": "module",
@@ -34,11 +34,15 @@
34
34
  }
35
35
  },
36
36
  "peerDependencies": {
37
- "bullmq": "^5.0.0"
37
+ "bullmq": "^5.0.0",
38
+ "bullmq-otel": "^1.3.0"
38
39
  },
39
40
  "peerDependenciesMeta": {
40
41
  "bullmq": {
41
42
  "optional": true
43
+ },
44
+ "bullmq-otel": {
45
+ "optional": true
42
46
  }
43
47
  },
44
48
  "devDependencies": {
@@ -52,7 +56,8 @@
52
56
  "access": "public"
53
57
  },
54
58
  "dependencies": {
55
- "@open-mercato/shared": "0.6.7-develop.6827.1.a575bebce9"
59
+ "@open-mercato/shared": "0.6.7-develop.6834.1.e76f4b8cbc",
60
+ "@open-mercato/telemetry": "0.6.7-develop.6834.1.e76f4b8cbc"
56
61
  },
57
62
  "repository": {
58
63
  "type": "git",
@@ -0,0 +1,122 @@
1
+ // An OTLP backend must be active for the async strategy to delegate tracing to
2
+ // bullmq-otel. Set before any module reads the (memoized) telemetry env, and
3
+ // restored afterwards — process.env is shared across test files in the same
4
+ // jest worker, and a leaked 'otlp' backend breaks sibling telemetry tests.
5
+ const originalTelemetryBackend = process.env.TELEMETRY_BACKEND
6
+ process.env.TELEMETRY_BACKEND = 'otlp'
7
+
8
+ afterAll(() => {
9
+ if (originalTelemetryBackend === undefined) delete process.env.TELEMETRY_BACKEND
10
+ else process.env.TELEMETRY_BACKEND = originalTelemetryBackend
11
+ })
12
+
13
+ import { createQueue } from '../factory'
14
+ import { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'
15
+ import {
16
+ registerTelemetryRuntime,
17
+ resetTelemetryRuntime,
18
+ } from '@open-mercato/shared/lib/telemetry/runtime'
19
+
20
+ const queueCtor = jest.fn()
21
+ const workerCtor = jest.fn()
22
+ const queueAdd = jest.fn(async () => ({ id: 'bull-job-id' }))
23
+
24
+ jest.mock('@open-mercato/shared/lib/redis/connection', () => ({
25
+ getRedisUrlOrThrow: jest.fn(),
26
+ }))
27
+
28
+ jest.mock('bullmq', () => {
29
+ class MockQueue<T> {
30
+ constructor(name: string, opts: unknown) {
31
+ queueCtor(name, opts)
32
+ }
33
+ add = queueAdd as unknown as (name: string, data: T, opts?: unknown) => Promise<{ id?: string }>
34
+ close = jest.fn(async () => {})
35
+ obliterate = jest.fn(async () => {})
36
+ getJobCounts = jest.fn(async () => ({ waiting: 0, active: 0, completed: 0, failed: 0 }))
37
+ }
38
+ class MockWorker<T> {
39
+ constructor(
40
+ name: string,
41
+ _processor: (job: { id?: string; data: T; attemptsMade: number }) => Promise<void>,
42
+ opts: unknown,
43
+ ) {
44
+ workerCtor(name, _processor, opts)
45
+ }
46
+ on = jest.fn()
47
+ close = jest.fn(async () => {})
48
+ }
49
+ return { Queue: MockQueue, Worker: MockWorker }
50
+ })
51
+
52
+ class MockBullMQOtel {
53
+ constructor(public readonly tracerName: string) {}
54
+ }
55
+ jest.mock('bullmq-otel', () => ({ BullMQOtel: MockBullMQOtel }))
56
+
57
+ describe('Queue - async strategy telemetry wiring', () => {
58
+ beforeEach(() => {
59
+ jest.clearAllMocks()
60
+ ;(getRedisUrlOrThrow as jest.MockedFunction<typeof getRedisUrlOrThrow>).mockReturnValue(
61
+ 'rediss://default:secret@example.com:6380/1',
62
+ )
63
+ registerTelemetryRuntime({
64
+ canUseGlobalTracePropagation: () => true,
65
+ captureTraceContext: () => ({}),
66
+ continueTrace: (_carrier, _name, fn) => fn(),
67
+ recordHttpDuration: () => {},
68
+ reportError: () => {},
69
+ shutdown: async () => {},
70
+ })
71
+ })
72
+
73
+ afterEach(() => {
74
+ resetTelemetryRuntime()
75
+ })
76
+
77
+ it('wires bullmq-otel into BOTH the queue and worker when they resolve concurrently', async () => {
78
+ const queue = createQueue<{ value: number }>('trace-queue', 'async', { concurrency: 3 })
79
+
80
+ // Resolve enqueue (Queue) and process (Worker) concurrently: both hit the
81
+ // shared telemetry resolution at once. The memoized in-flight promise must
82
+ // hand both the SAME bullmq-otel instance — never one with, one without.
83
+ await Promise.all([queue.enqueue({ value: 1 }), queue.process(async () => {})])
84
+
85
+ const queueOpts = queueCtor.mock.calls[0]?.[1] as { telemetry?: unknown }
86
+ const workerOpts = workerCtor.mock.calls[0]?.[2] as { telemetry?: unknown }
87
+ expect(queueOpts.telemetry).toBeInstanceOf(MockBullMQOtel)
88
+ expect(workerOpts.telemetry).toBeInstanceOf(MockBullMQOtel)
89
+ expect(queueOpts.telemetry).toBe(workerOpts.telemetry)
90
+ })
91
+
92
+ it('omits the metadata._trace carrier when bullmq-otel owns propagation', async () => {
93
+ const queue = createQueue<{ value: number }>('trace-queue', 'async')
94
+
95
+ await queue.enqueue({ value: 42 })
96
+
97
+ const jobData = queueAdd.mock.calls[0]?.[1] as Record<string, unknown>
98
+ expect(jobData).not.toHaveProperty('metadata')
99
+ })
100
+
101
+ it('uses the dedicated carrier when global propagation is not explicitly trusted', async () => {
102
+ resetTelemetryRuntime()
103
+ registerTelemetryRuntime({
104
+ canUseGlobalTracePropagation: () => false,
105
+ captureTraceContext: () => ({ traceparent: 'secure-carrier' }),
106
+ continueTrace: (_carrier, _name, fn) => fn(),
107
+ recordHttpDuration: () => {},
108
+ reportError: () => {},
109
+ shutdown: async () => {},
110
+ })
111
+ const queue = createQueue<{ value: number }>('secure-trace-queue', 'async')
112
+
113
+ await queue.enqueue({ value: 7 })
114
+
115
+ const queueOpts = queueCtor.mock.calls[0]?.[1] as { telemetry?: unknown }
116
+ const jobData = queueAdd.mock.calls[0]?.[1] as {
117
+ metadata?: { _trace?: { traceparent?: string } }
118
+ }
119
+ expect(queueOpts.telemetry).toBeUndefined()
120
+ expect(jobData.metadata?._trace?.traceparent).toBe('secure-carrier')
121
+ })
122
+ })
@@ -0,0 +1,123 @@
1
+ import fs from 'node:fs'
2
+ import os from 'node:os'
3
+ import path from 'node:path'
4
+ import { attachTraceMetadata, runJobInTrace } from '../tracing'
5
+ import { createLocalQueue } from '../strategies/local'
6
+ import { registerProvider, initTelemetry, shutdownTelemetry } from '@open-mercato/telemetry'
7
+ import type { LogRecord, MetricPoint, Span, SpanOptions, TelemetryProvider, TraceCarrier } from '@open-mercato/telemetry'
8
+
9
+ /**
10
+ * Verifies the enqueue → worker trace handoff: the active context is captured
11
+ * onto job metadata at enqueue, and the worker continues that trace at dispatch.
12
+ * Uses a recording provider under the explicitly enabled console seam rather
13
+ * than the real OTLP SDK.
14
+ */
15
+ const spanNames: string[] = []
16
+ const remoteCarriers: TraceCarrier[] = []
17
+
18
+ function noopSpan(): Span {
19
+ return { setAttribute() {}, setAttributes() {}, recordException() {}, setStatus() {}, end() {} }
20
+ }
21
+
22
+ const recordingProvider: TelemetryProvider = {
23
+ name: 'console',
24
+ supports: ['traces'],
25
+ async start() {},
26
+ async shutdown() {},
27
+ runInSpan<T>(name: string, _o: SpanOptions, fn: (s: Span) => T): T {
28
+ spanNames.push(name)
29
+ return fn(noopSpan())
30
+ },
31
+ activeSpan: () => undefined,
32
+ activeTraceContext: () => undefined,
33
+ inject: (carrier) => {
34
+ carrier.traceparent = 'test-traceparent'
35
+ },
36
+ runInRemoteSpan<T>(carrier: TraceCarrier, name: string, _o: SpanOptions, fn: (s: Span) => T): T {
37
+ remoteCarriers.push(carrier)
38
+ spanNames.push(name)
39
+ return fn(noopSpan())
40
+ },
41
+ emitLog: (_r: LogRecord) => {},
42
+ recordMetric: (_p: MetricPoint) => {},
43
+ }
44
+
45
+ beforeAll(async () => {
46
+ process.env.TELEMETRY_BACKEND = 'console'
47
+ registerProvider(recordingProvider)
48
+ await initTelemetry()
49
+ })
50
+
51
+ afterAll(async () => {
52
+ await shutdownTelemetry()
53
+ delete process.env.TELEMETRY_BACKEND
54
+ })
55
+
56
+ describe('queue trace propagation', () => {
57
+ it('attaches the active trace carrier to job metadata at enqueue', () => {
58
+ const metadata = attachTraceMetadata(undefined)
59
+ expect(metadata).toEqual({ _trace: { traceparent: 'test-traceparent' } })
60
+ })
61
+
62
+ it('preserves existing metadata while attaching the trace carrier', () => {
63
+ const metadata = attachTraceMetadata({ foo: 'bar' })
64
+ expect(metadata).toMatchObject({ foo: 'bar', _trace: { traceparent: 'test-traceparent' } })
65
+ })
66
+
67
+ it('continues the producer trace from job metadata at dispatch', async () => {
68
+ const result = await runJobInTrace('orders-process', { _trace: { traceparent: 'tp-123' } }, () =>
69
+ Promise.resolve('done'),
70
+ )
71
+ expect(result).toBe('done')
72
+ expect(remoteCarriers).toContainEqual({ traceparent: 'tp-123' })
73
+ expect(spanNames).toContain('queue.orders-process')
74
+ })
75
+
76
+ it('runs jobs without a carrier under a fresh span (no crash)', async () => {
77
+ const result = await runJobInTrace('orders-process', undefined, () => Promise.resolve(42))
78
+ expect(result).toBe(42)
79
+ })
80
+ })
81
+
82
+ /**
83
+ * End-to-end through the REAL local strategy (file write + read + dispatch),
84
+ * proving the headline acceptance criterion: a queued job continues the
85
+ * enqueuing request's trace. This exercises the actual enqueue/dispatch wiring,
86
+ * not just the helpers above.
87
+ */
88
+ describe('queue trace propagation (real local strategy)', () => {
89
+ let baseDir: string
90
+
91
+ beforeEach(() => {
92
+ baseDir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-queue-trace-'))
93
+ })
94
+
95
+ afterEach(() => {
96
+ fs.rmSync(baseDir, { recursive: true, force: true })
97
+ })
98
+
99
+ it('persists the trace carrier on enqueue and continues it on dispatch', async () => {
100
+ const queue = createLocalQueue<{ orderId: string }>('orders-process', { baseDir })
101
+
102
+ await queue.enqueue({ orderId: 'o-1' })
103
+
104
+ // The carrier is written to the job's metadata — NOT the user payload.
105
+ const stored = JSON.parse(
106
+ fs.readFileSync(path.join(baseDir, 'orders-process', 'queue.json'), 'utf8'),
107
+ ) as Array<{ payload: unknown; metadata?: Record<string, unknown> }>
108
+ expect(stored[0].metadata).toEqual({ _trace: { traceparent: 'test-traceparent' } })
109
+ expect(stored[0].payload).toEqual({ orderId: 'o-1' })
110
+
111
+ let handlerRan = false
112
+ await queue.process((job) => {
113
+ handlerRan = true
114
+ // The handler still sees only its payload; the carrier is invisible to it.
115
+ expect(job.payload).toEqual({ orderId: 'o-1' })
116
+ })
117
+
118
+ expect(handlerRan).toBe(true)
119
+ // The worker continued the producer's trace under a `queue.<name>` span.
120
+ expect(remoteCarriers).toContainEqual({ traceparent: 'test-traceparent' })
121
+ expect(spanNames).toContain('queue.orders-process')
122
+ })
123
+ })
@@ -0,0 +1,80 @@
1
+ import path from 'node:path'
2
+ import fs from 'node:fs'
3
+ import os from 'node:os'
4
+
5
+ /**
6
+ * Regression: the worker's graceful shutdown used to close the queues
7
+ * and call process.exit() without flushing telemetry. A worker never returns
8
+ * from run(), so the CLI's post-run shutdownTelemetry() is unreachable — the
9
+ * BatchSpanProcessor's buffered tail (~5s of spans/logs) was dropped on every
10
+ * restart/redeploy. The shutdown handler must flush BEFORE exiting.
11
+ */
12
+
13
+ import {
14
+ registerTelemetryRuntime,
15
+ resetTelemetryRuntime,
16
+ type TelemetryRuntime,
17
+ } from '@open-mercato/shared/lib/telemetry/runtime'
18
+
19
+ import { runWorker } from '../worker/runner'
20
+
21
+ const mockCallOrder: string[] = []
22
+ const runtime: TelemetryRuntime = {
23
+ canUseGlobalTracePropagation: () => false,
24
+ captureTraceContext: () => ({}),
25
+ continueTrace: (_carrier, _name, fn) => fn(),
26
+ recordHttpDuration: () => {},
27
+ reportError: () => {},
28
+ shutdown: jest.fn(async () => {
29
+ mockCallOrder.push('flush')
30
+ }),
31
+ }
32
+
33
+ describe('worker shutdown flushes telemetry', () => {
34
+ let tmpDir: string
35
+ let cwd: string
36
+
37
+ beforeEach(() => {
38
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'worker-shutdown-'))
39
+ cwd = process.cwd()
40
+ process.chdir(tmpDir)
41
+ mockCallOrder.length = 0
42
+ delete process.env.TELEMETRY_BACKEND
43
+ registerTelemetryRuntime(runtime)
44
+ })
45
+
46
+ afterEach(() => {
47
+ process.chdir(cwd)
48
+ resetTelemetryRuntime()
49
+ fs.rmSync(tmpDir, { recursive: true, force: true })
50
+ })
51
+
52
+ it('SIGTERM flushes telemetry before process.exit', async () => {
53
+ const exitSpy = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => {
54
+ mockCallOrder.push(`exit:${code ?? 0}`)
55
+ return undefined as never
56
+ }) as never)
57
+ const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
58
+
59
+ try {
60
+ await runWorker({
61
+ queueName: 'shutdown-flush-test',
62
+ handler: async () => {},
63
+ strategy: 'local',
64
+ background: true,
65
+ gracefulShutdown: true,
66
+ })
67
+
68
+ process.emit('SIGTERM')
69
+ // The shutdown handler is async (close → flush → exit); let it settle.
70
+ await new Promise((resolve) => setTimeout(resolve, 100))
71
+
72
+ expect(mockCallOrder).toContain('flush')
73
+ expect(mockCallOrder).toContain('exit:0')
74
+ expect(mockCallOrder.indexOf('flush')).toBeLessThan(mockCallOrder.indexOf('exit:0'))
75
+ } finally {
76
+ exitSpy.mockRestore()
77
+ logSpy.mockRestore()
78
+ }
79
+ })
80
+ })
@@ -1,5 +1,7 @@
1
1
  import type { Queue, QueuedJob, JobHandler, AsyncQueueOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'
2
2
  import { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'
3
+ import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'
4
+ import { attachTraceMetadata, runJobInTrace } from '../tracing'
3
5
  import { createLogger } from '@open-mercato/shared/lib/logger'
4
6
 
5
7
  const packageLogger = createLogger('queue')
@@ -43,14 +45,23 @@ interface BullWorkerInterface {
43
45
  }
44
46
 
45
47
  interface BullMQModule {
46
- Queue: new <T>(name: string, opts: { connection: ConnectionOptions }) => BullQueueInterface<T>
48
+ Queue: new <T>(name: string, opts: { connection: ConnectionOptions; telemetry?: unknown }) => BullQueueInterface<T>
47
49
  Worker: new <T>(
48
50
  name: string,
49
51
  processor: (job: { id?: string; data: T; attemptsMade: number }) => Promise<void>,
50
- opts: { connection: ConnectionOptions; concurrency: number; lockDuration?: number; maxStalledCount?: number }
52
+ opts: {
53
+ connection: ConnectionOptions
54
+ concurrency: number
55
+ telemetry?: unknown
56
+ lockDuration?: number
57
+ maxStalledCount?: number
58
+ }
51
59
  ) => BullWorkerInterface
52
60
  }
53
61
 
62
+ /** The `bullmq-otel` package (optional). Loaded only when an OTLP backend is active. */
63
+ type BullMQOtelModule = { BullMQOtel: new (tracerName: string) => object }
64
+
54
65
  const REMOVABLE_JOB_STATES = ['waiting', 'delayed', 'prioritized', 'paused', 'waiting-children']
55
66
 
56
67
  function payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {
@@ -119,6 +130,10 @@ export function createAsyncQueue<T = unknown>(
119
130
  let bullQueue: BullQueueInterface<QueuedJob<T>> | null = null
120
131
  let bullWorker: BullWorkerInterface | null = null
121
132
  let bullmqModule: BullMQModule | null = null
133
+ // Resolved once: a BullMQOtel instance (delegate async tracing to BullMQ) or
134
+ // undefined (use our own metadata._trace carrier instead). Memoized as the
135
+ // in-flight promise so concurrent first-time callers share one resolution.
136
+ let telemetryPromise: Promise<object | undefined> | null = null
122
137
 
123
138
  // -------------------------------------------------------------------------
124
139
  // Lazy BullMQ initialization
@@ -137,10 +152,35 @@ export function createAsyncQueue<T = unknown>(
137
152
  return bullmqModule
138
153
  }
139
154
 
155
+ /**
156
+ * When an OTLP backend is active, delegate async-queue tracing to `bullmq-otel`
157
+ * (richer BullMQ-internal spans: add / process / wait / attempts). Returns
158
+ * `undefined` — meaning "use our own `metadata._trace` carrier" — when telemetry
159
+ * is off, a non-OTEL backend is selected, or `bullmq-otel` isn't installed. (The
160
+ * `local` strategy always uses our carrier; it isn't BullMQ, so `bullmq-otel`
161
+ * cannot instrument it.)
162
+ */
163
+ async function getQueueTelemetry(): Promise<object | undefined> {
164
+ if (!telemetryPromise) {
165
+ telemetryPromise = (async () => {
166
+ if (!getTelemetryRuntime()?.canUseGlobalTracePropagation()) return undefined
167
+ try {
168
+ const mod = (await import('bullmq-otel')) as unknown as BullMQOtelModule
169
+ return new mod.BullMQOtel('open-mercato')
170
+ } catch {
171
+ packageLogger.warn('bullmq-otel not available; using built-in trace carrier', { queue: name })
172
+ return undefined
173
+ }
174
+ })()
175
+ }
176
+ return telemetryPromise
177
+ }
178
+
140
179
  async function getQueue(): Promise<BullQueueInterface<QueuedJob<T>>> {
141
180
  if (!bullQueue) {
142
181
  const { Queue: BullQueueClass } = await getBullMQ()
143
- bullQueue = new BullQueueClass<QueuedJob<T>>(name, { connection })
182
+ const telemetry = await getQueueTelemetry()
183
+ bullQueue = new BullQueueClass<QueuedJob<T>>(name, { connection, ...(telemetry ? { telemetry } : {}) })
144
184
  }
145
185
  return bullQueue
146
186
  }
@@ -151,10 +191,14 @@ export function createAsyncQueue<T = unknown>(
151
191
 
152
192
  async function enqueue(data: T, options?: EnqueueOptions): Promise<string> {
153
193
  const queue = await getQueue()
194
+ // When bullmq-otel handles propagation, don't also attach our carrier.
195
+ const telemetry = await getQueueTelemetry()
196
+ const metadata = telemetry ? undefined : attachTraceMetadata(undefined)
154
197
  const jobData: QueuedJob<T> = {
155
198
  id: crypto.randomUUID(),
156
199
  payload: data,
157
200
  createdAt: new Date().toISOString(),
201
+ ...(metadata ? { metadata } : {}),
158
202
  }
159
203
 
160
204
  const job = await queue.add(jobData.id, jobData, {
@@ -170,21 +214,31 @@ export function createAsyncQueue<T = unknown>(
170
214
 
171
215
  async function process(handler: JobHandler<T>): Promise<ProcessResult> {
172
216
  const { Worker } = await getBullMQ()
217
+ const telemetry = await getQueueTelemetry()
173
218
 
174
219
  // Create worker that processes jobs
175
220
  bullWorker = new Worker<QueuedJob<T>>(
176
221
  name,
177
222
  async (job) => {
178
223
  const jobData = job.data
179
- await handler(jobData, {
224
+ const ctx = {
180
225
  jobId: job.id ?? jobData.id,
181
226
  attemptNumber: job.attemptsMade + 1,
182
227
  queueName: name,
183
- })
228
+ }
229
+ // With bullmq-otel active, BullMQ owns the process span and active
230
+ // context (the handler's pg/undici spans nest under it). Otherwise
231
+ // continue the trace from our own carrier.
232
+ if (telemetry) {
233
+ await handler(jobData, ctx)
234
+ } else {
235
+ await runJobInTrace(name, jobData.metadata, () => handler(jobData, ctx))
236
+ }
184
237
  },
185
238
  {
186
239
  connection,
187
240
  concurrency,
241
+ ...(telemetry ? { telemetry } : {}),
188
242
  ...(lockDuration !== undefined ? { lockDuration } : {}),
189
243
  ...(maxStalledCount !== undefined ? { maxStalledCount } : {}),
190
244
  }
@@ -3,6 +3,7 @@ import path from 'node:path'
3
3
  import crypto from 'node:crypto'
4
4
  import { createLogger } from '@open-mercato/shared/lib/logger'
5
5
  import type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'
6
+ import { attachTraceMetadata, runJobInTrace } from '../tracing'
6
7
 
7
8
  const packageLogger = createLogger('queue')
8
9
 
@@ -205,11 +206,13 @@ export function createLocalQueue<T = unknown>(
205
206
  const availableAt = options?.delayMs && options.delayMs > 0
206
207
  ? new Date(Date.now() + options.delayMs).toISOString()
207
208
  : undefined
209
+ const metadata = attachTraceMetadata(undefined)
208
210
  const job: StoredJob<T> = {
209
211
  id: generateId(),
210
212
  payload: data,
211
213
  createdAt: new Date().toISOString(),
212
214
  ...(availableAt ? { availableAt } : {}),
215
+ ...(metadata ? { metadata } : {}),
213
216
  }
214
217
  await withFileLock(async () => {
215
218
  const jobs = await readQueue()
@@ -255,12 +258,14 @@ export function createLocalQueue<T = unknown>(
255
258
  for (const job of jobsToProcess) {
256
259
  const attemptNumber = (job.attemptCount ?? 0) + 1
257
260
  try {
258
- await Promise.resolve(
259
- handler(job, {
260
- jobId: job.id,
261
- attemptNumber,
262
- queueName: name,
263
- })
261
+ await runJobInTrace(name, job.metadata, () =>
262
+ Promise.resolve(
263
+ handler(job, {
264
+ jobId: job.id,
265
+ attemptNumber,
266
+ queueName: name,
267
+ })
268
+ )
264
269
  )
265
270
  processed++
266
271
  lastJobId = job.id
package/src/tracing.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'
2
+
3
+ /**
4
+ * Distributed-trace propagation across the enqueue → worker boundary.
5
+ *
6
+ * The W3C trace carrier rides on the job's `metadata._trace` (a first-class
7
+ * metadata channel, not the user payload). Both halves are automatic — the
8
+ * strategies call `attachTraceMetadata` at enqueue and `runJobInTrace` at
9
+ * dispatch — so a worker joins the enqueuing request's trace with no per-worker
10
+ * code. Everything here is a cheap no-op when telemetry is off.
11
+ *
12
+ * This also covers anything that rides the queue: persistent event subscribers
13
+ * (the event bus enqueues) and outbound webhook delivery (queued) become part of
14
+ * the originating request's trace for free.
15
+ */
16
+ const TRACE_META_KEY = '_trace'
17
+
18
+ /**
19
+ * Attach the active trace context to a job's metadata. Returns `metadata`
20
+ * unchanged when telemetry is off (no active span → empty carrier), so jobs stay
21
+ * clean unless tracing is active.
22
+ */
23
+ export function attachTraceMetadata(
24
+ metadata: Record<string, unknown> | undefined,
25
+ ): Record<string, unknown> | undefined {
26
+ const carrier = getTelemetryRuntime()?.captureTraceContext() ?? {}
27
+ if (Object.keys(carrier).length === 0) return metadata
28
+ return { ...(metadata ?? {}), [TRACE_META_KEY]: carrier }
29
+ }
30
+
31
+ /**
32
+ * Run a job handler inside a span (`queue.<queueName>`) that continues the
33
+ * producer's trace from the carrier on `metadata`. With no carrier (or telemetry
34
+ * off) it runs `fn` under a fresh root span — and a no-op when off. The span
35
+ * ends when `fn` settles (sync or async).
36
+ */
37
+ export function runJobInTrace<T>(
38
+ queueName: string,
39
+ metadata: Record<string, unknown> | undefined,
40
+ fn: () => T,
41
+ ): T {
42
+ const runtime = getTelemetryRuntime()
43
+ if (!runtime) return fn()
44
+ return runtime.continueTrace(
45
+ readTraceCarrier(metadata),
46
+ `queue.${queueName}`,
47
+ fn,
48
+ { kind: 'consumer' },
49
+ )
50
+ }
51
+
52
+ function readTraceCarrier(
53
+ metadata: Record<string, unknown> | undefined,
54
+ ): Record<string, string> | undefined {
55
+ const raw = metadata?.[TRACE_META_KEY]
56
+ if (!raw || typeof raw !== 'object') return undefined
57
+ return raw as Record<string, string>
58
+ }
@@ -1,5 +1,9 @@
1
1
  import { createQueue } from '../factory'
2
2
  import { createLogger } from '@open-mercato/shared/lib/logger'
3
+ import {
4
+ getTelemetryRuntime,
5
+ isTelemetryBackendEnabled,
6
+ } from '@open-mercato/shared/lib/telemetry/runtime'
3
7
  import type { Queue, JobHandler, AsyncQueueOptions, QueueStrategyType } from '../types'
4
8
 
5
9
  const logger = createLogger('queue').child({ component: 'worker' })
@@ -71,6 +75,17 @@ function registerShutdownHandlers(): void {
71
75
  unregisterShutdownHandlers(sigtermHandler, sigintHandler)
72
76
  shutdownInProgress = false
73
77
 
78
+ // Flush buffered spans/logs before the process dies. A worker never returns
79
+ // from run(), so bin.ts's post-run shutdownTelemetry() is unreachable on this
80
+ // path — without this, the BatchSpanProcessor's ~5s tail is dropped on every
81
+ // restart/redeploy. Idempotent and a no-op when telemetry is off; a flush
82
+ // failure must not turn a clean shutdown into a failed one.
83
+ try {
84
+ await getTelemetryRuntime()?.shutdown()
85
+ } catch (error) {
86
+ logger.error('Error flushing telemetry during shutdown', { err: error })
87
+ }
88
+
74
89
  if (!hasError) {
75
90
  logger.info('Worker closed successfully')
76
91
  }
@@ -141,6 +156,15 @@ export async function runWorker<T = unknown>(
141
156
  strategy: strategyOption,
142
157
  } = options
143
158
 
159
+ // Worker processes don't run Next's instrumentation hook, so initialize
160
+ // telemetry here — this is the single bootstrap every standalone worker passes
161
+ // through. Import the telemetry package only for an explicit enabled backend;
162
+ // with the default/unset backend the worker never evaluates the package.
163
+ if (!getTelemetryRuntime() && isTelemetryBackendEnabled()) {
164
+ const { initTelemetry } = await import('@open-mercato/telemetry')
165
+ await initTelemetry()
166
+ }
167
+
144
168
  // Determine queue strategy from option, env var, or default to 'local'
145
169
  const strategy: QueueStrategyType = strategyOption
146
170
  ?? (process.env.QUEUE_STRATEGY === 'async' ? 'async' : 'local')