@prsm/queue 3.0.0 → 3.0.1
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/README.md +9 -9
- package/package.json +1 -1
- package/src/queue.js +11 -11
- package/types/queue.d.ts +1 -1
package/README.md
CHANGED
|
@@ -147,7 +147,7 @@ queue.on('drain', () => {})
|
|
|
147
147
|
uuid: string,
|
|
148
148
|
payload: any,
|
|
149
149
|
createdAt: number,
|
|
150
|
-
|
|
150
|
+
group?: string, // present when pushed with { group }
|
|
151
151
|
attempts: number
|
|
152
152
|
}
|
|
153
153
|
```
|
|
@@ -178,15 +178,15 @@ Each tenant gets up to 2 concurrent LLM calls with a 50ms pause between them. To
|
|
|
178
178
|
|
|
179
179
|
## WebSocket Integration with [mesh](https://github.com/nvms/mesh)
|
|
180
180
|
|
|
181
|
-
Queue events are local-only - only the server that processes a task emits `complete`/`failed`. Use [
|
|
181
|
+
Queue events are local-only - only the server that processes a task emits `complete`/`failed`. Use [@prsm/realtime](https://github.com/nvms/realtime) to push results to connected clients in real time.
|
|
182
182
|
|
|
183
183
|
Send results to a specific client:
|
|
184
184
|
|
|
185
185
|
```js
|
|
186
186
|
import Queue from '@prsm/queue'
|
|
187
|
-
import {
|
|
187
|
+
import { RealtimeServer } from '@prsm/realtime'
|
|
188
188
|
|
|
189
|
-
const
|
|
189
|
+
const realtime = new RealtimeServer({ redis: { host: 'localhost', port: 6379 } })
|
|
190
190
|
const queue = new Queue({ concurrency: 5, groups: { concurrency: 1 } })
|
|
191
191
|
|
|
192
192
|
queue.process(async (payload) => {
|
|
@@ -194,14 +194,14 @@ queue.process(async (payload) => {
|
|
|
194
194
|
})
|
|
195
195
|
|
|
196
196
|
queue.on('complete', ({ task, result }) => {
|
|
197
|
-
|
|
197
|
+
realtime.sendTo(task.payload.connectionId, 'job:complete', result)
|
|
198
198
|
})
|
|
199
199
|
|
|
200
200
|
queue.on('failed', ({ task, error }) => {
|
|
201
|
-
|
|
201
|
+
realtime.sendTo(task.payload.connectionId, 'job:failed', { error: error.message })
|
|
202
202
|
})
|
|
203
203
|
|
|
204
|
-
|
|
204
|
+
realtime.exposeCommand('generate-report', async (ctx) => {
|
|
205
205
|
const taskId = await queue.push({
|
|
206
206
|
connectionId: ctx.connection.id,
|
|
207
207
|
...ctx.payload,
|
|
@@ -210,10 +210,10 @@ mesh.exposeCommand('generate-report', async (ctx) => {
|
|
|
210
210
|
})
|
|
211
211
|
|
|
212
212
|
await queue.ready()
|
|
213
|
-
await
|
|
213
|
+
await realtime.listen(8080)
|
|
214
214
|
```
|
|
215
215
|
|
|
216
|
-
Both queue and
|
|
216
|
+
Both queue and realtime use the same Redis instance. No key conflicts (`queue:*` vs `mesh:*`).
|
|
217
217
|
|
|
218
218
|
## Horizontal Scaling
|
|
219
219
|
|
package/package.json
CHANGED
package/src/queue.js
CHANGED
|
@@ -20,7 +20,7 @@ import ms from "@prsm/ms"
|
|
|
20
20
|
* @property {string} uuid
|
|
21
21
|
* @property {any} payload
|
|
22
22
|
* @property {number} createdAt
|
|
23
|
-
* @property {string} [
|
|
23
|
+
* @property {string} [group]
|
|
24
24
|
* @property {number} attempts
|
|
25
25
|
*/
|
|
26
26
|
|
|
@@ -157,7 +157,7 @@ export default class Queue extends EventEmitter {
|
|
|
157
157
|
async push(payload, { group } = {}) {
|
|
158
158
|
if (this._closed) throw new Error("Queue is closed")
|
|
159
159
|
const task = group
|
|
160
|
-
? { uuid: randomUUID(), payload, createdAt: Date.now(),
|
|
160
|
+
? { uuid: randomUUID(), payload, createdAt: Date.now(), group, attempts: 0 }
|
|
161
161
|
: { uuid: randomUUID(), payload, createdAt: Date.now(), attempts: 0 }
|
|
162
162
|
this._pushed++
|
|
163
163
|
try {
|
|
@@ -178,7 +178,7 @@ export default class Queue extends EventEmitter {
|
|
|
178
178
|
pushAndWait(payload, { group, timeout = 0 } = {}) {
|
|
179
179
|
if (this._closed) return Promise.reject(new Error("Queue is closed"))
|
|
180
180
|
const task = group
|
|
181
|
-
? { uuid: randomUUID(), payload, createdAt: Date.now(),
|
|
181
|
+
? { uuid: randomUUID(), payload, createdAt: Date.now(), group, attempts: 0 }
|
|
182
182
|
: { uuid: randomUUID(), payload, createdAt: Date.now(), attempts: 0 }
|
|
183
183
|
this._pushed++
|
|
184
184
|
const result = this._awaitTask(task.uuid, timeout)
|
|
@@ -338,13 +338,13 @@ export default class Queue extends EventEmitter {
|
|
|
338
338
|
timeout: this._options.groups.timeout,
|
|
339
339
|
maxRetries: this._options.groups.maxRetries,
|
|
340
340
|
retryKey: `queue:groups:${groupKey}`,
|
|
341
|
-
groupKey,
|
|
341
|
+
group: groupKey,
|
|
342
342
|
}
|
|
343
343
|
this._runWorkerLoop(workerId, client, `queue:groups:${groupKey}`, groupWorkers, opts)
|
|
344
344
|
}
|
|
345
345
|
|
|
346
346
|
async _runWorkerLoop(workerId, client, key, activeMap, opts) {
|
|
347
|
-
const delay = opts.
|
|
347
|
+
const delay = opts.group ? this._options.groups.delay : this._options.delay
|
|
348
348
|
|
|
349
349
|
while (activeMap.get(workerId)) {
|
|
350
350
|
try {
|
|
@@ -371,17 +371,17 @@ export default class Queue extends EventEmitter {
|
|
|
371
371
|
}
|
|
372
372
|
|
|
373
373
|
this._inFlight++
|
|
374
|
-
if (opts.
|
|
375
|
-
this._groupInFlight.set(opts.
|
|
374
|
+
if (opts.group) {
|
|
375
|
+
this._groupInFlight.set(opts.group, (this._groupInFlight.get(opts.group) || 0) + 1)
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
try {
|
|
379
379
|
await this._processTask(task, opts)
|
|
380
380
|
} finally {
|
|
381
|
-
if (opts.
|
|
382
|
-
const count = (this._groupInFlight.get(opts.
|
|
383
|
-
if (count <= 0) this._groupInFlight.delete(opts.
|
|
384
|
-
else this._groupInFlight.set(opts.
|
|
381
|
+
if (opts.group) {
|
|
382
|
+
const count = (this._groupInFlight.get(opts.group) || 1) - 1
|
|
383
|
+
if (count <= 0) this._groupInFlight.delete(opts.group)
|
|
384
|
+
else this._groupInFlight.set(opts.group, count)
|
|
385
385
|
}
|
|
386
386
|
if (leaseId) await this._releaseGlobal(leaseId).catch(() => {})
|
|
387
387
|
}
|
package/types/queue.d.ts
CHANGED