@prsm/queue 3.0.8 → 3.1.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/package.json +3 -2
- package/src/queue.js +28 -1
- package/types/queue.d.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prsm/queue",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Redis-backed distributed task queue with grouped concurrency, retries, and rate limiting",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -32,11 +32,12 @@
|
|
|
32
32
|
],
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@prsm/lock": "^1.0
|
|
35
|
+
"@prsm/lock": "^1.1.0",
|
|
36
36
|
"@prsm/ms": "^2.0.0",
|
|
37
37
|
"redis": "^5.1.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
+
"@prsm/trace": "^1.0.0",
|
|
40
41
|
"@types/node": "^22.15.29",
|
|
41
42
|
"typescript": "^5.9.3",
|
|
42
43
|
"vitest": "^3.2.4"
|
package/src/queue.js
CHANGED
|
@@ -86,6 +86,7 @@ export default class Queue extends EventEmitter {
|
|
|
86
86
|
cleanupInterval: options.cleanupInterval ?? 30000,
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
this._tracer = options.tracer ?? null
|
|
89
90
|
this._handler = null
|
|
90
91
|
this._workers = new Map()
|
|
91
92
|
this._groupWorkers = new Map()
|
|
@@ -137,15 +138,21 @@ export default class Queue extends EventEmitter {
|
|
|
137
138
|
*/
|
|
138
139
|
async push(payload, { group } = {}) {
|
|
139
140
|
if (this._closed) throw new Error("Queue is closed")
|
|
141
|
+
const traceparent = this._tracer ? this._tracer.toTraceparent() : null
|
|
140
142
|
const task = group
|
|
141
143
|
? { uuid: randomUUID(), payload, createdAt: Date.now(), group, attempts: 0 }
|
|
142
144
|
: { uuid: randomUUID(), payload, createdAt: Date.now(), attempts: 0 }
|
|
145
|
+
if (traceparent) task.traceparent = traceparent
|
|
143
146
|
this._pushed++
|
|
147
|
+
const span = this._tracer?.startSpan('queue.push', { 'queue.group': group ?? null, 'task.uuid': task.uuid }, { kind: 'producer' })
|
|
144
148
|
try {
|
|
145
149
|
await this._enqueue(task, group)
|
|
146
150
|
} catch (err) {
|
|
147
151
|
this._pushed--
|
|
152
|
+
span?.setError(err)
|
|
148
153
|
throw err
|
|
154
|
+
} finally {
|
|
155
|
+
span?.end()
|
|
149
156
|
}
|
|
150
157
|
this.emit("new", { task })
|
|
151
158
|
return task.uuid
|
|
@@ -158,9 +165,11 @@ export default class Queue extends EventEmitter {
|
|
|
158
165
|
*/
|
|
159
166
|
async pushAndWait(payload, { group, timeout = 0 } = {}) {
|
|
160
167
|
if (this._closed) throw new Error("Queue is closed")
|
|
168
|
+
const traceparent = this._tracer ? this._tracer.toTraceparent() : null
|
|
161
169
|
const task = group
|
|
162
170
|
? { uuid: randomUUID(), payload, createdAt: Date.now(), group, attempts: 0 }
|
|
163
171
|
: { uuid: randomUUID(), payload, createdAt: Date.now(), attempts: 0 }
|
|
172
|
+
if (traceparent) task.traceparent = traceparent
|
|
164
173
|
this._pushed++
|
|
165
174
|
const { promise, ready } = this._awaitTask(task.uuid, timeout)
|
|
166
175
|
promise.catch(() => {})
|
|
@@ -482,17 +491,35 @@ export default class Queue extends EventEmitter {
|
|
|
482
491
|
let succeeded = false
|
|
483
492
|
|
|
484
493
|
if (this._handler) {
|
|
485
|
-
|
|
494
|
+
const parent = this._tracer && task.traceparent ? this._tracer.fromTraceparent(task.traceparent) : null
|
|
495
|
+
const handle = this._tracer?.startSpan('queue.process', {
|
|
496
|
+
'queue.group': task.group ?? null,
|
|
497
|
+
'task.uuid': task.uuid,
|
|
498
|
+
'task.attempt': task.attempts,
|
|
499
|
+
}, {
|
|
500
|
+
kind: 'consumer',
|
|
501
|
+
parent: parent ? { traceId: parent.traceId, spanId: parent.parentSpanId, sampled: parent.sampled } : null,
|
|
502
|
+
})
|
|
503
|
+
const runHandler = async () => {
|
|
486
504
|
const timeoutPromise = opts.timeout > 0
|
|
487
505
|
? new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("Task timeout")), opts.timeout) })
|
|
488
506
|
: null
|
|
489
507
|
const workPromise = Promise.resolve(this._handler(task.payload, task))
|
|
490
508
|
result = timeoutPromise ? await Promise.race([workPromise, timeoutPromise]) : await workPromise
|
|
491
509
|
succeeded = true
|
|
510
|
+
}
|
|
511
|
+
try {
|
|
512
|
+
if (handle) {
|
|
513
|
+
await this._tracer.run(handle.context, runHandler)
|
|
514
|
+
} else {
|
|
515
|
+
await runHandler()
|
|
516
|
+
}
|
|
492
517
|
} catch (err) {
|
|
493
518
|
handlerError = err
|
|
519
|
+
handle?.setError(err)
|
|
494
520
|
} finally {
|
|
495
521
|
if (timer) clearTimeout(timer)
|
|
522
|
+
handle?.end()
|
|
496
523
|
}
|
|
497
524
|
} else {
|
|
498
525
|
succeeded = true
|