@contractspec/lib.jobs 1.57.0 → 1.58.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
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// src/queue/types.ts
|
|
2
|
+
export * from "@contractspec/lib.contracts/jobs/queue";
|
|
3
|
+
import {
|
|
4
|
+
calculateBackoff,
|
|
5
|
+
DEFAULT_RETRY_POLICY
|
|
6
|
+
} from "@contractspec/lib.contracts/jobs/queue";
|
|
7
|
+
|
|
8
|
+
// src/queue/memory-queue.ts
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
class MemoryJobQueue {
|
|
11
|
+
jobs = new Map;
|
|
12
|
+
handlers = new Map;
|
|
13
|
+
timer;
|
|
14
|
+
activeCount = 0;
|
|
15
|
+
pollIntervalMs;
|
|
16
|
+
concurrency;
|
|
17
|
+
retryPolicy;
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.pollIntervalMs = options.pollIntervalMs ?? 200;
|
|
20
|
+
this.concurrency = options.concurrency ?? 5;
|
|
21
|
+
this.retryPolicy = options.retryPolicy ?? DEFAULT_RETRY_POLICY;
|
|
22
|
+
}
|
|
23
|
+
async enqueue(jobType, payload, options = {}) {
|
|
24
|
+
if (options.dedupeKey) {
|
|
25
|
+
const existing = Array.from(this.jobs.values()).find((j) => j.dedupeKey === options.dedupeKey && j.status === "pending");
|
|
26
|
+
if (existing) {
|
|
27
|
+
return existing;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const now = new Date;
|
|
31
|
+
const scheduledAt = options.delaySeconds ? new Date(now.getTime() + options.delaySeconds * 1000) : now;
|
|
32
|
+
const job = {
|
|
33
|
+
id: randomUUID(),
|
|
34
|
+
type: jobType,
|
|
35
|
+
version: "1.0.0",
|
|
36
|
+
payload,
|
|
37
|
+
status: "pending",
|
|
38
|
+
priority: options.priority ?? 0,
|
|
39
|
+
attempts: 0,
|
|
40
|
+
maxRetries: options.maxRetries ?? this.retryPolicy.maxRetries,
|
|
41
|
+
createdAt: now,
|
|
42
|
+
updatedAt: now,
|
|
43
|
+
scheduledAt,
|
|
44
|
+
dedupeKey: options.dedupeKey,
|
|
45
|
+
tenantId: options.tenantId,
|
|
46
|
+
userId: options.userId,
|
|
47
|
+
traceId: options.traceId,
|
|
48
|
+
metadata: options.metadata
|
|
49
|
+
};
|
|
50
|
+
if (options.timeoutMs) {
|
|
51
|
+
job.timeoutAt = new Date(now.getTime() + options.timeoutMs);
|
|
52
|
+
}
|
|
53
|
+
this.jobs.set(job.id, job);
|
|
54
|
+
return job;
|
|
55
|
+
}
|
|
56
|
+
register(jobType, handler) {
|
|
57
|
+
this.handlers.set(jobType, handler);
|
|
58
|
+
}
|
|
59
|
+
start() {
|
|
60
|
+
if (this.timer)
|
|
61
|
+
return;
|
|
62
|
+
this.timer = setInterval(() => {
|
|
63
|
+
this.processNext();
|
|
64
|
+
}, this.pollIntervalMs);
|
|
65
|
+
}
|
|
66
|
+
async stop() {
|
|
67
|
+
if (this.timer) {
|
|
68
|
+
clearInterval(this.timer);
|
|
69
|
+
this.timer = undefined;
|
|
70
|
+
}
|
|
71
|
+
while (this.activeCount > 0) {
|
|
72
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async getJob(jobId) {
|
|
76
|
+
return this.jobs.get(jobId) ?? null;
|
|
77
|
+
}
|
|
78
|
+
async cancelJob(jobId) {
|
|
79
|
+
const job = this.jobs.get(jobId);
|
|
80
|
+
if (!job || job.status !== "pending") {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
job.status = "cancelled";
|
|
84
|
+
job.updatedAt = new Date;
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
async getStats() {
|
|
88
|
+
const stats = {
|
|
89
|
+
pending: 0,
|
|
90
|
+
running: 0,
|
|
91
|
+
completed: 0,
|
|
92
|
+
failed: 0,
|
|
93
|
+
deadLetter: 0
|
|
94
|
+
};
|
|
95
|
+
for (const job of this.jobs.values()) {
|
|
96
|
+
switch (job.status) {
|
|
97
|
+
case "pending":
|
|
98
|
+
stats.pending++;
|
|
99
|
+
break;
|
|
100
|
+
case "running":
|
|
101
|
+
stats.running++;
|
|
102
|
+
break;
|
|
103
|
+
case "completed":
|
|
104
|
+
stats.completed++;
|
|
105
|
+
break;
|
|
106
|
+
case "failed":
|
|
107
|
+
stats.failed++;
|
|
108
|
+
break;
|
|
109
|
+
case "dead_letter":
|
|
110
|
+
stats.deadLetter++;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return stats;
|
|
115
|
+
}
|
|
116
|
+
async processNext() {
|
|
117
|
+
if (this.activeCount >= this.concurrency)
|
|
118
|
+
return;
|
|
119
|
+
const now = new Date;
|
|
120
|
+
const pendingJobs = Array.from(this.jobs.values()).filter((j) => j.status === "pending" && (!j.scheduledAt || j.scheduledAt <= now)).sort((a, b) => {
|
|
121
|
+
if (a.priority !== b.priority) {
|
|
122
|
+
return b.priority - a.priority;
|
|
123
|
+
}
|
|
124
|
+
return (a.scheduledAt?.getTime() ?? 0) - (b.scheduledAt?.getTime() ?? 0);
|
|
125
|
+
});
|
|
126
|
+
const job = pendingJobs[0];
|
|
127
|
+
if (!job)
|
|
128
|
+
return;
|
|
129
|
+
const handler = this.handlers.get(job.type);
|
|
130
|
+
if (!handler)
|
|
131
|
+
return;
|
|
132
|
+
this.activeCount++;
|
|
133
|
+
job.status = "running";
|
|
134
|
+
job.startedAt = new Date;
|
|
135
|
+
job.updatedAt = new Date;
|
|
136
|
+
job.attempts += 1;
|
|
137
|
+
try {
|
|
138
|
+
const result = await handler(job);
|
|
139
|
+
job.status = "completed";
|
|
140
|
+
job.completedAt = new Date;
|
|
141
|
+
job.result = result;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
144
|
+
job.lastError = errorMessage;
|
|
145
|
+
if (job.attempts >= job.maxRetries) {
|
|
146
|
+
job.status = "dead_letter";
|
|
147
|
+
} else {
|
|
148
|
+
const backoff = calculateBackoff(job.attempts, this.retryPolicy);
|
|
149
|
+
job.status = "pending";
|
|
150
|
+
job.scheduledAt = new Date(Date.now() + backoff);
|
|
151
|
+
}
|
|
152
|
+
} finally {
|
|
153
|
+
job.updatedAt = new Date;
|
|
154
|
+
this.activeCount--;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
MemoryJobQueue
|
|
160
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/queue/register-defined-job.ts
|
|
2
|
+
function registerDefinedJob(queue, def) {
|
|
3
|
+
const wrapped = async (job) => {
|
|
4
|
+
const payload = def.schema.parse(job.payload);
|
|
5
|
+
const typedJob = {
|
|
6
|
+
...job,
|
|
7
|
+
payload
|
|
8
|
+
};
|
|
9
|
+
await def.handler(payload, typedJob);
|
|
10
|
+
};
|
|
11
|
+
queue.register(def.type, wrapped);
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
registerDefinedJob
|
|
15
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// src/queue/types.ts
|
|
2
|
+
export * from "@contractspec/lib.contracts/jobs/queue";
|
|
3
|
+
import {
|
|
4
|
+
calculateBackoff,
|
|
5
|
+
DEFAULT_RETRY_POLICY
|
|
6
|
+
} from "@contractspec/lib.contracts/jobs/queue";
|
|
7
|
+
|
|
8
|
+
// src/queue/scaleway-sqs-queue.ts
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
import {
|
|
11
|
+
DeleteMessageCommand,
|
|
12
|
+
ReceiveMessageCommand,
|
|
13
|
+
SendMessageCommand,
|
|
14
|
+
SQSClient
|
|
15
|
+
} from "@aws-sdk/client-sqs";
|
|
16
|
+
class ScalewaySqsJobQueue {
|
|
17
|
+
sqs;
|
|
18
|
+
queueUrl;
|
|
19
|
+
waitTimeSeconds;
|
|
20
|
+
maxNumberOfMessages;
|
|
21
|
+
visibilityTimeoutSeconds;
|
|
22
|
+
handlers = new Map;
|
|
23
|
+
logger;
|
|
24
|
+
running = false;
|
|
25
|
+
constructor(config) {
|
|
26
|
+
this.logger = config.logger;
|
|
27
|
+
const accessKeyId = config.credentials?.accessKeyId ?? process.env.SCALEWAY_ACCESS_KEY_QUEUE;
|
|
28
|
+
const secretAccessKey = config.credentials?.secretAccessKey ?? process.env.SCALEWAY_SECRET_KEY_QUEUE;
|
|
29
|
+
if (!accessKeyId || !secretAccessKey) {
|
|
30
|
+
throw new Error("Missing SCALEWAY_ACCESS_KEY_QUEUE / SCALEWAY_SECRET_KEY_QUEUE in env");
|
|
31
|
+
}
|
|
32
|
+
const region = config.region ?? process.env.SCALEWAY_REGION ?? "par";
|
|
33
|
+
const endpoint = config.endpoint ?? "https://sqs.mnq.fr-par.scaleway.com";
|
|
34
|
+
this.sqs = new SQSClient({
|
|
35
|
+
region,
|
|
36
|
+
endpoint,
|
|
37
|
+
credentials: {
|
|
38
|
+
accessKeyId,
|
|
39
|
+
secretAccessKey
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
this.queueUrl = config.queueUrl;
|
|
43
|
+
this.waitTimeSeconds = config.waitTimeSeconds ?? 20;
|
|
44
|
+
this.maxNumberOfMessages = config.maxNumberOfMessages ?? 5;
|
|
45
|
+
this.visibilityTimeoutSeconds = config.visibilityTimeoutSeconds ?? 60;
|
|
46
|
+
}
|
|
47
|
+
async enqueue(jobType, payload, options = {}) {
|
|
48
|
+
const id = randomUUID();
|
|
49
|
+
const now = new Date;
|
|
50
|
+
const scheduledAt = options.delaySeconds ? new Date(now.getTime() + options.delaySeconds * 1000) : now;
|
|
51
|
+
const envelope = {
|
|
52
|
+
id,
|
|
53
|
+
type: jobType,
|
|
54
|
+
payload
|
|
55
|
+
};
|
|
56
|
+
await this.sqs.send(new SendMessageCommand({
|
|
57
|
+
QueueUrl: this.queueUrl,
|
|
58
|
+
MessageBody: JSON.stringify(envelope),
|
|
59
|
+
DelaySeconds: options.delaySeconds ?? 0
|
|
60
|
+
}));
|
|
61
|
+
return {
|
|
62
|
+
id,
|
|
63
|
+
type: jobType,
|
|
64
|
+
version: "1.0.0",
|
|
65
|
+
payload,
|
|
66
|
+
status: "pending",
|
|
67
|
+
priority: options.priority ?? 0,
|
|
68
|
+
attempts: 0,
|
|
69
|
+
maxRetries: options.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,
|
|
70
|
+
createdAt: now,
|
|
71
|
+
updatedAt: now,
|
|
72
|
+
scheduledAt,
|
|
73
|
+
dedupeKey: options.dedupeKey,
|
|
74
|
+
tenantId: options.tenantId,
|
|
75
|
+
userId: options.userId,
|
|
76
|
+
traceId: options.traceId,
|
|
77
|
+
metadata: options.metadata
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
register(jobType, handler) {
|
|
81
|
+
if (this.handlers.has(jobType)) {
|
|
82
|
+
throw new Error(`Handler already registered for job type "${jobType}"`);
|
|
83
|
+
}
|
|
84
|
+
this.handlers.set(jobType, handler);
|
|
85
|
+
}
|
|
86
|
+
start() {
|
|
87
|
+
if (this.running)
|
|
88
|
+
return;
|
|
89
|
+
this.running = true;
|
|
90
|
+
this.pollLoop().catch((error) => {
|
|
91
|
+
this.logger?.error?.("jobs.queue.scaleway_sqs.poll_loop_fatal", {
|
|
92
|
+
error: error instanceof Error ? error.message : String(error)
|
|
93
|
+
});
|
|
94
|
+
this.running = false;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async stop() {
|
|
98
|
+
this.running = false;
|
|
99
|
+
}
|
|
100
|
+
async pollLoop() {
|
|
101
|
+
this.logger?.info?.("jobs.queue.scaleway_sqs.started", {
|
|
102
|
+
queueUrl: this.queueUrl
|
|
103
|
+
});
|
|
104
|
+
while (this.running) {
|
|
105
|
+
try {
|
|
106
|
+
const res = await this.sqs.send(new ReceiveMessageCommand({
|
|
107
|
+
QueueUrl: this.queueUrl,
|
|
108
|
+
MaxNumberOfMessages: this.maxNumberOfMessages,
|
|
109
|
+
WaitTimeSeconds: this.waitTimeSeconds,
|
|
110
|
+
VisibilityTimeout: this.visibilityTimeoutSeconds,
|
|
111
|
+
MessageSystemAttributeNames: ["ApproximateReceiveCount"]
|
|
112
|
+
}));
|
|
113
|
+
const messages = res.Messages ?? [];
|
|
114
|
+
if (messages.length === 0) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
for (const msg of messages) {
|
|
118
|
+
if (!msg.Body || !msg.ReceiptHandle) {
|
|
119
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.invalid_message", {
|
|
120
|
+
messageId: msg.MessageId,
|
|
121
|
+
reason: "missing_body_or_receipt"
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
let envelope;
|
|
126
|
+
try {
|
|
127
|
+
envelope = JSON.parse(msg.Body);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.parse_failed", {
|
|
130
|
+
messageId: msg.MessageId,
|
|
131
|
+
error: err instanceof Error ? err.message : String(err)
|
|
132
|
+
});
|
|
133
|
+
await this.deleteMessage(msg.ReceiptHandle);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
const handler = this.handlers.get(envelope.type);
|
|
137
|
+
if (!handler) {
|
|
138
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.missing_handler", {
|
|
139
|
+
jobType: envelope.type,
|
|
140
|
+
messageId: msg.MessageId
|
|
141
|
+
});
|
|
142
|
+
await this.deleteMessage(msg.ReceiptHandle);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const now = new Date;
|
|
146
|
+
const attempts = parseInt(msg.Attributes?.ApproximateReceiveCount ?? "1", 10);
|
|
147
|
+
const job = {
|
|
148
|
+
id: envelope.id,
|
|
149
|
+
type: envelope.type,
|
|
150
|
+
version: "1.0.0",
|
|
151
|
+
payload: envelope.payload,
|
|
152
|
+
status: "pending",
|
|
153
|
+
priority: 0,
|
|
154
|
+
attempts,
|
|
155
|
+
maxRetries: DEFAULT_RETRY_POLICY.maxRetries,
|
|
156
|
+
createdAt: now,
|
|
157
|
+
updatedAt: now
|
|
158
|
+
};
|
|
159
|
+
job.status = "running";
|
|
160
|
+
job.updatedAt = new Date;
|
|
161
|
+
try {
|
|
162
|
+
await handler(job);
|
|
163
|
+
job.status = "completed";
|
|
164
|
+
job.updatedAt = new Date;
|
|
165
|
+
await this.deleteMessage(msg.ReceiptHandle);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
job.status = "failed";
|
|
168
|
+
job.lastError = err instanceof Error ? err.message : "Unknown job error";
|
|
169
|
+
job.updatedAt = new Date;
|
|
170
|
+
this.logger?.error?.("jobs.queue.scaleway_sqs.job_failed", {
|
|
171
|
+
jobType: job.type,
|
|
172
|
+
jobId: job.id,
|
|
173
|
+
error: err instanceof Error ? err.message : String(err)
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} catch (err) {
|
|
178
|
+
this.logger?.error?.("jobs.queue.scaleway_sqs.poll_error", {
|
|
179
|
+
error: err instanceof Error ? err.message : String(err)
|
|
180
|
+
});
|
|
181
|
+
await this.sleep(5000);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
this.logger?.info?.("jobs.queue.scaleway_sqs.stopped", {
|
|
185
|
+
queueUrl: this.queueUrl
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
async deleteMessage(receiptHandle) {
|
|
189
|
+
try {
|
|
190
|
+
await this.sqs.send(new DeleteMessageCommand({
|
|
191
|
+
QueueUrl: this.queueUrl,
|
|
192
|
+
ReceiptHandle: receiptHandle
|
|
193
|
+
}));
|
|
194
|
+
} catch (err) {
|
|
195
|
+
this.logger?.warn?.("jobs.queue.scaleway_sqs.delete_failed", {
|
|
196
|
+
error: err instanceof Error ? err.message : String(err)
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
async sleep(ms) {
|
|
201
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export {
|
|
205
|
+
ScalewaySqsJobQueue
|
|
206
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/scheduler/index.ts
|
|
2
|
+
function getNextCronRun(cronExpression, after = new Date) {
|
|
3
|
+
try {
|
|
4
|
+
const parts = cronExpression.trim().split(/\s+/);
|
|
5
|
+
if (parts.length !== 5) {
|
|
6
|
+
console.warn(`Invalid cron expression: ${cronExpression}`);
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
const minute = parts[0];
|
|
10
|
+
const hour = parts[1];
|
|
11
|
+
const dayOfMonth = parts[2];
|
|
12
|
+
const month = parts[3];
|
|
13
|
+
const next = new Date(after);
|
|
14
|
+
next.setSeconds(0);
|
|
15
|
+
next.setMilliseconds(0);
|
|
16
|
+
if (minute && hour && minute !== "*" && hour !== "*" && dayOfMonth === "*" && month === "*") {
|
|
17
|
+
const targetMinute = Number.parseInt(minute, 10);
|
|
18
|
+
const targetHour = Number.parseInt(hour, 10);
|
|
19
|
+
next.setHours(targetHour, targetMinute, 0, 0);
|
|
20
|
+
if (next <= after) {
|
|
21
|
+
next.setDate(next.getDate() + 1);
|
|
22
|
+
}
|
|
23
|
+
return next;
|
|
24
|
+
}
|
|
25
|
+
next.setMinutes(next.getMinutes() + 1);
|
|
26
|
+
return next;
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class JobScheduler {
|
|
33
|
+
queue;
|
|
34
|
+
schedules = new Map;
|
|
35
|
+
timer;
|
|
36
|
+
checkIntervalMs;
|
|
37
|
+
constructor(queue, options = {}) {
|
|
38
|
+
this.queue = queue;
|
|
39
|
+
this.checkIntervalMs = options.checkIntervalMs ?? 60000;
|
|
40
|
+
}
|
|
41
|
+
schedule(config) {
|
|
42
|
+
const nextRun = config.enabled !== false ? getNextCronRun(config.cronExpression) : null;
|
|
43
|
+
this.schedules.set(config.name, {
|
|
44
|
+
...config,
|
|
45
|
+
enabled: config.enabled ?? true,
|
|
46
|
+
nextRun,
|
|
47
|
+
lastRun: null
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
unschedule(name) {
|
|
51
|
+
return this.schedules.delete(name);
|
|
52
|
+
}
|
|
53
|
+
enable(name) {
|
|
54
|
+
const schedule = this.schedules.get(name);
|
|
55
|
+
if (!schedule)
|
|
56
|
+
return false;
|
|
57
|
+
schedule.enabled = true;
|
|
58
|
+
schedule.nextRun = getNextCronRun(schedule.cronExpression);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
disable(name) {
|
|
62
|
+
const schedule = this.schedules.get(name);
|
|
63
|
+
if (!schedule)
|
|
64
|
+
return false;
|
|
65
|
+
schedule.enabled = false;
|
|
66
|
+
schedule.nextRun = null;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
getSchedules() {
|
|
70
|
+
return Array.from(this.schedules.values());
|
|
71
|
+
}
|
|
72
|
+
getSchedule(name) {
|
|
73
|
+
return this.schedules.get(name);
|
|
74
|
+
}
|
|
75
|
+
start() {
|
|
76
|
+
if (this.timer)
|
|
77
|
+
return;
|
|
78
|
+
this.checkSchedules();
|
|
79
|
+
this.timer = setInterval(() => {
|
|
80
|
+
this.checkSchedules();
|
|
81
|
+
}, this.checkIntervalMs);
|
|
82
|
+
}
|
|
83
|
+
stop() {
|
|
84
|
+
if (this.timer) {
|
|
85
|
+
clearInterval(this.timer);
|
|
86
|
+
this.timer = undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async checkSchedules() {
|
|
90
|
+
const now = new Date;
|
|
91
|
+
for (const schedule of this.schedules.values()) {
|
|
92
|
+
if (!schedule.enabled || !schedule.nextRun)
|
|
93
|
+
continue;
|
|
94
|
+
if (schedule.nextRun <= now) {
|
|
95
|
+
try {
|
|
96
|
+
const payload = typeof schedule.payload === "function" ? await schedule.payload() : schedule.payload;
|
|
97
|
+
await this.queue.enqueue(schedule.jobType, payload, schedule.options);
|
|
98
|
+
schedule.lastRun = now;
|
|
99
|
+
schedule.nextRun = getNextCronRun(schedule.cronExpression, now);
|
|
100
|
+
} catch (error) {
|
|
101
|
+
console.error(`Failed to enqueue scheduled job ${schedule.name}:`, error);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function createScheduler(queue, options) {
|
|
108
|
+
return new JobScheduler(queue, options);
|
|
109
|
+
}
|
|
110
|
+
function defineSchedule(config) {
|
|
111
|
+
return config;
|
|
112
|
+
}
|
|
113
|
+
export {
|
|
114
|
+
defineSchedule,
|
|
115
|
+
createScheduler,
|
|
116
|
+
JobScheduler
|
|
117
|
+
};
|
|
@@ -1,42 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/queue/gcp-cloud-tasks.d.ts
|
|
1
|
+
import { type EnqueueOptions, type Job, type JobHandler, type JobQueue } from './types';
|
|
4
2
|
interface CloudTasksClientLike {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
createTask(request: {
|
|
4
|
+
parent: string;
|
|
5
|
+
task: {
|
|
6
|
+
httpRequest: {
|
|
7
|
+
httpMethod: number | string;
|
|
8
|
+
url: string;
|
|
9
|
+
body: Buffer;
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
oidcToken?: {
|
|
12
|
+
serviceAccountEmail: string;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
scheduleTime?: {
|
|
16
|
+
seconds: number;
|
|
17
|
+
};
|
|
15
18
|
};
|
|
16
|
-
|
|
17
|
-
scheduleTime?: {
|
|
18
|
-
seconds: number;
|
|
19
|
-
};
|
|
20
|
-
};
|
|
21
|
-
}): Promise<unknown>;
|
|
19
|
+
}): Promise<unknown>;
|
|
22
20
|
}
|
|
23
|
-
interface GcpCloudTasksQueueOptions {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
export interface GcpCloudTasksQueueOptions {
|
|
22
|
+
client: CloudTasksClientLike;
|
|
23
|
+
projectId: string;
|
|
24
|
+
location: string;
|
|
25
|
+
queue: string;
|
|
26
|
+
resolveUrl(jobType: string): string;
|
|
27
|
+
serviceAccountEmail?: string;
|
|
30
28
|
}
|
|
31
|
-
declare class GcpCloudTasksQueue implements
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
29
|
+
export declare class GcpCloudTasksQueue implements JobQueue {
|
|
30
|
+
private readonly options;
|
|
31
|
+
private readonly handlers;
|
|
32
|
+
constructor(options: GcpCloudTasksQueueOptions);
|
|
33
|
+
enqueue<TPayload>(jobType: string, payload: TPayload, options?: EnqueueOptions): Promise<Job<TPayload>>;
|
|
34
|
+
register<TPayload, TResult = void>(jobType: string, handler: JobHandler<TPayload, TResult>): void;
|
|
35
|
+
start(): void;
|
|
36
|
+
stop(): Promise<void>;
|
|
39
37
|
}
|
|
40
|
-
|
|
41
|
-
export { GcpCloudTasksQueue, GcpCloudTasksQueueOptions };
|
|
38
|
+
export {};
|
|
42
39
|
//# sourceMappingURL=gcp-cloud-tasks.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gcp-cloud-tasks.d.ts","
|
|
1
|
+
{"version":3,"file":"gcp-cloud-tasks.d.ts","sourceRoot":"","sources":["../../src/queue/gcp-cloud-tasks.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,GAAG,EACR,KAAK,UAAU,EACf,KAAK,QAAQ,EACd,MAAM,SAAS,CAAC;AAEjB,UAAU,oBAAoB;IAC5B,UAAU,CAAC,OAAO,EAAE;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE;YACJ,WAAW,EAAE;gBACX,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;gBAC5B,GAAG,EAAE,MAAM,CAAC;gBACZ,IAAI,EAAE,MAAM,CAAC;gBACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBACjC,SAAS,CAAC,EAAE;oBAAE,mBAAmB,EAAE,MAAM,CAAA;iBAAE,CAAC;aAC7C,CAAC;YACF,YAAY,CAAC,EAAE;gBAAE,OAAO,EAAE,MAAM,CAAA;aAAE,CAAC;SACpC,CAAC;KACH,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACtB;AAED,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,oBAAoB,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACpC,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAED,qBAAa,kBAAmB,YAAW,QAAQ;IAGrC,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiC;gBAE7B,OAAO,EAAE,yBAAyB;IAEzD,OAAO,CAAC,QAAQ,EACpB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,QAAQ,EACjB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAoDzB,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;IAIP,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAG5B"}
|