@open-mercato/queue 0.6.5 → 0.6.6-canary.315.1.g6673635216
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.
- package/AGENTS.md +15 -0
- package/dist/strategies/async.js +35 -4
- package/dist/strategies/async.js.map +2 -2
- package/dist/strategies/local.js +87 -50
- package/dist/strategies/local.js.map +2 -2
- package/dist/worker/registry.js +3 -1
- package/dist/worker/registry.js.map +2 -2
- package/dist/worker/runner.js +9 -7
- package/dist/worker/runner.js.map +2 -2
- package/jest.config.cjs +1 -1
- package/package.json +8 -5
- package/src/__tests__/async.strategy.test.ts +48 -0
- package/src/__tests__/local.strategy.test.ts +99 -15
- package/src/strategies/async.ts +48 -5
- package/src/strategies/local.ts +101 -57
- package/src/types.ts +13 -0
- package/src/worker/registry.ts +4 -1
- package/src/worker/runner.ts +10 -7
package/AGENTS.md
CHANGED
|
@@ -42,6 +42,21 @@ yarn workspace @open-mercato/queue build
|
|
|
42
42
|
| CPU-bound (calculations, parsing) | 1–2 | Avoid blocking the event loop |
|
|
43
43
|
| Database-heavy (bulk writes) | 3–5 | Balance throughput with connection pool |
|
|
44
44
|
|
|
45
|
+
## Connection Budget (MUST keep workers within the DB pool)
|
|
46
|
+
|
|
47
|
+
Since each worker job runs in its own request container (one `EntityManager`, so
|
|
48
|
+
one pooled DB connection checked out for the job's duration), the peak DB
|
|
49
|
+
connection demand of `worker --all` is **`Σ(per-queue concurrency)`**. That sum
|
|
50
|
+
MUST stay within the database's connection budget, or background jobs starve the
|
|
51
|
+
connections the request/onboarding path needs — the failure mode behind the
|
|
52
|
+
2026-06 onboarding stall.
|
|
53
|
+
|
|
54
|
+
- **Invariant:** `web_pool_max + worker_pool_max + scheduler/overhead ≤ Postgres max_connections` (leave headroom). `*_pool_max` is each process's `DB_POOL_MAX` (default 20).
|
|
55
|
+
- `worker --all` fits `Σconcurrency` to the worker's connection budget at startup and logs the resolved plan (`[worker] DB connection budget: …`). The budget defaults to the resolved `DB_POOL_MAX`; override with `OM_WORKERS_DB_CONNECTION_BUDGET`. Every queue keeps a floor of 1, and no queue exceeds its declared concurrency — so clamping only removes over-subscription, never real throughput.
|
|
56
|
+
- When you change a worker's `concurrency`, add a queue, or raise a pool size, re-check the invariant. A change that multiplies per-job resource usage (e.g. moving to per-job containers) MUST state and verify the new connection ceiling.
|
|
57
|
+
- Give long-running background processes (the worker) a **smaller** `DB_POOL_MAX` than the web process when they share one database, so a worker storm can never consume the web pool's share of `max_connections`.
|
|
58
|
+
- Workers that call external services MUST bound those calls with a timeout (e.g. `VECTOR_EMBEDDING_TIMEOUT_MS` for embeddings) so a dead dependency releases its connection promptly instead of pinning pool capacity.
|
|
59
|
+
|
|
45
60
|
## Adding a New Worker
|
|
46
61
|
|
|
47
62
|
1. Create worker file in `src/modules/<module>/workers/<worker-name>.ts`
|
package/dist/strategies/async.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { getRedisUrlOrThrow } from "@open-mercato/shared/lib/redis/connection";
|
|
2
|
+
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
3
|
+
const packageLogger = createLogger("queue");
|
|
4
|
+
const REMOVABLE_JOB_STATES = ["waiting", "delayed", "prioritized", "paused", "waiting-children"];
|
|
5
|
+
function payloadMatchesScope(payload, scope) {
|
|
6
|
+
if (!payload || typeof payload !== "object") return false;
|
|
7
|
+
const scopedPayload = payload;
|
|
8
|
+
if (scopedPayload.tenantId !== scope.tenantId) return false;
|
|
9
|
+
if (scope.organizationId !== void 0) {
|
|
10
|
+
if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false;
|
|
11
|
+
}
|
|
12
|
+
if (scope.jobTypes?.length) {
|
|
13
|
+
return typeof scopedPayload.jobType === "string" && scope.jobTypes.includes(scopedPayload.jobType);
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
2
17
|
function resolveConnection(options) {
|
|
3
18
|
if (options?.url) {
|
|
4
19
|
return { url: options.url };
|
|
@@ -18,6 +33,7 @@ function resolveConnection(options) {
|
|
|
18
33
|
function createAsyncQueue(name, options) {
|
|
19
34
|
const connection = resolveConnection(options?.connection);
|
|
20
35
|
const concurrency = options?.concurrency ?? 1;
|
|
36
|
+
const logger = packageLogger.child({ queue: name });
|
|
21
37
|
let bullQueue = null;
|
|
22
38
|
let bullWorker = null;
|
|
23
39
|
let bullmqModule = null;
|
|
@@ -75,18 +91,18 @@ function createAsyncQueue(name, options) {
|
|
|
75
91
|
);
|
|
76
92
|
bullWorker.on("completed", (job) => {
|
|
77
93
|
const jobWithId = job;
|
|
78
|
-
|
|
94
|
+
logger.info("Job completed", { jobId: jobWithId.id });
|
|
79
95
|
});
|
|
80
96
|
bullWorker.on("failed", (job, err) => {
|
|
81
97
|
const jobWithId = job;
|
|
82
98
|
const error = err;
|
|
83
|
-
|
|
99
|
+
logger.error("Job failed", { jobId: jobWithId?.id, err: error });
|
|
84
100
|
});
|
|
85
101
|
bullWorker.on("error", (err) => {
|
|
86
102
|
const error = err;
|
|
87
|
-
|
|
103
|
+
logger.error("Worker error", { err: error });
|
|
88
104
|
});
|
|
89
|
-
|
|
105
|
+
logger.info("Worker started", { concurrency });
|
|
90
106
|
return { processed: -1, failed: -1, lastJobId: void 0 };
|
|
91
107
|
}
|
|
92
108
|
async function clear() {
|
|
@@ -94,6 +110,20 @@ function createAsyncQueue(name, options) {
|
|
|
94
110
|
await queue.obliterate({ force: true });
|
|
95
111
|
return { removed: -1 };
|
|
96
112
|
}
|
|
113
|
+
async function removeQueuedJobsByScope(scope) {
|
|
114
|
+
const queue = await getQueue();
|
|
115
|
+
const jobs = await queue.getJobs(REMOVABLE_JOB_STATES, 0, -1);
|
|
116
|
+
let removed = 0;
|
|
117
|
+
for (const job of jobs) {
|
|
118
|
+
if (!payloadMatchesScope(job.data?.payload, scope)) continue;
|
|
119
|
+
try {
|
|
120
|
+
await job.remove();
|
|
121
|
+
removed++;
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return { removed };
|
|
126
|
+
}
|
|
97
127
|
async function close() {
|
|
98
128
|
if (bullWorker) {
|
|
99
129
|
await bullWorker.close();
|
|
@@ -120,6 +150,7 @@ function createAsyncQueue(name, options) {
|
|
|
120
150
|
enqueue,
|
|
121
151
|
process,
|
|
122
152
|
clear,
|
|
153
|
+
removeQueuedJobsByScope,
|
|
123
154
|
close,
|
|
124
155
|
getJobCounts
|
|
125
156
|
};
|
|
@@ -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 } from '../types'\nimport { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'\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}\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 }\n ) => BullWorkerInterface\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\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: 3,\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 }\n )\n\n // Set up event handlers\n bullWorker.on('completed', (job) => {\n const jobWithId = job as { id?: string }\n
|
|
5
|
-
"mappings": "AACA,SAAS,0BAA0B;
|
|
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 }\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 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: 3,\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 }\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 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,QAA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AAQ,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;",
|
|
6
6
|
"names": ["options"]
|
|
7
7
|
}
|
package/dist/strategies/local.js
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import crypto from "node:crypto";
|
|
4
|
+
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
5
|
+
const packageLogger = createLogger("queue");
|
|
6
|
+
function payloadMatchesScope(payload, scope) {
|
|
7
|
+
if (!payload || typeof payload !== "object") return false;
|
|
8
|
+
const scopedPayload = payload;
|
|
9
|
+
if (scopedPayload.tenantId !== scope.tenantId) return false;
|
|
10
|
+
if (scope.organizationId !== void 0) {
|
|
11
|
+
if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false;
|
|
12
|
+
}
|
|
13
|
+
if (scope.jobTypes?.length) {
|
|
14
|
+
return typeof scopedPayload.jobType === "string" && scope.jobTypes.includes(scopedPayload.jobType);
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
4
18
|
const DEFAULT_POLL_INTERVAL = 1e3;
|
|
5
19
|
const DEFAULT_LOCAL_QUEUE_BASE_DIR = ".mercato/queue";
|
|
6
20
|
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
@@ -13,11 +27,13 @@ function createLocalQueue(name, options) {
|
|
|
13
27
|
const queueDir = path.join(baseDir, name);
|
|
14
28
|
const queueFile = path.join(queueDir, "queue.json");
|
|
15
29
|
const stateFile = path.join(queueDir, "state.json");
|
|
30
|
+
const logger = packageLogger.child({ queue: name });
|
|
16
31
|
const concurrency = options?.concurrency ?? 1;
|
|
17
32
|
const pollInterval = options?.pollInterval ?? DEFAULT_POLL_INTERVAL;
|
|
18
33
|
let pollingTimer = null;
|
|
19
34
|
let isProcessing = false;
|
|
20
35
|
let activeHandler = null;
|
|
36
|
+
const inFlightJobIds = /* @__PURE__ */ new Set();
|
|
21
37
|
let fileOpChain = Promise.resolve();
|
|
22
38
|
function withFileLock(fn) {
|
|
23
39
|
const run = fileOpChain.then(() => fn(), () => fn());
|
|
@@ -63,7 +79,7 @@ function createLocalQueue(name, options) {
|
|
|
63
79
|
if (readError.code === "ENOENT") {
|
|
64
80
|
return [];
|
|
65
81
|
}
|
|
66
|
-
|
|
82
|
+
logger.error("Failed to read queue file", { err: readError });
|
|
67
83
|
throw new Error(`Queue file unreadable: ${readError.message}`);
|
|
68
84
|
}
|
|
69
85
|
try {
|
|
@@ -74,9 +90,9 @@ function createLocalQueue(name, options) {
|
|
|
74
90
|
return parsed;
|
|
75
91
|
} catch (error) {
|
|
76
92
|
const parseError = error;
|
|
77
|
-
|
|
93
|
+
logger.error("Failed to parse queue file", { err: parseError });
|
|
78
94
|
const backupFile = await backupCorruptedQueueFile(content);
|
|
79
|
-
|
|
95
|
+
logger.error("Backed up corrupted queue file and recreated queue.json", { backupFile });
|
|
80
96
|
return [];
|
|
81
97
|
}
|
|
82
98
|
}
|
|
@@ -126,58 +142,67 @@ function createLocalQueue(name, options) {
|
|
|
126
142
|
return new Date(job.availableAt).getTime() <= Date.now();
|
|
127
143
|
});
|
|
128
144
|
const jobsToProcess = options2?.limit ? pendingJobs.slice(0, options2.limit) : pendingJobs;
|
|
145
|
+
for (const job of jobsToProcess) {
|
|
146
|
+
inFlightJobIds.add(job.id);
|
|
147
|
+
}
|
|
129
148
|
let processed = 0;
|
|
130
149
|
let failed = 0;
|
|
131
150
|
let lastJobId;
|
|
132
151
|
const completedJobIds = /* @__PURE__ */ new Set();
|
|
133
152
|
const deadJobIds = /* @__PURE__ */ new Set();
|
|
134
153
|
const retryUpdates = /* @__PURE__ */ new Map();
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
154
|
+
try {
|
|
155
|
+
for (const job of jobsToProcess) {
|
|
156
|
+
const attemptNumber = (job.attemptCount ?? 0) + 1;
|
|
157
|
+
try {
|
|
158
|
+
await Promise.resolve(
|
|
159
|
+
handler(job, {
|
|
160
|
+
jobId: job.id,
|
|
161
|
+
attemptNumber,
|
|
162
|
+
queueName: name
|
|
163
|
+
})
|
|
164
|
+
);
|
|
165
|
+
processed++;
|
|
166
|
+
lastJobId = job.id;
|
|
167
|
+
completedJobIds.add(job.id);
|
|
168
|
+
logger.info("Job completed", { jobId: job.id });
|
|
169
|
+
} catch (error) {
|
|
170
|
+
logger.error("Job failed", { jobId: job.id, attemptNumber, maxAttempts: DEFAULT_MAX_ATTEMPTS, err: error });
|
|
171
|
+
failed++;
|
|
172
|
+
lastJobId = job.id;
|
|
173
|
+
if (attemptNumber >= DEFAULT_MAX_ATTEMPTS) {
|
|
174
|
+
logger.error("Job exhausted all attempts, moving to dead letter", { jobId: job.id, maxAttempts: DEFAULT_MAX_ATTEMPTS });
|
|
175
|
+
deadJobIds.add(job.id);
|
|
176
|
+
} else {
|
|
177
|
+
const backoffMs = RETRY_BACKOFF_BASE_MS * Math.pow(2, attemptNumber - 1);
|
|
178
|
+
retryUpdates.set(job.id, {
|
|
179
|
+
...job,
|
|
180
|
+
attemptCount: attemptNumber,
|
|
181
|
+
availableAt: new Date(Date.now() + backoffMs).toISOString()
|
|
182
|
+
});
|
|
183
|
+
}
|
|
163
184
|
}
|
|
164
185
|
}
|
|
186
|
+
const hasChanges = completedJobIds.size > 0 || deadJobIds.size > 0 || retryUpdates.size > 0;
|
|
187
|
+
if (hasChanges) {
|
|
188
|
+
await withFileLock(async () => {
|
|
189
|
+
const currentJobs = await readQueue();
|
|
190
|
+
const updatedJobs = currentJobs.filter((j) => !completedJobIds.has(j.id) && !deadJobIds.has(j.id)).map((j) => retryUpdates.get(j.id) ?? j);
|
|
191
|
+
await writeQueue(updatedJobs);
|
|
192
|
+
const newState = {
|
|
193
|
+
lastProcessedId: lastJobId,
|
|
194
|
+
completedCount: (state.completedCount ?? 0) + processed,
|
|
195
|
+
failedCount: (state.failedCount ?? 0) + deadJobIds.size
|
|
196
|
+
};
|
|
197
|
+
await writeState(newState);
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
return { processed, failed, lastJobId };
|
|
201
|
+
} finally {
|
|
202
|
+
for (const job of jobsToProcess) {
|
|
203
|
+
inFlightJobIds.delete(job.id);
|
|
204
|
+
}
|
|
165
205
|
}
|
|
166
|
-
const hasChanges = completedJobIds.size > 0 || deadJobIds.size > 0 || retryUpdates.size > 0;
|
|
167
|
-
if (hasChanges) {
|
|
168
|
-
await withFileLock(async () => {
|
|
169
|
-
const currentJobs = await readQueue();
|
|
170
|
-
const updatedJobs = currentJobs.filter((j) => !completedJobIds.has(j.id) && !deadJobIds.has(j.id)).map((j) => retryUpdates.get(j.id) ?? j);
|
|
171
|
-
await writeQueue(updatedJobs);
|
|
172
|
-
const newState = {
|
|
173
|
-
lastProcessedId: lastJobId,
|
|
174
|
-
completedCount: (state.completedCount ?? 0) + processed,
|
|
175
|
-
failedCount: (state.failedCount ?? 0) + deadJobIds.size
|
|
176
|
-
};
|
|
177
|
-
await writeState(newState);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
return { processed, failed, lastJobId };
|
|
181
206
|
}
|
|
182
207
|
async function pollAndProcess() {
|
|
183
208
|
if (isProcessing || !activeHandler) return;
|
|
@@ -185,7 +210,7 @@ function createLocalQueue(name, options) {
|
|
|
185
210
|
try {
|
|
186
211
|
await processBatch(activeHandler);
|
|
187
212
|
} catch (error) {
|
|
188
|
-
|
|
213
|
+
logger.error("Polling error", { err: error });
|
|
189
214
|
} finally {
|
|
190
215
|
isProcessing = false;
|
|
191
216
|
}
|
|
@@ -198,10 +223,10 @@ function createLocalQueue(name, options) {
|
|
|
198
223
|
await processBatch(handler);
|
|
199
224
|
pollingTimer = setInterval(() => {
|
|
200
225
|
pollAndProcess().catch((err) => {
|
|
201
|
-
|
|
226
|
+
logger.error("Poll cycle error", { err });
|
|
202
227
|
});
|
|
203
228
|
}, pollInterval);
|
|
204
|
-
|
|
229
|
+
logger.info("Worker started", { concurrency });
|
|
205
230
|
return { processed: -1, failed: -1, lastJobId: void 0 };
|
|
206
231
|
}
|
|
207
232
|
async function clear() {
|
|
@@ -217,6 +242,17 @@ function createLocalQueue(name, options) {
|
|
|
217
242
|
return { removed };
|
|
218
243
|
});
|
|
219
244
|
}
|
|
245
|
+
async function removeQueuedJobsByScope(scope) {
|
|
246
|
+
return withFileLock(async () => {
|
|
247
|
+
const jobs = await readQueue();
|
|
248
|
+
const retainedJobs = jobs.filter((job) => inFlightJobIds.has(job.id) || !payloadMatchesScope(job.payload, scope));
|
|
249
|
+
const removed = jobs.length - retainedJobs.length;
|
|
250
|
+
if (removed > 0) {
|
|
251
|
+
await writeQueue(retainedJobs);
|
|
252
|
+
}
|
|
253
|
+
return { removed };
|
|
254
|
+
});
|
|
255
|
+
}
|
|
220
256
|
async function close() {
|
|
221
257
|
if (pollingTimer) {
|
|
222
258
|
clearInterval(pollingTimer);
|
|
@@ -227,7 +263,7 @@ function createLocalQueue(name, options) {
|
|
|
227
263
|
const startTime = Date.now();
|
|
228
264
|
while (isProcessing) {
|
|
229
265
|
if (Date.now() - startTime > SHUTDOWN_TIMEOUT) {
|
|
230
|
-
|
|
266
|
+
logger.warn("Force closing after shutdown timeout", { timeoutMs: SHUTDOWN_TIMEOUT });
|
|
231
267
|
break;
|
|
232
268
|
}
|
|
233
269
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
@@ -253,6 +289,7 @@ function createLocalQueue(name, options) {
|
|
|
253
289
|
enqueue,
|
|
254
290
|
process,
|
|
255
291
|
clear,
|
|
292
|
+
removeQueuedJobsByScope,
|
|
256
293
|
close,
|
|
257
294
|
getJobCounts
|
|
258
295
|
};
|
|
@@ -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 type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions } from '../types'\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\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 * - No retry mechanism for failed jobs\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 // 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\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 console.error(`[queue:${name}] Failed to read queue file:`, readError.message)\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 console.error(`[queue:${name}] Failed to read queue file:`, parseError.message)\n const backupFile = await backupCorruptedQueueFile(content)\n console.error(`[queue:${name}] Backed up corrupted queue file to ${backupFile} and recreated queue.json`)\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 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 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 console.log(`[queue:${name}] Job ${job.id} completed`)\n } catch (error) {\n console.error(`[queue:${name}] Job ${job.id} failed (attempt ${attemptNumber}/${DEFAULT_MAX_ATTEMPTS}):`, error)\n failed++\n lastJobId = job.id\n if (attemptNumber >= DEFAULT_MAX_ATTEMPTS) {\n console.error(`[queue:${name}] Job ${job.id} exhausted all ${DEFAULT_MAX_ATTEMPTS} attempts, moving to dead letter`)\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 }\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 console.error(`[queue:${name}] Polling error:`, 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 console.error(`[queue:${name}] Poll cycle error:`, err)\n })\n }, pollInterval)\n\n console.log(`[queue:${name}] Worker started with concurrency ${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 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 console.warn(`[queue:${name}] Force closing after ${SHUTDOWN_TIMEOUT}ms 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 close,\n getJobCounts,\n }\n}\n"],
|
|
5
|
-
"mappings": "AAAA,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,YAAY;
|
|
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\n * backoff and moved to a dead-letter store once attempts are exhausted (see the\n * 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, moving to dead letter', { 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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L;AAAA,IACA,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["options"]
|
|
7
7
|
}
|
package/dist/worker/registry.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { applyWorkerOverridesToDescriptors } from "@open-mercato/shared/modules/overrides";
|
|
2
|
+
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
3
|
+
const logger = createLogger("queue").child({ component: "worker-registry" });
|
|
2
4
|
const workers = /* @__PURE__ */ new Map();
|
|
3
5
|
function registerWorker(worker) {
|
|
4
6
|
if (workers.has(worker.id)) {
|
|
5
|
-
|
|
7
|
+
logger.warn("Worker already registered, overwriting", { workerId: worker.id });
|
|
6
8
|
}
|
|
7
9
|
workers.set(worker.id, worker);
|
|
8
10
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/worker/registry.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Worker Registry\n *\n * Provides registration and lookup for auto-discovered queue workers.\n * Workers are registered during bootstrap and accessed by the CLI worker command.\n */\n\nimport type { WorkerDescriptor } from '../types'\nimport { applyWorkerOverridesToDescriptors } from '@open-mercato/shared/modules/overrides'\n\nconst workers: Map<string, WorkerDescriptor> = new Map()\n\n/**\n * Register a single worker.\n * @param worker - The worker descriptor to register\n */\nexport function registerWorker(worker: WorkerDescriptor): void {\n if (workers.has(worker.id)) {\n
|
|
5
|
-
"mappings": "AAQA,SAAS,yCAAyC;
|
|
4
|
+
"sourcesContent": ["/**\n * Worker Registry\n *\n * Provides registration and lookup for auto-discovered queue workers.\n * Workers are registered during bootstrap and accessed by the CLI worker command.\n */\n\nimport type { WorkerDescriptor } from '../types'\nimport { applyWorkerOverridesToDescriptors } from '@open-mercato/shared/modules/overrides'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('queue').child({ component: 'worker-registry' })\n\nconst workers: Map<string, WorkerDescriptor> = new Map()\n\n/**\n * Register a single worker.\n * @param worker - The worker descriptor to register\n */\nexport function registerWorker(worker: WorkerDescriptor): void {\n if (workers.has(worker.id)) {\n logger.warn('Worker already registered, overwriting', { workerId: worker.id })\n }\n workers.set(worker.id, worker)\n}\n\n/**\n * Register multiple workers at once (typically from module discovery).\n * @param list - Array of worker descriptors to register\n */\nexport function registerModuleWorkers(list: WorkerDescriptor[]): void {\n for (const worker of applyWorkerOverridesToDescriptors(list)) {\n registerWorker(worker)\n }\n}\n\n/**\n * Get all registered workers.\n * @returns Array of all worker descriptors\n */\nexport function getWorkers(): WorkerDescriptor[] {\n return Array.from(workers.values())\n}\n\n/**\n * Get workers registered for a specific queue.\n * @param queue - The queue name to filter by\n * @returns Array of workers for the specified queue\n */\nexport function getWorkersByQueue(queue: string): WorkerDescriptor[] {\n return Array.from(workers.values()).filter((w) => w.queue === queue)\n}\n\n/**\n * Get a specific worker by ID.\n * @param id - The worker ID to look up\n * @returns The worker descriptor if found, undefined otherwise\n */\nexport function getWorker(id: string): WorkerDescriptor | undefined {\n return workers.get(id)\n}\n\n/**\n * Get all unique queue names that have registered workers.\n * @returns Array of queue names\n */\nexport function getRegisteredQueues(): string[] {\n const queues = new Set<string>()\n for (const worker of workers.values()) {\n queues.add(worker.queue)\n }\n return Array.from(queues)\n}\n\n/**\n * Clear all registered workers (useful for testing).\n */\nexport function clearWorkers(): void {\n workers.clear()\n}\n"],
|
|
5
|
+
"mappings": "AAQA,SAAS,yCAAyC;AAClD,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,OAAO,EAAE,MAAM,EAAE,WAAW,kBAAkB,CAAC;AAE3E,MAAM,UAAyC,oBAAI,IAAI;AAMhD,SAAS,eAAe,QAAgC;AAC7D,MAAI,QAAQ,IAAI,OAAO,EAAE,GAAG;AAC1B,WAAO,KAAK,0CAA0C,EAAE,UAAU,OAAO,GAAG,CAAC;AAAA,EAC/E;AACA,UAAQ,IAAI,OAAO,IAAI,MAAM;AAC/B;AAMO,SAAS,sBAAsB,MAAgC;AACpE,aAAW,UAAU,kCAAkC,IAAI,GAAG;AAC5D,mBAAe,MAAM;AAAA,EACvB;AACF;AAMO,SAAS,aAAiC;AAC/C,SAAO,MAAM,KAAK,QAAQ,OAAO,CAAC;AACpC;AAOO,SAAS,kBAAkB,OAAmC;AACnE,SAAO,MAAM,KAAK,QAAQ,OAAO,CAAC,EAAE,OAAO,CAAC,MAAM,EAAE,UAAU,KAAK;AACrE;AAOO,SAAS,UAAU,IAA0C;AAClE,SAAO,QAAQ,IAAI,EAAE;AACvB;AAMO,SAAS,sBAAgC;AAC9C,QAAM,SAAS,oBAAI,IAAY;AAC/B,aAAW,UAAU,QAAQ,OAAO,GAAG;AACrC,WAAO,IAAI,OAAO,KAAK;AAAA,EACzB;AACA,SAAO,MAAM,KAAK,MAAM;AAC1B;AAKO,SAAS,eAAqB;AACnC,UAAQ,MAAM;AAChB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/worker/runner.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createQueue } from "../factory.js";
|
|
2
|
+
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
3
|
+
const logger = createLogger("queue").child({ component: "worker" });
|
|
2
4
|
const managedQueues = /* @__PURE__ */ new Set();
|
|
3
5
|
let shutdownHandlersRegistered = false;
|
|
4
6
|
let shutdownInProgress = false;
|
|
@@ -12,21 +14,21 @@ function registerShutdownHandlers() {
|
|
|
12
14
|
const shutdown = async (signal) => {
|
|
13
15
|
if (shutdownInProgress) return;
|
|
14
16
|
shutdownInProgress = true;
|
|
15
|
-
|
|
17
|
+
logger.info("Received signal, shutting down gracefully", { signal });
|
|
16
18
|
let hasError = false;
|
|
17
19
|
for (const queue of managedQueues) {
|
|
18
20
|
try {
|
|
19
21
|
await queue.close();
|
|
20
22
|
} catch (error) {
|
|
21
23
|
hasError = true;
|
|
22
|
-
|
|
24
|
+
logger.error("Error during shutdown", { err: error });
|
|
23
25
|
}
|
|
24
26
|
}
|
|
25
27
|
managedQueues.clear();
|
|
26
28
|
unregisterShutdownHandlers(sigtermHandler, sigintHandler);
|
|
27
29
|
shutdownInProgress = false;
|
|
28
30
|
if (!hasError) {
|
|
29
|
-
|
|
31
|
+
logger.info("Worker closed successfully");
|
|
30
32
|
}
|
|
31
33
|
process.exit(hasError ? 1 : 0);
|
|
32
34
|
};
|
|
@@ -51,7 +53,7 @@ async function runWorker(options) {
|
|
|
51
53
|
strategy: strategyOption
|
|
52
54
|
} = options;
|
|
53
55
|
const strategy = strategyOption ?? (process.env.QUEUE_STRATEGY === "async" ? "async" : "local");
|
|
54
|
-
|
|
56
|
+
logger.info("Starting worker for queue", { queueName, strategy });
|
|
55
57
|
const queue = createQueue(queueName, strategy, {
|
|
56
58
|
connection,
|
|
57
59
|
concurrency
|
|
@@ -61,11 +63,11 @@ async function runWorker(options) {
|
|
|
61
63
|
registerShutdownHandlers();
|
|
62
64
|
}
|
|
63
65
|
await queue.process(handler);
|
|
64
|
-
|
|
66
|
+
logger.info("Worker running", { concurrency });
|
|
65
67
|
if (background) {
|
|
66
68
|
return;
|
|
67
69
|
}
|
|
68
|
-
|
|
70
|
+
logger.info("Press Ctrl+C to stop");
|
|
69
71
|
await new Promise(() => {
|
|
70
72
|
});
|
|
71
73
|
}
|
|
@@ -74,7 +76,7 @@ function createRoutedHandler(handlers) {
|
|
|
74
76
|
const type = job.payload.type;
|
|
75
77
|
const handler = handlers[type];
|
|
76
78
|
if (!handler) {
|
|
77
|
-
|
|
79
|
+
logger.warn("No handler registered for job type", { type });
|
|
78
80
|
return;
|
|
79
81
|
}
|
|
80
82
|
await handler(job, ctx);
|