@contractspec/lib.jobs 1.57.0 → 1.59.0
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/contracts/index.d.ts +494 -500
- package/dist/contracts/index.d.ts.map +1 -1
- package/dist/contracts/index.js +298 -461
- package/dist/entities/index.d.ts +117 -122
- package/dist/entities/index.d.ts.map +1 -1
- package/dist/entities/index.js +170 -193
- package/dist/events.d.ts +297 -303
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +199 -351
- package/dist/handlers/gmail-sync-handler.d.ts +5 -9
- package/dist/handlers/gmail-sync-handler.d.ts.map +1 -1
- package/dist/handlers/gmail-sync-handler.js +8 -8
- package/dist/handlers/index.d.ts +5 -9
- package/dist/handlers/index.d.ts.map +1 -1
- package/dist/handlers/index.js +53 -10
- package/dist/handlers/ping-job.d.ts +6 -10
- package/dist/handlers/ping-job.d.ts.map +1 -1
- package/dist/handlers/ping-job.js +13 -12
- package/dist/handlers/storage-document-handler.d.ts +7 -11
- package/dist/handlers/storage-document-handler.d.ts.map +1 -1
- package/dist/handlers/storage-document-handler.js +15 -13
- package/dist/index.d.ts +7 -24
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1366 -64
- package/dist/jobs.capability.d.ts +2 -7
- package/dist/jobs.capability.d.ts.map +1 -1
- package/dist/jobs.capability.js +29 -33
- package/dist/jobs.feature.d.ts +1 -6
- package/dist/jobs.feature.d.ts.map +1 -1
- package/dist/jobs.feature.js +45 -108
- package/dist/node/contracts/index.js +318 -0
- package/dist/node/entities/index.js +174 -0
- package/dist/node/events.js +200 -0
- package/dist/node/handlers/gmail-sync-handler.js +9 -0
- package/dist/node/handlers/index.js +55 -0
- package/dist/node/handlers/ping-job.js +14 -0
- package/dist/node/handlers/storage-document-handler.js +16 -0
- package/dist/node/index.js +1368 -0
- package/dist/node/jobs.capability.js +28 -0
- package/dist/node/jobs.feature.js +46 -0
- package/dist/node/queue/gcp-cloud-tasks.js +66 -0
- package/dist/node/queue/gcp-pubsub.js +54 -0
- package/dist/node/queue/index.js +478 -0
- package/dist/node/queue/memory-queue.js +160 -0
- package/dist/node/queue/register-defined-job.js +15 -0
- package/dist/node/queue/scaleway-sqs-queue.js +206 -0
- package/dist/node/queue/types.js +10 -0
- package/dist/node/scheduler/index.js +117 -0
- package/dist/queue/gcp-cloud-tasks.d.ts +33 -36
- package/dist/queue/gcp-cloud-tasks.d.ts.map +1 -1
- package/dist/queue/gcp-cloud-tasks.js +65 -59
- package/dist/queue/gcp-pubsub.d.ts +18 -21
- package/dist/queue/gcp-pubsub.d.ts.map +1 -1
- package/dist/queue/gcp-pubsub.js +53 -45
- package/dist/queue/index.d.ts +6 -15
- package/dist/queue/index.d.ts.map +1 -1
- package/dist/queue/index.js +476 -20
- package/dist/queue/memory-queue.d.ts +25 -29
- package/dist/queue/memory-queue.d.ts.map +1 -1
- package/dist/queue/memory-queue.js +159 -138
- package/dist/queue/register-defined-job.d.ts +3 -7
- package/dist/queue/register-defined-job.d.ts.map +1 -1
- package/dist/queue/register-defined-job.js +14 -14
- package/dist/queue/scaleway-sqs-queue.d.ts +31 -35
- package/dist/queue/scaleway-sqs-queue.d.ts.map +1 -1
- package/dist/queue/scaleway-sqs-queue.js +205 -173
- package/dist/queue/types.d.ts +2 -8
- package/dist/queue/types.d.ts.map +1 -1
- package/dist/queue/types.js +11 -12
- package/dist/scheduler/index.d.ts +68 -72
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/scheduler/index.js +113 -141
- package/package.json +176 -50
- package/dist/_virtual/_rolldown/runtime.js +0 -36
- package/dist/contracts/index.js.map +0 -1
- package/dist/entities/index.js.map +0 -1
- package/dist/events.js.map +0 -1
- package/dist/handlers/gmail-sync-handler.js.map +0 -1
- package/dist/handlers/index.js.map +0 -1
- package/dist/handlers/ping-job.js.map +0 -1
- package/dist/handlers/storage-document-handler.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jobs.capability.js.map +0 -1
- package/dist/jobs.feature.js.map +0 -1
- package/dist/queue/gcp-cloud-tasks.js.map +0 -1
- package/dist/queue/gcp-pubsub.js.map +0 -1
- package/dist/queue/index.js.map +0 -1
- package/dist/queue/memory-queue.js.map +0 -1
- package/dist/queue/register-defined-job.js.map +0 -1
- package/dist/queue/scaleway-sqs-queue.js.map +0 -1
- package/dist/queue/types.js.map +0 -1
- package/dist/scheduler/index.js.map +0 -1
package/dist/queue/index.js
CHANGED
|
@@ -1,23 +1,479 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/queue/register-defined-job.ts
|
|
3
|
+
function registerDefinedJob(queue, def) {
|
|
4
|
+
const wrapped = async (job) => {
|
|
5
|
+
const payload = def.schema.parse(job.payload);
|
|
6
|
+
const typedJob = {
|
|
7
|
+
...job,
|
|
8
|
+
payload
|
|
9
|
+
};
|
|
10
|
+
await def.handler(payload, typedJob);
|
|
11
|
+
};
|
|
12
|
+
queue.register(def.type, wrapped);
|
|
13
|
+
}
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
// src/queue/types.ts
|
|
16
|
+
export * from "@contractspec/lib.contracts/jobs/queue";
|
|
17
|
+
import {
|
|
18
|
+
calculateBackoff,
|
|
19
|
+
DEFAULT_RETRY_POLICY
|
|
20
|
+
} from "@contractspec/lib.contracts/jobs/queue";
|
|
10
21
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
// src/queue/memory-queue.ts
|
|
23
|
+
import { randomUUID } from "crypto";
|
|
24
|
+
class MemoryJobQueue {
|
|
25
|
+
jobs = new Map;
|
|
26
|
+
handlers = new Map;
|
|
27
|
+
timer;
|
|
28
|
+
activeCount = 0;
|
|
29
|
+
pollIntervalMs;
|
|
30
|
+
concurrency;
|
|
31
|
+
retryPolicy;
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
this.pollIntervalMs = options.pollIntervalMs ?? 200;
|
|
34
|
+
this.concurrency = options.concurrency ?? 5;
|
|
35
|
+
this.retryPolicy = options.retryPolicy ?? DEFAULT_RETRY_POLICY;
|
|
36
|
+
}
|
|
37
|
+
async enqueue(jobType, payload, options = {}) {
|
|
38
|
+
if (options.dedupeKey) {
|
|
39
|
+
const existing = Array.from(this.jobs.values()).find((j) => j.dedupeKey === options.dedupeKey && j.status === "pending");
|
|
40
|
+
if (existing) {
|
|
41
|
+
return existing;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const now = new Date;
|
|
45
|
+
const scheduledAt = options.delaySeconds ? new Date(now.getTime() + options.delaySeconds * 1000) : now;
|
|
46
|
+
const job = {
|
|
47
|
+
id: randomUUID(),
|
|
48
|
+
type: jobType,
|
|
49
|
+
version: "1.0.0",
|
|
50
|
+
payload,
|
|
51
|
+
status: "pending",
|
|
52
|
+
priority: options.priority ?? 0,
|
|
53
|
+
attempts: 0,
|
|
54
|
+
maxRetries: options.maxRetries ?? this.retryPolicy.maxRetries,
|
|
55
|
+
createdAt: now,
|
|
56
|
+
updatedAt: now,
|
|
57
|
+
scheduledAt,
|
|
58
|
+
dedupeKey: options.dedupeKey,
|
|
59
|
+
tenantId: options.tenantId,
|
|
60
|
+
userId: options.userId,
|
|
61
|
+
traceId: options.traceId,
|
|
62
|
+
metadata: options.metadata
|
|
63
|
+
};
|
|
64
|
+
if (options.timeoutMs) {
|
|
65
|
+
job.timeoutAt = new Date(now.getTime() + options.timeoutMs);
|
|
66
|
+
}
|
|
67
|
+
this.jobs.set(job.id, job);
|
|
68
|
+
return job;
|
|
69
|
+
}
|
|
70
|
+
register(jobType, handler) {
|
|
71
|
+
this.handlers.set(jobType, handler);
|
|
72
|
+
}
|
|
73
|
+
start() {
|
|
74
|
+
if (this.timer)
|
|
75
|
+
return;
|
|
76
|
+
this.timer = setInterval(() => {
|
|
77
|
+
this.processNext();
|
|
78
|
+
}, this.pollIntervalMs);
|
|
79
|
+
}
|
|
80
|
+
async stop() {
|
|
81
|
+
if (this.timer) {
|
|
82
|
+
clearInterval(this.timer);
|
|
83
|
+
this.timer = undefined;
|
|
84
|
+
}
|
|
85
|
+
while (this.activeCount > 0) {
|
|
86
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async getJob(jobId) {
|
|
90
|
+
return this.jobs.get(jobId) ?? null;
|
|
91
|
+
}
|
|
92
|
+
async cancelJob(jobId) {
|
|
93
|
+
const job = this.jobs.get(jobId);
|
|
94
|
+
if (!job || job.status !== "pending") {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
job.status = "cancelled";
|
|
98
|
+
job.updatedAt = new Date;
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
async getStats() {
|
|
102
|
+
const stats = {
|
|
103
|
+
pending: 0,
|
|
104
|
+
running: 0,
|
|
105
|
+
completed: 0,
|
|
106
|
+
failed: 0,
|
|
107
|
+
deadLetter: 0
|
|
108
|
+
};
|
|
109
|
+
for (const job of this.jobs.values()) {
|
|
110
|
+
switch (job.status) {
|
|
111
|
+
case "pending":
|
|
112
|
+
stats.pending++;
|
|
113
|
+
break;
|
|
114
|
+
case "running":
|
|
115
|
+
stats.running++;
|
|
116
|
+
break;
|
|
117
|
+
case "completed":
|
|
118
|
+
stats.completed++;
|
|
119
|
+
break;
|
|
120
|
+
case "failed":
|
|
121
|
+
stats.failed++;
|
|
122
|
+
break;
|
|
123
|
+
case "dead_letter":
|
|
124
|
+
stats.deadLetter++;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return stats;
|
|
129
|
+
}
|
|
130
|
+
async processNext() {
|
|
131
|
+
if (this.activeCount >= this.concurrency)
|
|
132
|
+
return;
|
|
133
|
+
const now = new Date;
|
|
134
|
+
const pendingJobs = Array.from(this.jobs.values()).filter((j) => j.status === "pending" && (!j.scheduledAt || j.scheduledAt <= now)).sort((a, b) => {
|
|
135
|
+
if (a.priority !== b.priority) {
|
|
136
|
+
return b.priority - a.priority;
|
|
137
|
+
}
|
|
138
|
+
return (a.scheduledAt?.getTime() ?? 0) - (b.scheduledAt?.getTime() ?? 0);
|
|
139
|
+
});
|
|
140
|
+
const job = pendingJobs[0];
|
|
141
|
+
if (!job)
|
|
142
|
+
return;
|
|
143
|
+
const handler = this.handlers.get(job.type);
|
|
144
|
+
if (!handler)
|
|
145
|
+
return;
|
|
146
|
+
this.activeCount++;
|
|
147
|
+
job.status = "running";
|
|
148
|
+
job.startedAt = new Date;
|
|
149
|
+
job.updatedAt = new Date;
|
|
150
|
+
job.attempts += 1;
|
|
151
|
+
try {
|
|
152
|
+
const result = await handler(job);
|
|
153
|
+
job.status = "completed";
|
|
154
|
+
job.completedAt = new Date;
|
|
155
|
+
job.result = result;
|
|
156
|
+
} catch (error) {
|
|
157
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
158
|
+
job.lastError = errorMessage;
|
|
159
|
+
if (job.attempts >= job.maxRetries) {
|
|
160
|
+
job.status = "dead_letter";
|
|
161
|
+
} else {
|
|
162
|
+
const backoff = calculateBackoff(job.attempts, this.retryPolicy);
|
|
163
|
+
job.status = "pending";
|
|
164
|
+
job.scheduledAt = new Date(Date.now() + backoff);
|
|
165
|
+
}
|
|
166
|
+
} finally {
|
|
167
|
+
job.updatedAt = new Date;
|
|
168
|
+
this.activeCount--;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
20
172
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
173
|
+
// src/queue/scaleway-sqs-queue.ts
|
|
174
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
175
|
+
import {
|
|
176
|
+
DeleteMessageCommand,
|
|
177
|
+
ReceiveMessageCommand,
|
|
178
|
+
SendMessageCommand,
|
|
179
|
+
SQSClient
|
|
180
|
+
} from "@aws-sdk/client-sqs";
|
|
181
|
+
class ScalewaySqsJobQueue {
|
|
182
|
+
sqs;
|
|
183
|
+
queueUrl;
|
|
184
|
+
waitTimeSeconds;
|
|
185
|
+
maxNumberOfMessages;
|
|
186
|
+
visibilityTimeoutSeconds;
|
|
187
|
+
handlers = new Map;
|
|
188
|
+
logger;
|
|
189
|
+
running = false;
|
|
190
|
+
constructor(config) {
|
|
191
|
+
this.logger = config.logger;
|
|
192
|
+
const accessKeyId = config.credentials?.accessKeyId ?? process.env.SCALEWAY_ACCESS_KEY_QUEUE;
|
|
193
|
+
const secretAccessKey = config.credentials?.secretAccessKey ?? process.env.SCALEWAY_SECRET_KEY_QUEUE;
|
|
194
|
+
if (!accessKeyId || !secretAccessKey) {
|
|
195
|
+
throw new Error("Missing SCALEWAY_ACCESS_KEY_QUEUE / SCALEWAY_SECRET_KEY_QUEUE in env");
|
|
196
|
+
}
|
|
197
|
+
const region = config.region ?? process.env.SCALEWAY_REGION ?? "par";
|
|
198
|
+
const endpoint = config.endpoint ?? "https://sqs.mnq.fr-par.scaleway.com";
|
|
199
|
+
this.sqs = new SQSClient({
|
|
200
|
+
region,
|
|
201
|
+
endpoint,
|
|
202
|
+
credentials: {
|
|
203
|
+
accessKeyId,
|
|
204
|
+
secretAccessKey
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
this.queueUrl = config.queueUrl;
|
|
208
|
+
this.waitTimeSeconds = config.waitTimeSeconds ?? 20;
|
|
209
|
+
this.maxNumberOfMessages = config.maxNumberOfMessages ?? 5;
|
|
210
|
+
this.visibilityTimeoutSeconds = config.visibilityTimeoutSeconds ?? 60;
|
|
211
|
+
}
|
|
212
|
+
async enqueue(jobType, payload, options = {}) {
|
|
213
|
+
const id = randomUUID2();
|
|
214
|
+
const now = new Date;
|
|
215
|
+
const scheduledAt = options.delaySeconds ? new Date(now.getTime() + options.delaySeconds * 1000) : now;
|
|
216
|
+
const envelope = {
|
|
217
|
+
id,
|
|
218
|
+
type: jobType,
|
|
219
|
+
payload
|
|
220
|
+
};
|
|
221
|
+
await this.sqs.send(new SendMessageCommand({
|
|
222
|
+
QueueUrl: this.queueUrl,
|
|
223
|
+
MessageBody: JSON.stringify(envelope),
|
|
224
|
+
DelaySeconds: options.delaySeconds ?? 0
|
|
225
|
+
}));
|
|
226
|
+
return {
|
|
227
|
+
id,
|
|
228
|
+
type: jobType,
|
|
229
|
+
version: "1.0.0",
|
|
230
|
+
payload,
|
|
231
|
+
status: "pending",
|
|
232
|
+
priority: options.priority ?? 0,
|
|
233
|
+
attempts: 0,
|
|
234
|
+
maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,
|
|
235
|
+
createdAt: now,
|
|
236
|
+
updatedAt: now,
|
|
237
|
+
scheduledAt,
|
|
238
|
+
dedupeKey: options.dedupeKey,
|
|
239
|
+
tenantId: options.tenantId,
|
|
240
|
+
userId: options.userId,
|
|
241
|
+
traceId: options.traceId,
|
|
242
|
+
metadata: options.metadata
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
register(jobType, handler) {
|
|
246
|
+
if (this.handlers.has(jobType)) {
|
|
247
|
+
throw new Error(`Handler already registered for job type "${jobType}"`);
|
|
248
|
+
}
|
|
249
|
+
this.handlers.set(jobType, handler);
|
|
250
|
+
}
|
|
251
|
+
start() {
|
|
252
|
+
if (this.running)
|
|
253
|
+
return;
|
|
254
|
+
this.running = true;
|
|
255
|
+
this.pollLoop().catch((error) => {
|
|
256
|
+
this.logger?.error?.("jobs.queue.scaleway_sqs.poll_loop_fatal", {
|
|
257
|
+
error: error instanceof Error ? error.message : String(error)
|
|
258
|
+
});
|
|
259
|
+
this.running = false;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
async stop() {
|
|
263
|
+
this.running = false;
|
|
264
|
+
}
|
|
265
|
+
async pollLoop() {
|
|
266
|
+
this.logger?.info?.("jobs.queue.scaleway_sqs.started", {
|
|
267
|
+
queueUrl: this.queueUrl
|
|
268
|
+
});
|
|
269
|
+
while (this.running) {
|
|
270
|
+
try {
|
|
271
|
+
const res = await this.sqs.send(new ReceiveMessageCommand({
|
|
272
|
+
QueueUrl: this.queueUrl,
|
|
273
|
+
MaxNumberOfMessages: this.maxNumberOfMessages,
|
|
274
|
+
WaitTimeSeconds: this.waitTimeSeconds,
|
|
275
|
+
VisibilityTimeout: this.visibilityTimeoutSeconds,
|
|
276
|
+
MessageSystemAttributeNames: ["ApproximateReceiveCount"]
|
|
277
|
+
}));
|
|
278
|
+
const messages = res.Messages ?? [];
|
|
279
|
+
if (messages.length === 0) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
for (const msg of messages) {
|
|
283
|
+
if (!msg.Body || !msg.ReceiptHandle) {
|
|
284
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.invalid_message", {
|
|
285
|
+
messageId: msg.MessageId,
|
|
286
|
+
reason: "missing_body_or_receipt"
|
|
287
|
+
});
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
let envelope;
|
|
291
|
+
try {
|
|
292
|
+
envelope = JSON.parse(msg.Body);
|
|
293
|
+
} catch (err) {
|
|
294
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.parse_failed", {
|
|
295
|
+
messageId: msg.MessageId,
|
|
296
|
+
error: err instanceof Error ? err.message : String(err)
|
|
297
|
+
});
|
|
298
|
+
await this.deleteMessage(msg.ReceiptHandle);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
const handler = this.handlers.get(envelope.type);
|
|
302
|
+
if (!handler) {
|
|
303
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.missing_handler", {
|
|
304
|
+
jobType: envelope.type,
|
|
305
|
+
messageId: msg.MessageId
|
|
306
|
+
});
|
|
307
|
+
await this.deleteMessage(msg.ReceiptHandle);
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
const now = new Date;
|
|
311
|
+
const attempts = parseInt(msg.Attributes?.ApproximateReceiveCount ?? "1", 10);
|
|
312
|
+
const job = {
|
|
313
|
+
id: envelope.id,
|
|
314
|
+
type: envelope.type,
|
|
315
|
+
version: "1.0.0",
|
|
316
|
+
payload: envelope.payload,
|
|
317
|
+
status: "pending",
|
|
318
|
+
priority: 0,
|
|
319
|
+
attempts,
|
|
320
|
+
maxRetries: DEFAULT_RETRY_POLICY.maxRetries,
|
|
321
|
+
createdAt: now,
|
|
322
|
+
updatedAt: now
|
|
323
|
+
};
|
|
324
|
+
job.status = "running";
|
|
325
|
+
job.updatedAt = new Date;
|
|
326
|
+
try {
|
|
327
|
+
await handler(job);
|
|
328
|
+
job.status = "completed";
|
|
329
|
+
job.updatedAt = new Date;
|
|
330
|
+
await this.deleteMessage(msg.ReceiptHandle);
|
|
331
|
+
} catch (err) {
|
|
332
|
+
job.status = "failed";
|
|
333
|
+
job.lastError = err instanceof Error ? err.message : "Unknown job error";
|
|
334
|
+
job.updatedAt = new Date;
|
|
335
|
+
this.logger?.error?.("jobs.queue.scaleway_sqs.job_failed", {
|
|
336
|
+
jobType: job.type,
|
|
337
|
+
jobId: job.id,
|
|
338
|
+
error: err instanceof Error ? err.message : String(err)
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} catch (err) {
|
|
343
|
+
this.logger?.error?.("jobs.queue.scaleway_sqs.poll_error", {
|
|
344
|
+
error: err instanceof Error ? err.message : String(err)
|
|
345
|
+
});
|
|
346
|
+
await this.sleep(5000);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
this.logger?.info?.("jobs.queue.scaleway_sqs.stopped", {
|
|
350
|
+
queueUrl: this.queueUrl
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
async deleteMessage(receiptHandle) {
|
|
354
|
+
try {
|
|
355
|
+
await this.sqs.send(new DeleteMessageCommand({
|
|
356
|
+
QueueUrl: this.queueUrl,
|
|
357
|
+
ReceiptHandle: receiptHandle
|
|
358
|
+
}));
|
|
359
|
+
} catch (err) {
|
|
360
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.delete_failed", {
|
|
361
|
+
error: err instanceof Error ? err.message : String(err)
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
async sleep(ms) {
|
|
366
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// src/queue/gcp-cloud-tasks.ts
|
|
371
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
372
|
+
class GcpCloudTasksQueue {
|
|
373
|
+
options;
|
|
374
|
+
handlers = new Map;
|
|
375
|
+
constructor(options) {
|
|
376
|
+
this.options = options;
|
|
377
|
+
}
|
|
378
|
+
async enqueue(jobType, payload, options = {}) {
|
|
379
|
+
const now = new Date;
|
|
380
|
+
const enqueueTime = options.delaySeconds != null ? { seconds: Math.floor(Date.now() / 1000) + options.delaySeconds } : undefined;
|
|
381
|
+
const body = Buffer.from(JSON.stringify({
|
|
382
|
+
id: randomUUID3(),
|
|
383
|
+
type: jobType,
|
|
384
|
+
payload
|
|
385
|
+
}), "utf-8");
|
|
386
|
+
await this.options.client.createTask({
|
|
387
|
+
parent: `projects/${this.options.projectId}/locations/${this.options.location}/queues/${this.options.queue}`,
|
|
388
|
+
task: {
|
|
389
|
+
httpRequest: {
|
|
390
|
+
httpMethod: "POST",
|
|
391
|
+
url: this.options.resolveUrl(jobType),
|
|
392
|
+
body,
|
|
393
|
+
headers: { "Content-Type": "application/json" },
|
|
394
|
+
oidcToken: this.options.serviceAccountEmail ? { serviceAccountEmail: this.options.serviceAccountEmail } : undefined
|
|
395
|
+
},
|
|
396
|
+
scheduleTime: enqueueTime
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
id: randomUUID3(),
|
|
401
|
+
type: jobType,
|
|
402
|
+
version: "1.0.0",
|
|
403
|
+
payload,
|
|
404
|
+
status: "pending",
|
|
405
|
+
priority: options.priority ?? 0,
|
|
406
|
+
attempts: 0,
|
|
407
|
+
maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,
|
|
408
|
+
createdAt: now,
|
|
409
|
+
updatedAt: now,
|
|
410
|
+
scheduledAt: options.delaySeconds ? new Date(now.getTime() + options.delaySeconds * 1000) : now,
|
|
411
|
+
dedupeKey: options.dedupeKey,
|
|
412
|
+
tenantId: options.tenantId,
|
|
413
|
+
userId: options.userId,
|
|
414
|
+
traceId: options.traceId,
|
|
415
|
+
metadata: options.metadata
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
register(jobType, handler) {
|
|
419
|
+
this.handlers.set(jobType, handler);
|
|
420
|
+
}
|
|
421
|
+
start() {}
|
|
422
|
+
async stop() {
|
|
423
|
+
this.handlers.clear();
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// src/queue/gcp-pubsub.ts
|
|
428
|
+
import { randomUUID as randomUUID4 } from "crypto";
|
|
429
|
+
class GcpPubSubQueue {
|
|
430
|
+
options;
|
|
431
|
+
handlers = new Map;
|
|
432
|
+
constructor(options) {
|
|
433
|
+
this.options = options;
|
|
434
|
+
}
|
|
435
|
+
async enqueue(jobType, payload, options = {}) {
|
|
436
|
+
const now = new Date;
|
|
437
|
+
await this.options.client.topic(this.options.topicName).publishMessage({
|
|
438
|
+
data: Buffer.from(JSON.stringify({
|
|
439
|
+
id: randomUUID4(),
|
|
440
|
+
type: jobType,
|
|
441
|
+
payload
|
|
442
|
+
}), "utf-8")
|
|
443
|
+
});
|
|
444
|
+
return {
|
|
445
|
+
id: randomUUID4(),
|
|
446
|
+
type: jobType,
|
|
447
|
+
version: "1.0.0",
|
|
448
|
+
payload,
|
|
449
|
+
status: "pending",
|
|
450
|
+
priority: options.priority ?? 0,
|
|
451
|
+
attempts: 0,
|
|
452
|
+
maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,
|
|
453
|
+
createdAt: now,
|
|
454
|
+
updatedAt: now,
|
|
455
|
+
scheduledAt: options.delaySeconds ? new Date(now.getTime() + options.delaySeconds * 1000) : now,
|
|
456
|
+
dedupeKey: options.dedupeKey,
|
|
457
|
+
tenantId: options.tenantId,
|
|
458
|
+
userId: options.userId,
|
|
459
|
+
traceId: options.traceId,
|
|
460
|
+
metadata: options.metadata
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
register(jobType, handler) {
|
|
464
|
+
this.handlers.set(jobType, handler);
|
|
465
|
+
}
|
|
466
|
+
start() {}
|
|
467
|
+
async stop() {
|
|
468
|
+
this.handlers.clear();
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
export {
|
|
472
|
+
registerDefinedJob,
|
|
473
|
+
calculateBackoff,
|
|
474
|
+
ScalewaySqsJobQueue,
|
|
475
|
+
MemoryJobQueue,
|
|
476
|
+
GcpPubSubQueue,
|
|
477
|
+
GcpCloudTasksQueue,
|
|
478
|
+
DEFAULT_RETRY_POLICY
|
|
479
|
+
};
|
|
@@ -1,35 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/** Default retry policy */
|
|
10
|
-
retryPolicy?: types_d_exports.RetryPolicy;
|
|
1
|
+
import type { Job, JobHandler, JobQueue, EnqueueOptions, QueueStats, RetryPolicy } from './types';
|
|
2
|
+
export interface MemoryQueueOptions {
|
|
3
|
+
/** Poll interval in milliseconds */
|
|
4
|
+
pollIntervalMs?: number;
|
|
5
|
+
/** Maximum concurrent jobs */
|
|
6
|
+
concurrency?: number;
|
|
7
|
+
/** Default retry policy */
|
|
8
|
+
retryPolicy?: RetryPolicy;
|
|
11
9
|
}
|
|
12
10
|
/**
|
|
13
11
|
* In-memory job queue for development and testing.
|
|
14
12
|
*/
|
|
15
|
-
declare class MemoryJobQueue implements
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
13
|
+
export declare class MemoryJobQueue implements JobQueue {
|
|
14
|
+
private readonly jobs;
|
|
15
|
+
private readonly handlers;
|
|
16
|
+
private timer?;
|
|
17
|
+
private activeCount;
|
|
18
|
+
private readonly pollIntervalMs;
|
|
19
|
+
private readonly concurrency;
|
|
20
|
+
private readonly retryPolicy;
|
|
21
|
+
constructor(options?: MemoryQueueOptions);
|
|
22
|
+
enqueue<TPayload>(jobType: string, payload: TPayload, options?: EnqueueOptions): Promise<Job<TPayload>>;
|
|
23
|
+
register<TPayload, TResult = void>(jobType: string, handler: JobHandler<TPayload, TResult>): void;
|
|
24
|
+
start(): void;
|
|
25
|
+
stop(): Promise<void>;
|
|
26
|
+
getJob(jobId: string): Promise<Job | null>;
|
|
27
|
+
cancelJob(jobId: string): Promise<boolean>;
|
|
28
|
+
getStats(): Promise<QueueStats>;
|
|
29
|
+
private processNext;
|
|
32
30
|
}
|
|
33
|
-
//#endregion
|
|
34
|
-
export { MemoryJobQueue, MemoryQueueOptions };
|
|
35
31
|
//# sourceMappingURL=memory-queue.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-queue.d.ts","
|
|
1
|
+
{"version":3,"file":"memory-queue.d.ts","sourceRoot":"","sources":["../../src/queue/memory-queue.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,GAAG,EACH,UAAU,EACV,QAAQ,EACR,cAAc,EACd,UAAU,EACV,WAAW,EACZ,MAAM,SAAS,CAAC;AAGjB,MAAM,WAAW,kBAAkB;IACjC,oCAAoC;IACpC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,8BAA8B;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,2BAA2B;IAC3B,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED;;GAEG;AACH,qBAAa,cAAe,YAAW,QAAQ;IAC7C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA0B;IAC/C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiC;IAC1D,OAAO,CAAC,KAAK,CAAC,CAAiC;IAC/C,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAS;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;gBAE9B,OAAO,GAAE,kBAAuB;IAMtC,OAAO,CAAC,QAAQ,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,QAAQ,EACjB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IA2CzB,QAAQ,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI,EAC/B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,GACrC,IAAI;IAIP,KAAK,IAAI,IAAI;IAOP,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAWrB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,CAAC;IAI1C,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAU1C,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC;YAgCvB,WAAW;CAuD1B"}
|