@prsm/queue 1.0.2 → 2.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/README.md +109 -24
- package/package.json +11 -21
- package/src/index.js +1 -0
- package/src/queue.js +525 -0
- package/types/index.d.ts +1 -0
- package/types/queue.d.ts +4768 -0
- package/dist/index.cjs +0 -290
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -77
- package/dist/index.d.ts +0 -77
- package/dist/index.js +0 -257
- package/dist/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src=".github/logo.svg" width="80" height="80" alt="queue logo">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@prsm/queue</h1>
|
|
2
6
|
|
|
3
7
|
Redis-backed distributed task queue with grouped concurrency, retries, and rate limiting.
|
|
4
8
|
|
|
@@ -10,7 +14,7 @@ npm install @prsm/queue
|
|
|
10
14
|
|
|
11
15
|
## Quick Start
|
|
12
16
|
|
|
13
|
-
```
|
|
17
|
+
```js
|
|
14
18
|
import Queue from '@prsm/queue'
|
|
15
19
|
|
|
16
20
|
const queue = new Queue({
|
|
@@ -30,20 +34,22 @@ queue.on('failed', ({ task, error }) => {
|
|
|
30
34
|
console.log('Failed after retries:', task.uuid, error.message)
|
|
31
35
|
})
|
|
32
36
|
|
|
37
|
+
await queue.ready()
|
|
33
38
|
await queue.push({ userId: 123, action: 'sync' })
|
|
34
39
|
```
|
|
35
40
|
|
|
36
41
|
## Options
|
|
37
42
|
|
|
38
|
-
```
|
|
43
|
+
```js
|
|
39
44
|
const queue = new Queue({
|
|
40
|
-
concurrency: 2, //
|
|
45
|
+
concurrency: 2, // max concurrent tasks per instance
|
|
46
|
+
globalConcurrency: 10, // max concurrent tasks across all instances (Redis-backed)
|
|
41
47
|
delay: '100ms', // pause between tasks (string or ms)
|
|
42
48
|
timeout: '30s', // max task duration
|
|
43
49
|
maxRetries: 3, // attempts before failing
|
|
44
50
|
|
|
45
51
|
groups: {
|
|
46
|
-
concurrency: 1, //
|
|
52
|
+
concurrency: 1, // max concurrent tasks per group
|
|
47
53
|
delay: '50ms',
|
|
48
54
|
timeout: '10s',
|
|
49
55
|
maxRetries: 3
|
|
@@ -56,9 +62,44 @@ const queue = new Queue({
|
|
|
56
62
|
})
|
|
57
63
|
```
|
|
58
64
|
|
|
65
|
+
## Concurrency
|
|
66
|
+
|
|
67
|
+
Three independent limits compose together. A task must pass all applicable gates before processing.
|
|
68
|
+
|
|
69
|
+
**`concurrency`** - per-instance limit. Controls how many tasks this server can process simultaneously. This is the number of worker loops created for the main queue, and also caps total active tasks (including grouped) on this instance via an in-memory semaphore. Default: `1`.
|
|
70
|
+
|
|
71
|
+
**`globalConcurrency`** - cross-instance limit. Controls how many tasks can run across all servers sharing the same Redis. Uses a Redis-backed semaphore with automatic lease expiry for crash safety. If an instance crashes, its slots are reclaimed after 60 seconds. Default: `0` (disabled).
|
|
72
|
+
|
|
73
|
+
**`groups.concurrency`** - per-group limit. Controls how many tasks can run concurrently within a single group. Default: `1`.
|
|
74
|
+
|
|
75
|
+
### Examples
|
|
76
|
+
|
|
77
|
+
Protect local resources (CPU/memory bound):
|
|
78
|
+
|
|
79
|
+
```js
|
|
80
|
+
const queue = new Queue({
|
|
81
|
+
concurrency: 5,
|
|
82
|
+
groups: { concurrency: 1 }
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
100 groups each with 1 task - only 5 run at a time on this server.
|
|
87
|
+
|
|
88
|
+
Protect an external API (shared rate across servers):
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
const queue = new Queue({
|
|
92
|
+
concurrency: 10,
|
|
93
|
+
globalConcurrency: 20,
|
|
94
|
+
groups: { concurrency: 2 }
|
|
95
|
+
})
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
3 servers, each can handle 10 concurrent tasks, but only 20 total across all servers. Each group (tenant) gets up to 2 concurrent slots.
|
|
99
|
+
|
|
59
100
|
## Process Handler
|
|
60
101
|
|
|
61
|
-
```
|
|
102
|
+
```js
|
|
62
103
|
queue.process(async (payload, task) => {
|
|
63
104
|
console.log('Task:', task.uuid, 'Attempt:', task.attempts)
|
|
64
105
|
return await someWork(payload)
|
|
@@ -69,10 +110,11 @@ Throw an error to trigger retry. After `maxRetries`, the task fails permanently.
|
|
|
69
110
|
|
|
70
111
|
## Grouped Queues
|
|
71
112
|
|
|
72
|
-
Isolated concurrency per key - perfect for per-tenant
|
|
113
|
+
Isolated concurrency per key - perfect for per-tenant throttling.
|
|
73
114
|
|
|
74
|
-
```
|
|
115
|
+
```js
|
|
75
116
|
const queue = new Queue({
|
|
117
|
+
concurrency: 5,
|
|
76
118
|
groups: { concurrency: 1, delay: '50ms' }
|
|
77
119
|
})
|
|
78
120
|
|
|
@@ -80,40 +122,44 @@ queue.process(async (payload) => {
|
|
|
80
122
|
return await callExternalAPI(payload)
|
|
81
123
|
})
|
|
82
124
|
|
|
125
|
+
await queue.ready()
|
|
126
|
+
|
|
83
127
|
await queue.group('tenant-123').push({ action: 'sync' })
|
|
84
128
|
await queue.group('tenant-456').push({ action: 'sync' })
|
|
85
129
|
```
|
|
86
130
|
|
|
87
|
-
Each tenant processes independently. One slow tenant won't block others.
|
|
131
|
+
Each tenant processes independently. One slow tenant won't block others. Total concurrent tasks across all tenants is capped by `concurrency`.
|
|
88
132
|
|
|
89
133
|
## Events
|
|
90
134
|
|
|
91
|
-
```
|
|
135
|
+
```js
|
|
92
136
|
queue.on('new', ({ task }) => {})
|
|
93
137
|
queue.on('complete', ({ task, result }) => {})
|
|
94
138
|
queue.on('retry', ({ task, error, attempt }) => {})
|
|
95
139
|
queue.on('failed', ({ task, error }) => {})
|
|
140
|
+
queue.on('drain', () => {})
|
|
96
141
|
```
|
|
97
142
|
|
|
98
143
|
## Task Object
|
|
99
144
|
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
uuid: string
|
|
103
|
-
payload:
|
|
104
|
-
createdAt: number
|
|
105
|
-
groupKey?: string
|
|
145
|
+
```js
|
|
146
|
+
{
|
|
147
|
+
uuid: string,
|
|
148
|
+
payload: any,
|
|
149
|
+
createdAt: number,
|
|
150
|
+
groupKey?: string, // present when pushed via group()
|
|
106
151
|
attempts: number
|
|
107
152
|
}
|
|
108
153
|
```
|
|
109
154
|
|
|
110
|
-
##
|
|
155
|
+
## Throttling Example
|
|
111
156
|
|
|
112
|
-
|
|
157
|
+
Throttle LLM calls to external providers per tenant:
|
|
113
158
|
|
|
114
|
-
```
|
|
159
|
+
```js
|
|
115
160
|
const queue = new Queue({
|
|
116
|
-
|
|
161
|
+
concurrency: 20,
|
|
162
|
+
groups: { concurrency: 2, delay: '50ms' },
|
|
117
163
|
maxRetries: 3
|
|
118
164
|
})
|
|
119
165
|
|
|
@@ -128,15 +174,54 @@ app.post('/api/generate', async (req, res) => {
|
|
|
128
174
|
})
|
|
129
175
|
```
|
|
130
176
|
|
|
131
|
-
|
|
177
|
+
Each tenant gets up to 2 concurrent LLM calls with a 50ms pause between them. Total concurrent calls across all tenants is capped at 20, protecting your server and API budget from any single tenant overwhelming the system.
|
|
132
178
|
|
|
133
|
-
|
|
179
|
+
## WebSocket Integration with [mesh](https://github.com/nvms/mesh)
|
|
180
|
+
|
|
181
|
+
Queue events are local-only - only the server that processes a task emits `complete`/`failed`. Use [mesh](https://github.com/nvms/mesh) to push results to connected clients in real time.
|
|
182
|
+
|
|
183
|
+
Send results to a specific client:
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
import Queue from '@prsm/queue'
|
|
187
|
+
import { MeshServer } from '@mesh-kit/server'
|
|
188
|
+
|
|
189
|
+
const mesh = new MeshServer({ redis: { host: 'localhost', port: 6379 } })
|
|
190
|
+
const queue = new Queue({ concurrency: 5, groups: { concurrency: 1 } })
|
|
191
|
+
|
|
192
|
+
queue.process(async (payload) => {
|
|
193
|
+
return await generateReport(payload)
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
queue.on('complete', ({ task, result }) => {
|
|
197
|
+
mesh.sendTo(task.payload.connectionId, 'job:complete', result)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
queue.on('failed', ({ task, error }) => {
|
|
201
|
+
mesh.sendTo(task.payload.connectionId, 'job:failed', { error: error.message })
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
mesh.exposeCommand('generate-report', async (ctx) => {
|
|
205
|
+
const taskId = await queue.group(ctx.connection.id).push({
|
|
206
|
+
connectionId: ctx.connection.id,
|
|
207
|
+
...ctx.payload,
|
|
208
|
+
})
|
|
209
|
+
return { queued: true, taskId }
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
await queue.ready()
|
|
213
|
+
await mesh.listen(8080)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Both queue and mesh use the same Redis instance. No key conflicts (`queue:*` vs `mesh:*`).
|
|
217
|
+
|
|
218
|
+
## Horizontal Scaling
|
|
134
219
|
|
|
135
|
-
|
|
220
|
+
Multiple servers can push to the same queue. Redis coordinates via atomic operations - no duplicate processing. Use `globalConcurrency` to enforce a hard limit across all instances.
|
|
136
221
|
|
|
137
222
|
## Cleanup
|
|
138
223
|
|
|
139
|
-
```
|
|
224
|
+
```js
|
|
140
225
|
await queue.close()
|
|
141
226
|
```
|
|
142
227
|
|
package/package.json
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prsm/queue",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Redis-backed distributed task queue with grouped concurrency, retries, and rate limiting",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "./dist/index.cjs",
|
|
7
|
-
"module": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
6
|
"exports": {
|
|
10
7
|
".": {
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"default": "./dist/index.js"
|
|
14
|
-
},
|
|
15
|
-
"require": {
|
|
16
|
-
"types": "./dist/index.d.cts",
|
|
17
|
-
"default": "./dist/index.cjs"
|
|
18
|
-
}
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
19
10
|
}
|
|
20
11
|
},
|
|
12
|
+
"types": "./types/index.d.ts",
|
|
21
13
|
"files": [
|
|
22
|
-
"
|
|
14
|
+
"src",
|
|
15
|
+
"types"
|
|
23
16
|
],
|
|
24
17
|
"scripts": {
|
|
25
|
-
"
|
|
26
|
-
"test": "vitest",
|
|
27
|
-
"
|
|
28
|
-
"prepublishOnly": "npm run build"
|
|
18
|
+
"test": "vitest --reporter=verbose --run",
|
|
19
|
+
"test:watch": "vitest",
|
|
20
|
+
"prepublishOnly": "npx tsc --declaration --allowJs --emitDeclarationOnly --skipLibCheck --target es2020 --module nodenext --moduleResolution nodenext --strict false --esModuleInterop true --outDir ./types src/index.js"
|
|
29
21
|
},
|
|
30
22
|
"keywords": [
|
|
31
23
|
"queue",
|
|
@@ -38,7 +30,6 @@
|
|
|
38
30
|
"concurrency",
|
|
39
31
|
"retry"
|
|
40
32
|
],
|
|
41
|
-
"author": "",
|
|
42
33
|
"license": "MIT",
|
|
43
34
|
"dependencies": {
|
|
44
35
|
"@prsm/ms": "^1.0.1",
|
|
@@ -46,9 +37,8 @@
|
|
|
46
37
|
},
|
|
47
38
|
"devDependencies": {
|
|
48
39
|
"@types/node": "^22.15.29",
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"vitest": "^3.1.4"
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"vitest": "^3.2.4"
|
|
52
42
|
},
|
|
53
43
|
"engines": {
|
|
54
44
|
"node": ">=18"
|
package/src/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./queue.js"
|