@open-mercato/queue 0.6.6-develop.6429.1.2fba7258d1 → 0.6.6-develop.6450.1.b493fb430c
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/dist/strategies/async.js +28 -0
- package/dist/strategies/async.js.map +2 -2
- package/dist/strategies/local.js +77 -43
- package/dist/strategies/local.js.map +2 -2
- package/package.json +2 -2
- package/src/__tests__/async.strategy.test.ts +48 -0
- package/src/__tests__/local.strategy.test.ts +77 -0
- package/src/strategies/async.ts +40 -1
- package/src/strategies/local.ts +86 -49
- package/src/types.ts +13 -0
package/dist/strategies/async.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import { getRedisUrlOrThrow } from "@open-mercato/shared/lib/redis/connection";
|
|
2
|
+
const REMOVABLE_JOB_STATES = ["waiting", "delayed", "prioritized", "paused", "waiting-children"];
|
|
3
|
+
function payloadMatchesScope(payload, scope) {
|
|
4
|
+
if (!payload || typeof payload !== "object") return false;
|
|
5
|
+
const scopedPayload = payload;
|
|
6
|
+
if (scopedPayload.tenantId !== scope.tenantId) return false;
|
|
7
|
+
if (scope.organizationId !== void 0) {
|
|
8
|
+
if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false;
|
|
9
|
+
}
|
|
10
|
+
if (scope.jobTypes?.length) {
|
|
11
|
+
return typeof scopedPayload.jobType === "string" && scope.jobTypes.includes(scopedPayload.jobType);
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
2
15
|
function resolveConnection(options) {
|
|
3
16
|
if (options?.url) {
|
|
4
17
|
return { url: options.url };
|
|
@@ -94,6 +107,20 @@ function createAsyncQueue(name, options) {
|
|
|
94
107
|
await queue.obliterate({ force: true });
|
|
95
108
|
return { removed: -1 };
|
|
96
109
|
}
|
|
110
|
+
async function removeQueuedJobsByScope(scope) {
|
|
111
|
+
const queue = await getQueue();
|
|
112
|
+
const jobs = await queue.getJobs(REMOVABLE_JOB_STATES, 0, -1);
|
|
113
|
+
let removed = 0;
|
|
114
|
+
for (const job of jobs) {
|
|
115
|
+
if (!payloadMatchesScope(job.data?.payload, scope)) continue;
|
|
116
|
+
try {
|
|
117
|
+
await job.remove();
|
|
118
|
+
removed++;
|
|
119
|
+
} catch {
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { removed };
|
|
123
|
+
}
|
|
97
124
|
async function close() {
|
|
98
125
|
if (bullWorker) {
|
|
99
126
|
await bullWorker.close();
|
|
@@ -120,6 +147,7 @@ function createAsyncQueue(name, options) {
|
|
|
120
147
|
enqueue,
|
|
121
148
|
process,
|
|
122
149
|
clear,
|
|
150
|
+
removeQueuedJobsByScope,
|
|
123
151
|
close,
|
|
124
152
|
getJobCounts
|
|
125
153
|
};
|
|
@@ -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 console.log(`[queue:${name}] Job ${jobWithId.id} completed`)\n })\n\n bullWorker.on('failed', (job, err) => {\n const jobWithId = job as { id?: string } | undefined\n const error = err as Error\n console.error(`[queue:${name}] Job ${jobWithId?.id} failed:`, error.message)\n })\n\n bullWorker.on('error', (err) => {\n const error = err as Error\n console.error(`[queue:${name}] Worker error:`, error.message)\n })\n\n console.log(`[queue:${name}] Worker started with concurrency ${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 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 close,\n getJobCounts,\n }\n}\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'\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\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 console.log(`[queue:${name}] Job ${jobWithId.id} completed`)\n })\n\n bullWorker.on('failed', (job, err) => {\n const jobWithId = job as { id?: string } | undefined\n const error = err as Error\n console.error(`[queue:${name}] Job ${jobWithId?.id} failed:`, error.message)\n })\n\n bullWorker.on('error', (err) => {\n const error = err as Error\n console.error(`[queue:${name}] Worker error:`, error.message)\n })\n\n console.log(`[queue:${name}] Worker started with concurrency ${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;AAiDnC,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,W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,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,18 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import crypto from "node:crypto";
|
|
4
|
+
function payloadMatchesScope(payload, scope) {
|
|
5
|
+
if (!payload || typeof payload !== "object") return false;
|
|
6
|
+
const scopedPayload = payload;
|
|
7
|
+
if (scopedPayload.tenantId !== scope.tenantId) return false;
|
|
8
|
+
if (scope.organizationId !== void 0) {
|
|
9
|
+
if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false;
|
|
10
|
+
}
|
|
11
|
+
if (scope.jobTypes?.length) {
|
|
12
|
+
return typeof scopedPayload.jobType === "string" && scope.jobTypes.includes(scopedPayload.jobType);
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
4
16
|
const DEFAULT_POLL_INTERVAL = 1e3;
|
|
5
17
|
const DEFAULT_LOCAL_QUEUE_BASE_DIR = ".mercato/queue";
|
|
6
18
|
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
@@ -18,6 +30,7 @@ function createLocalQueue(name, options) {
|
|
|
18
30
|
let pollingTimer = null;
|
|
19
31
|
let isProcessing = false;
|
|
20
32
|
let activeHandler = null;
|
|
33
|
+
const inFlightJobIds = /* @__PURE__ */ new Set();
|
|
21
34
|
let fileOpChain = Promise.resolve();
|
|
22
35
|
function withFileLock(fn) {
|
|
23
36
|
const run = fileOpChain.then(() => fn(), () => fn());
|
|
@@ -126,58 +139,67 @@ function createLocalQueue(name, options) {
|
|
|
126
139
|
return new Date(job.availableAt).getTime() <= Date.now();
|
|
127
140
|
});
|
|
128
141
|
const jobsToProcess = options2?.limit ? pendingJobs.slice(0, options2.limit) : pendingJobs;
|
|
142
|
+
for (const job of jobsToProcess) {
|
|
143
|
+
inFlightJobIds.add(job.id);
|
|
144
|
+
}
|
|
129
145
|
let processed = 0;
|
|
130
146
|
let failed = 0;
|
|
131
147
|
let lastJobId;
|
|
132
148
|
const completedJobIds = /* @__PURE__ */ new Set();
|
|
133
149
|
const deadJobIds = /* @__PURE__ */ new Set();
|
|
134
150
|
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
|
-
|
|
151
|
+
try {
|
|
152
|
+
for (const job of jobsToProcess) {
|
|
153
|
+
const attemptNumber = (job.attemptCount ?? 0) + 1;
|
|
154
|
+
try {
|
|
155
|
+
await Promise.resolve(
|
|
156
|
+
handler(job, {
|
|
157
|
+
jobId: job.id,
|
|
158
|
+
attemptNumber,
|
|
159
|
+
queueName: name
|
|
160
|
+
})
|
|
161
|
+
);
|
|
162
|
+
processed++;
|
|
163
|
+
lastJobId = job.id;
|
|
164
|
+
completedJobIds.add(job.id);
|
|
165
|
+
console.log(`[queue:${name}] Job ${job.id} completed`);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
console.error(`[queue:${name}] Job ${job.id} failed (attempt ${attemptNumber}/${DEFAULT_MAX_ATTEMPTS}):`, error);
|
|
168
|
+
failed++;
|
|
169
|
+
lastJobId = job.id;
|
|
170
|
+
if (attemptNumber >= DEFAULT_MAX_ATTEMPTS) {
|
|
171
|
+
console.error(`[queue:${name}] Job ${job.id} exhausted all ${DEFAULT_MAX_ATTEMPTS} attempts, moving to dead letter`);
|
|
172
|
+
deadJobIds.add(job.id);
|
|
173
|
+
} else {
|
|
174
|
+
const backoffMs = RETRY_BACKOFF_BASE_MS * Math.pow(2, attemptNumber - 1);
|
|
175
|
+
retryUpdates.set(job.id, {
|
|
176
|
+
...job,
|
|
177
|
+
attemptCount: attemptNumber,
|
|
178
|
+
availableAt: new Date(Date.now() + backoffMs).toISOString()
|
|
179
|
+
});
|
|
180
|
+
}
|
|
163
181
|
}
|
|
164
182
|
}
|
|
183
|
+
const hasChanges = completedJobIds.size > 0 || deadJobIds.size > 0 || retryUpdates.size > 0;
|
|
184
|
+
if (hasChanges) {
|
|
185
|
+
await withFileLock(async () => {
|
|
186
|
+
const currentJobs = await readQueue();
|
|
187
|
+
const updatedJobs = currentJobs.filter((j) => !completedJobIds.has(j.id) && !deadJobIds.has(j.id)).map((j) => retryUpdates.get(j.id) ?? j);
|
|
188
|
+
await writeQueue(updatedJobs);
|
|
189
|
+
const newState = {
|
|
190
|
+
lastProcessedId: lastJobId,
|
|
191
|
+
completedCount: (state.completedCount ?? 0) + processed,
|
|
192
|
+
failedCount: (state.failedCount ?? 0) + deadJobIds.size
|
|
193
|
+
};
|
|
194
|
+
await writeState(newState);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return { processed, failed, lastJobId };
|
|
198
|
+
} finally {
|
|
199
|
+
for (const job of jobsToProcess) {
|
|
200
|
+
inFlightJobIds.delete(job.id);
|
|
201
|
+
}
|
|
165
202
|
}
|
|
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
203
|
}
|
|
182
204
|
async function pollAndProcess() {
|
|
183
205
|
if (isProcessing || !activeHandler) return;
|
|
@@ -217,6 +239,17 @@ function createLocalQueue(name, options) {
|
|
|
217
239
|
return { removed };
|
|
218
240
|
});
|
|
219
241
|
}
|
|
242
|
+
async function removeQueuedJobsByScope(scope) {
|
|
243
|
+
return withFileLock(async () => {
|
|
244
|
+
const jobs = await readQueue();
|
|
245
|
+
const retainedJobs = jobs.filter((job) => inFlightJobIds.has(job.id) || !payloadMatchesScope(job.payload, scope));
|
|
246
|
+
const removed = jobs.length - retainedJobs.length;
|
|
247
|
+
if (removed > 0) {
|
|
248
|
+
await writeQueue(retainedJobs);
|
|
249
|
+
}
|
|
250
|
+
return { removed };
|
|
251
|
+
});
|
|
252
|
+
}
|
|
220
253
|
async function close() {
|
|
221
254
|
if (pollingTimer) {
|
|
222
255
|
clearInterval(pollingTimer);
|
|
@@ -253,6 +286,7 @@ function createLocalQueue(name, options) {
|
|
|
253
286
|
enqueue,
|
|
254
287
|
process,
|
|
255
288
|
clear,
|
|
289
|
+
removeQueuedJobsByScope,
|
|
256
290
|
close,
|
|
257
291
|
getJobCounts
|
|
258
292
|
};
|
|
@@ -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 *\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 // 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 type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions, QueueJobScope } 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\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 // 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 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 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 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 } 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 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 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 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 }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,IAAI,IAAI,YAAY,kBAAkB;AAC7C,gBAAQ,KAAK,UAAU,IAAI,yBAAyB,gBAAgB,YAAY;AAChF;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;",
|
|
6
6
|
"names": ["options"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/queue",
|
|
3
|
-
"version": "0.6.6-develop.
|
|
3
|
+
"version": "0.6.6-develop.6450.1.b493fb430c",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Multi-strategy job queue with local and BullMQ support",
|
|
6
6
|
"type": "module",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@open-mercato/shared": "0.6.6-develop.
|
|
55
|
+
"@open-mercato/shared": "0.6.6-develop.6450.1.b493fb430c"
|
|
56
56
|
},
|
|
57
57
|
"repository": {
|
|
58
58
|
"type": "git",
|
|
@@ -6,6 +6,7 @@ const workerCtor = jest.fn()
|
|
|
6
6
|
const queueAdd = jest.fn(async () => ({ id: 'bull-job-id' }))
|
|
7
7
|
const queueClose = jest.fn(async () => {})
|
|
8
8
|
const queueObliterate = jest.fn(async () => {})
|
|
9
|
+
const queueGetJobs = jest.fn(async () => [])
|
|
9
10
|
const queueGetJobCounts = jest.fn(async () => ({
|
|
10
11
|
waiting: 2,
|
|
11
12
|
active: 1,
|
|
@@ -29,6 +30,7 @@ jest.mock('bullmq', () => {
|
|
|
29
30
|
close = queueClose
|
|
30
31
|
obliterate = queueObliterate
|
|
31
32
|
getJobCounts = queueGetJobCounts
|
|
33
|
+
getJobs = queueGetJobs
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
class MockWorker<T> {
|
|
@@ -112,6 +114,52 @@ describe('Queue - async strategy', () => {
|
|
|
112
114
|
await queue.close()
|
|
113
115
|
})
|
|
114
116
|
|
|
117
|
+
it('removeQueuedJobsByScope removes only queued jobs matching tenant scope', async () => {
|
|
118
|
+
const removeMatching = jest.fn(async () => {})
|
|
119
|
+
const removeAutoIndex = jest.fn(async () => {})
|
|
120
|
+
const removeOtherOrg = jest.fn(async () => {})
|
|
121
|
+
const removeOtherTenant = jest.fn(async () => {})
|
|
122
|
+
queueGetJobs.mockResolvedValueOnce([
|
|
123
|
+
{
|
|
124
|
+
data: { payload: { tenantId: 'tenant-1', organizationId: 'org-1', jobType: 'batch-index', value: 1 } },
|
|
125
|
+
remove: removeMatching,
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
data: { payload: { tenantId: 'tenant-1', organizationId: 'org-1', jobType: 'index', value: 2 } },
|
|
129
|
+
remove: removeAutoIndex,
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
data: { payload: { tenantId: 'tenant-1', organizationId: 'org-2', value: 2 } },
|
|
133
|
+
remove: removeOtherOrg,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
data: { payload: { tenantId: 'tenant-2', organizationId: 'org-1', value: 3 } },
|
|
137
|
+
remove: removeOtherTenant,
|
|
138
|
+
},
|
|
139
|
+
])
|
|
140
|
+
const queue = createQueue<{ tenantId: string; organizationId?: string | null; value: number }>(
|
|
141
|
+
'test-queue',
|
|
142
|
+
'async',
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
const result = await queue.removeQueuedJobsByScope!({
|
|
146
|
+
tenantId: 'tenant-1',
|
|
147
|
+
organizationId: 'org-1',
|
|
148
|
+
jobTypes: ['batch-index'],
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
expect(queueGetJobs).toHaveBeenCalledWith(
|
|
152
|
+
['waiting', 'delayed', 'prioritized', 'paused', 'waiting-children'],
|
|
153
|
+
0,
|
|
154
|
+
-1,
|
|
155
|
+
)
|
|
156
|
+
expect(result.removed).toBe(1)
|
|
157
|
+
expect(removeMatching).toHaveBeenCalledTimes(1)
|
|
158
|
+
expect(removeAutoIndex).not.toHaveBeenCalled()
|
|
159
|
+
expect(removeOtherOrg).not.toHaveBeenCalled()
|
|
160
|
+
expect(removeOtherTenant).not.toHaveBeenCalled()
|
|
161
|
+
})
|
|
162
|
+
|
|
115
163
|
it('keeps structured Redis options when host-based config is used', async () => {
|
|
116
164
|
const queue = createQueue<{ value: number }>('test-queue', 'async', {
|
|
117
165
|
connection: {
|
|
@@ -107,6 +107,83 @@ describe('Queue - local strategy', () => {
|
|
|
107
107
|
await queue.close()
|
|
108
108
|
})
|
|
109
109
|
|
|
110
|
+
test('removeQueuedJobsByScope removes only matching tenant scoped jobs', async () => {
|
|
111
|
+
const queue = createQueue<{
|
|
112
|
+
tenantId?: string
|
|
113
|
+
organizationId?: string | null
|
|
114
|
+
jobType?: string
|
|
115
|
+
value: number
|
|
116
|
+
}>('test-queue', 'local')
|
|
117
|
+
const queuePath = path.join('.mercato', 'queue', 'test-queue', 'queue.json')
|
|
118
|
+
|
|
119
|
+
await queue.enqueue({ tenantId: 'tenant-1', organizationId: 'org-1', jobType: 'batch-index', value: 1 })
|
|
120
|
+
await queue.enqueue({ tenantId: 'tenant-1', organizationId: 'org-1', jobType: 'index', value: 2 })
|
|
121
|
+
await queue.enqueue({ tenantId: 'tenant-1', organizationId: 'org-2', jobType: 'batch-index', value: 3 })
|
|
122
|
+
await queue.enqueue({ tenantId: 'tenant-2', organizationId: 'org-1', jobType: 'batch-index', value: 4 })
|
|
123
|
+
await queue.enqueue({ tenantId: 'tenant-1', organizationId: null, jobType: 'batch-index', value: 5 })
|
|
124
|
+
await queue.enqueue({ jobType: 'batch-index', value: 6 })
|
|
125
|
+
|
|
126
|
+
const scopedResult = await queue.removeQueuedJobsByScope!({
|
|
127
|
+
tenantId: 'tenant-1',
|
|
128
|
+
organizationId: 'org-1',
|
|
129
|
+
jobTypes: ['batch-index'],
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
expect(scopedResult.removed).toBe(1)
|
|
133
|
+
let remaining = readJson(queuePath)
|
|
134
|
+
expect(remaining.map((job: { payload: { value: number } }) => job.payload.value)).toEqual([2, 3, 4, 5, 6])
|
|
135
|
+
|
|
136
|
+
const tenantResult = await queue.removeQueuedJobsByScope!({ tenantId: 'tenant-1', jobTypes: ['batch-index'] })
|
|
137
|
+
|
|
138
|
+
expect(tenantResult.removed).toBe(2)
|
|
139
|
+
remaining = readJson(queuePath)
|
|
140
|
+
expect(remaining.map((job: { payload: { value: number } }) => job.payload.value)).toEqual([2, 4, 6])
|
|
141
|
+
|
|
142
|
+
await queue.close()
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('removeQueuedJobsByScope preserves in-flight local jobs', async () => {
|
|
146
|
+
const queue = createQueue<{
|
|
147
|
+
tenantId: string
|
|
148
|
+
organizationId: string
|
|
149
|
+
jobType: string
|
|
150
|
+
value: number
|
|
151
|
+
}>('test-queue', 'local')
|
|
152
|
+
const queuePath = path.join('.mercato', 'queue', 'test-queue', 'queue.json')
|
|
153
|
+
let release!: () => void
|
|
154
|
+
const releasePromise = new Promise<void>((resolve) => {
|
|
155
|
+
release = resolve
|
|
156
|
+
})
|
|
157
|
+
let started!: () => void
|
|
158
|
+
const startedPromise = new Promise<void>((resolve) => {
|
|
159
|
+
started = resolve
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
await queue.enqueue({ tenantId: 'tenant-1', organizationId: 'org-1', jobType: 'batch-index', value: 1 })
|
|
163
|
+
await queue.enqueue({ tenantId: 'tenant-1', organizationId: 'org-1', jobType: 'batch-index', value: 2 })
|
|
164
|
+
const processing = queue.process(async () => {
|
|
165
|
+
started()
|
|
166
|
+
await releasePromise
|
|
167
|
+
}, { limit: 1 })
|
|
168
|
+
|
|
169
|
+
await startedPromise
|
|
170
|
+
const result = await queue.removeQueuedJobsByScope!({
|
|
171
|
+
tenantId: 'tenant-1',
|
|
172
|
+
organizationId: 'org-1',
|
|
173
|
+
jobTypes: ['batch-index'],
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
expect(result.removed).toBe(1)
|
|
177
|
+
let remaining = readJson(queuePath)
|
|
178
|
+
expect(remaining.map((job: { payload: { value: number } }) => job.payload.value)).toEqual([1])
|
|
179
|
+
|
|
180
|
+
release()
|
|
181
|
+
await processing
|
|
182
|
+
remaining = readJson(queuePath)
|
|
183
|
+
expect(remaining).toEqual([])
|
|
184
|
+
await queue.close()
|
|
185
|
+
})
|
|
186
|
+
|
|
110
187
|
test('getJobCounts returns correct counts', async () => {
|
|
111
188
|
const queue = createQueue<{ value: number }>('test-queue', 'local')
|
|
112
189
|
|
package/src/strategies/async.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Queue, QueuedJob, JobHandler, AsyncQueueOptions, ProcessResult, EnqueueOptions } from '../types'
|
|
1
|
+
import type { Queue, QueuedJob, JobHandler, AsyncQueueOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'
|
|
2
2
|
import { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'
|
|
3
3
|
|
|
4
4
|
// BullMQ interface types - we define the shape we use to maintain type safety
|
|
@@ -28,6 +28,10 @@ interface BullQueueInterface<T> {
|
|
|
28
28
|
obliterate: (opts?: { force?: boolean }) => Promise<void>
|
|
29
29
|
close: () => Promise<void>
|
|
30
30
|
getJobCounts: (...states: string[]) => Promise<Record<string, number>>
|
|
31
|
+
getJobs: (types: string[], start?: number, end?: number) => Promise<Array<{
|
|
32
|
+
data?: T
|
|
33
|
+
remove: () => Promise<void>
|
|
34
|
+
}>>
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
interface BullWorkerInterface {
|
|
@@ -44,6 +48,21 @@ interface BullMQModule {
|
|
|
44
48
|
) => BullWorkerInterface
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
const REMOVABLE_JOB_STATES = ['waiting', 'delayed', 'prioritized', 'paused', 'waiting-children']
|
|
52
|
+
|
|
53
|
+
function payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {
|
|
54
|
+
if (!payload || typeof payload !== 'object') return false
|
|
55
|
+
const scopedPayload = payload as { tenantId?: unknown; organizationId?: unknown; jobType?: unknown }
|
|
56
|
+
if (scopedPayload.tenantId !== scope.tenantId) return false
|
|
57
|
+
if (scope.organizationId !== undefined) {
|
|
58
|
+
if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false
|
|
59
|
+
}
|
|
60
|
+
if (scope.jobTypes?.length) {
|
|
61
|
+
return typeof scopedPayload.jobType === 'string' && scope.jobTypes.includes(scopedPayload.jobType)
|
|
62
|
+
}
|
|
63
|
+
return true
|
|
64
|
+
}
|
|
65
|
+
|
|
47
66
|
/**
|
|
48
67
|
* Resolves Redis connection options from various sources.
|
|
49
68
|
*
|
|
@@ -195,6 +214,25 @@ export function createAsyncQueue<T = unknown>(
|
|
|
195
214
|
return { removed: -1 } // BullMQ obliterate doesn't return count
|
|
196
215
|
}
|
|
197
216
|
|
|
217
|
+
async function removeQueuedJobsByScope(scope: QueueJobScope): Promise<{ removed: number }> {
|
|
218
|
+
const queue = await getQueue()
|
|
219
|
+
const jobs = await queue.getJobs(REMOVABLE_JOB_STATES, 0, -1)
|
|
220
|
+
let removed = 0
|
|
221
|
+
|
|
222
|
+
for (const job of jobs) {
|
|
223
|
+
if (!payloadMatchesScope(job.data?.payload, scope)) continue
|
|
224
|
+
try {
|
|
225
|
+
await job.remove()
|
|
226
|
+
removed++
|
|
227
|
+
} catch {
|
|
228
|
+
// The job may have started between enumeration and removal. In-flight
|
|
229
|
+
// cancellation is handled by the caller's lock/heartbeat contract.
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return { removed }
|
|
234
|
+
}
|
|
235
|
+
|
|
198
236
|
async function close(): Promise<void> {
|
|
199
237
|
if (bullWorker) {
|
|
200
238
|
await bullWorker.close()
|
|
@@ -228,6 +266,7 @@ export function createAsyncQueue<T = unknown>(
|
|
|
228
266
|
enqueue,
|
|
229
267
|
process,
|
|
230
268
|
clear,
|
|
269
|
+
removeQueuedJobsByScope,
|
|
231
270
|
close,
|
|
232
271
|
getJobCounts,
|
|
233
272
|
}
|
package/src/strategies/local.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs'
|
|
2
2
|
import path from 'node:path'
|
|
3
3
|
import crypto from 'node:crypto'
|
|
4
|
-
import type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions } from '../types'
|
|
4
|
+
import type { Queue, QueuedJob, JobHandler, LocalQueueOptions, ProcessOptions, ProcessResult, EnqueueOptions, QueueJobScope } from '../types'
|
|
5
5
|
|
|
6
6
|
type LocalState = {
|
|
7
7
|
lastProcessedId?: string
|
|
@@ -14,6 +14,19 @@ type StoredJob<T> = QueuedJob<T> & {
|
|
|
14
14
|
attemptCount?: number
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
function payloadMatchesScope(payload: unknown, scope: QueueJobScope): boolean {
|
|
18
|
+
if (!payload || typeof payload !== 'object') return false
|
|
19
|
+
const scopedPayload = payload as { tenantId?: unknown; organizationId?: unknown; jobType?: unknown }
|
|
20
|
+
if (scopedPayload.tenantId !== scope.tenantId) return false
|
|
21
|
+
if (scope.organizationId !== undefined) {
|
|
22
|
+
if ((scopedPayload.organizationId ?? null) !== scope.organizationId) return false
|
|
23
|
+
}
|
|
24
|
+
if (scope.jobTypes?.length) {
|
|
25
|
+
return typeof scopedPayload.jobType === 'string' && scope.jobTypes.includes(scopedPayload.jobType)
|
|
26
|
+
}
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
/** Default polling interval in milliseconds */
|
|
18
31
|
const DEFAULT_POLL_INTERVAL = 1000
|
|
19
32
|
const DEFAULT_LOCAL_QUEUE_BASE_DIR = '.mercato/queue'
|
|
@@ -65,6 +78,7 @@ export function createLocalQueue<T = unknown>(
|
|
|
65
78
|
let pollingTimer: ReturnType<typeof setInterval> | null = null
|
|
66
79
|
let isProcessing = false
|
|
67
80
|
let activeHandler: JobHandler<T> | null = null
|
|
81
|
+
const inFlightJobIds = new Set<string>()
|
|
68
82
|
|
|
69
83
|
// Per-queue mutex. Serializes read-modify-write segments so async fs calls
|
|
70
84
|
// cannot interleave and clobber each other's writes.
|
|
@@ -213,6 +227,10 @@ export function createLocalQueue<T = unknown>(
|
|
|
213
227
|
? pendingJobs.slice(0, options.limit)
|
|
214
228
|
: pendingJobs
|
|
215
229
|
|
|
230
|
+
for (const job of jobsToProcess) {
|
|
231
|
+
inFlightJobIds.add(job.id)
|
|
232
|
+
}
|
|
233
|
+
|
|
216
234
|
let processed = 0
|
|
217
235
|
let failed = 0
|
|
218
236
|
let lastJobId: string | undefined
|
|
@@ -220,58 +238,64 @@ export function createLocalQueue<T = unknown>(
|
|
|
220
238
|
const deadJobIds = new Set<string>()
|
|
221
239
|
const retryUpdates = new Map<string, StoredJob<T>>()
|
|
222
240
|
|
|
223
|
-
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
241
|
+
try {
|
|
242
|
+
for (const job of jobsToProcess) {
|
|
243
|
+
const attemptNumber = (job.attemptCount ?? 0) + 1
|
|
244
|
+
try {
|
|
245
|
+
await Promise.resolve(
|
|
246
|
+
handler(job, {
|
|
247
|
+
jobId: job.id,
|
|
248
|
+
attemptNumber,
|
|
249
|
+
queueName: name,
|
|
250
|
+
})
|
|
251
|
+
)
|
|
252
|
+
processed++
|
|
253
|
+
lastJobId = job.id
|
|
254
|
+
completedJobIds.add(job.id)
|
|
255
|
+
console.log(`[queue:${name}] Job ${job.id} completed`)
|
|
256
|
+
} catch (error) {
|
|
257
|
+
console.error(`[queue:${name}] Job ${job.id} failed (attempt ${attemptNumber}/${DEFAULT_MAX_ATTEMPTS}):`, error)
|
|
258
|
+
failed++
|
|
259
|
+
lastJobId = job.id
|
|
260
|
+
if (attemptNumber >= DEFAULT_MAX_ATTEMPTS) {
|
|
261
|
+
console.error(`[queue:${name}] Job ${job.id} exhausted all ${DEFAULT_MAX_ATTEMPTS} attempts, moving to dead letter`)
|
|
262
|
+
deadJobIds.add(job.id)
|
|
263
|
+
} else {
|
|
264
|
+
const backoffMs = RETRY_BACKOFF_BASE_MS * Math.pow(2, attemptNumber - 1)
|
|
265
|
+
retryUpdates.set(job.id, {
|
|
266
|
+
...job,
|
|
267
|
+
attemptCount: attemptNumber,
|
|
268
|
+
availableAt: new Date(Date.now() + backoffMs).toISOString(),
|
|
269
|
+
})
|
|
270
|
+
}
|
|
251
271
|
}
|
|
252
272
|
}
|
|
253
|
-
}
|
|
254
273
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
274
|
+
const hasChanges = completedJobIds.size > 0 || deadJobIds.size > 0 || retryUpdates.size > 0
|
|
275
|
+
if (hasChanges) {
|
|
276
|
+
await withFileLock(async () => {
|
|
277
|
+
// Re-read so jobs enqueued during handler execution are preserved.
|
|
278
|
+
const currentJobs = await readQueue()
|
|
279
|
+
const updatedJobs = currentJobs
|
|
280
|
+
.filter((j) => !completedJobIds.has(j.id) && !deadJobIds.has(j.id))
|
|
281
|
+
.map((j) => retryUpdates.get(j.id) ?? j)
|
|
282
|
+
await writeQueue(updatedJobs)
|
|
283
|
+
|
|
284
|
+
const newState: LocalState = {
|
|
285
|
+
lastProcessedId: lastJobId,
|
|
286
|
+
completedCount: (state.completedCount ?? 0) + processed,
|
|
287
|
+
failedCount: (state.failedCount ?? 0) + deadJobIds.size,
|
|
288
|
+
}
|
|
289
|
+
await writeState(newState)
|
|
290
|
+
})
|
|
291
|
+
}
|
|
273
292
|
|
|
274
|
-
|
|
293
|
+
return { processed, failed, lastJobId }
|
|
294
|
+
} finally {
|
|
295
|
+
for (const job of jobsToProcess) {
|
|
296
|
+
inFlightJobIds.delete(job.id)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
275
299
|
}
|
|
276
300
|
|
|
277
301
|
/**
|
|
@@ -334,6 +358,18 @@ export function createLocalQueue<T = unknown>(
|
|
|
334
358
|
})
|
|
335
359
|
}
|
|
336
360
|
|
|
361
|
+
async function removeQueuedJobsByScope(scope: QueueJobScope): Promise<{ removed: number }> {
|
|
362
|
+
return withFileLock(async () => {
|
|
363
|
+
const jobs = await readQueue()
|
|
364
|
+
const retainedJobs = jobs.filter((job) => inFlightJobIds.has(job.id) || !payloadMatchesScope(job.payload, scope))
|
|
365
|
+
const removed = jobs.length - retainedJobs.length
|
|
366
|
+
if (removed > 0) {
|
|
367
|
+
await writeQueue(retainedJobs)
|
|
368
|
+
}
|
|
369
|
+
return { removed }
|
|
370
|
+
})
|
|
371
|
+
}
|
|
372
|
+
|
|
337
373
|
async function close(): Promise<void> {
|
|
338
374
|
// Stop polling timer
|
|
339
375
|
if (pollingTimer) {
|
|
@@ -380,6 +416,7 @@ export function createLocalQueue<T = unknown>(
|
|
|
380
416
|
enqueue,
|
|
381
417
|
process,
|
|
382
418
|
clear,
|
|
419
|
+
removeQueuedJobsByScope,
|
|
383
420
|
close,
|
|
384
421
|
getJobCounts,
|
|
385
422
|
}
|
package/src/types.ts
CHANGED
|
@@ -135,6 +135,12 @@ export type ProcessResult = {
|
|
|
135
135
|
lastJobId?: string
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
export type QueueJobScope = {
|
|
139
|
+
tenantId: string
|
|
140
|
+
organizationId?: string | null
|
|
141
|
+
jobTypes?: readonly string[]
|
|
142
|
+
}
|
|
143
|
+
|
|
138
144
|
// ============================================================================
|
|
139
145
|
// Queue Interface
|
|
140
146
|
// ============================================================================
|
|
@@ -175,6 +181,13 @@ export interface Queue<T = unknown> {
|
|
|
175
181
|
*/
|
|
176
182
|
clear(): Promise<{ removed: number }>
|
|
177
183
|
|
|
184
|
+
/**
|
|
185
|
+
* Remove queued jobs whose payload belongs to the provided tenant/org scope.
|
|
186
|
+
* Active jobs are not forcibly terminated; callers should rely on their own
|
|
187
|
+
* cancellation/heartbeat contracts for in-flight work.
|
|
188
|
+
*/
|
|
189
|
+
removeQueuedJobsByScope?(scope: QueueJobScope): Promise<{ removed: number }>
|
|
190
|
+
|
|
178
191
|
/**
|
|
179
192
|
* Close the queue and release resources.
|
|
180
193
|
*/
|