@chainlink/external-adapter-framework 2.10.0 → 2.11.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 +1 -1
- package/adapter/market-status.d.ts +6 -0
- package/adapter/market-status.js +22 -0
- package/adapter/market-status.js.map +1 -1
- package/config/index.js +1 -1
- package/generator-adapter/node_modules/.yarn-integrity +8 -8
- package/generator-adapter/node_modules/@types/node/README.md +1 -1
- package/generator-adapter/node_modules/@types/node/package.json +2 -2
- package/generator-adapter/node_modules/@types/node/process.d.ts +7 -0
- package/generator-adapter/node_modules/@yeoman/adapter/dist/queued-adapter.js +1 -3
- package/generator-adapter/node_modules/@yeoman/adapter/dist/queued-adapter.js.map +1 -1
- package/generator-adapter/node_modules/@yeoman/adapter/package.json +3 -3
- package/generator-adapter/node_modules/@yeoman/types/package.json +3 -3
- package/generator-adapter/node_modules/p-queue/dist/index.d.ts +181 -11
- package/generator-adapter/node_modules/p-queue/dist/index.js +366 -45
- package/generator-adapter/node_modules/p-queue/dist/options copy.d.ts +121 -0
- package/generator-adapter/node_modules/p-queue/dist/options copy.js +1 -0
- package/generator-adapter/node_modules/p-queue/dist/options.d.ts +26 -7
- package/generator-adapter/node_modules/p-queue/dist/priority-queue.d.ts +1 -0
- package/generator-adapter/node_modules/p-queue/dist/priority-queue.js +12 -6
- package/generator-adapter/node_modules/p-queue/dist/queue.d.ts +1 -0
- package/generator-adapter/node_modules/p-queue/package.json +19 -27
- package/generator-adapter/node_modules/p-queue/readme.md +541 -19
- package/generator-adapter/node_modules/p-timeout/index.d.ts +2 -4
- package/generator-adapter/node_modules/p-timeout/index.js +23 -50
- package/generator-adapter/node_modules/p-timeout/package.json +11 -9
- package/generator-adapter/node_modules/p-timeout/readme.md +11 -9
- package/generator-adapter/package.json +3 -3
- package/package.json +8 -7
- package/validation/market-status.d.ts +6 -0
- package/validation/market-status.js +69 -0
- package/validation/market-status.js.map +1 -0
|
@@ -8,6 +8,34 @@ For servers, you probably want a Redis-backed [job queue](https://github.com/sin
|
|
|
8
8
|
|
|
9
9
|
Note that the project is feature complete. We are happy to review pull requests, but we don't plan any further development. We are also not answering email support questions.
|
|
10
10
|
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
<br>
|
|
14
|
+
<div align="center">
|
|
15
|
+
<p>
|
|
16
|
+
<p>
|
|
17
|
+
<sup>
|
|
18
|
+
<a href="https://github.com/sponsors/sindresorhus">Sindre's open source work is supported by the community</a><br>Special thanks to:
|
|
19
|
+
</sup>
|
|
20
|
+
</p>
|
|
21
|
+
<br>
|
|
22
|
+
<br>
|
|
23
|
+
<a href="https://fetchfox.ai?ref=sindre">
|
|
24
|
+
<div>
|
|
25
|
+
<img src="https://sindresorhus.com/assets/thanks/fetchfox-logo.svg" height="200"/>
|
|
26
|
+
</div>
|
|
27
|
+
<b>Scrape anything with FetchFox</b>
|
|
28
|
+
<div>
|
|
29
|
+
<sup>FetchFox is an AI powered scraping tool that lets you scrape data from any website</sup>
|
|
30
|
+
</div>
|
|
31
|
+
</a>
|
|
32
|
+
</p>
|
|
33
|
+
<br>
|
|
34
|
+
<br>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
11
39
|
## Install
|
|
12
40
|
|
|
13
41
|
```sh
|
|
@@ -18,7 +46,7 @@ npm install p-queue
|
|
|
18
46
|
|
|
19
47
|
## Usage
|
|
20
48
|
|
|
21
|
-
Here we run only one promise at
|
|
49
|
+
Here we run only one promise at a time. For example, set `concurrency` to 4 to run four promises at the same time.
|
|
22
50
|
|
|
23
51
|
```js
|
|
24
52
|
import PQueue from 'p-queue';
|
|
@@ -35,12 +63,6 @@ const queue = new PQueue({concurrency: 1});
|
|
|
35
63
|
await queue.add(() => got('https://avajs.dev'));
|
|
36
64
|
console.log('Done: avajs.dev');
|
|
37
65
|
})();
|
|
38
|
-
|
|
39
|
-
(async () => {
|
|
40
|
-
const task = await getUnicornTask();
|
|
41
|
-
await queue.add(task);
|
|
42
|
-
console.log('Done: Unicorn task');
|
|
43
|
-
})();
|
|
44
66
|
```
|
|
45
67
|
|
|
46
68
|
## API
|
|
@@ -63,16 +85,24 @@ Concurrency limit.
|
|
|
63
85
|
|
|
64
86
|
##### timeout
|
|
65
87
|
|
|
66
|
-
Type: `number
|
|
88
|
+
Type: `number`\
|
|
89
|
+
Default: `undefined`
|
|
67
90
|
|
|
68
|
-
Per-operation timeout in milliseconds. Operations
|
|
91
|
+
Per-operation timeout in milliseconds. Operations will throw a `TimeoutError` if they don't complete within the specified time.
|
|
69
92
|
|
|
70
|
-
|
|
93
|
+
The timeout begins when the operation is dequeued and starts execution, not while it's waiting in the queue.
|
|
71
94
|
|
|
72
|
-
|
|
73
|
-
|
|
95
|
+
Can be overridden per task using the `timeout` option in `.add()`:
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const queue = new PQueue({timeout: 5000});
|
|
99
|
+
|
|
100
|
+
// This task uses the global 5s timeout
|
|
101
|
+
await queue.add(() => fetchData());
|
|
74
102
|
|
|
75
|
-
|
|
103
|
+
// This task has a 10s timeout
|
|
104
|
+
await queue.add(() => slowTask(), {timeout: 10000});
|
|
105
|
+
```
|
|
76
106
|
|
|
77
107
|
##### autoStart
|
|
78
108
|
|
|
@@ -103,7 +133,7 @@ Minimum: `0`
|
|
|
103
133
|
|
|
104
134
|
The length of time in milliseconds before the interval count resets. Must be finite.
|
|
105
135
|
|
|
106
|
-
#####
|
|
136
|
+
##### carryoverIntervalCount
|
|
107
137
|
|
|
108
138
|
Type: `boolean`\
|
|
109
139
|
Default: `false`
|
|
@@ -116,9 +146,15 @@ If `true`, specifies that any [pending](https://developer.mozilla.org/en-US/docs
|
|
|
116
146
|
|
|
117
147
|
#### .add(fn, options?)
|
|
118
148
|
|
|
119
|
-
Adds a sync or async task to the queue.
|
|
149
|
+
Adds a sync or async task to the queue.
|
|
150
|
+
|
|
151
|
+
Returns a promise that settles when the task completes, not when it's added to the queue. The promise resolves with the return value of `fn`.
|
|
120
152
|
|
|
121
|
-
|
|
153
|
+
> [!IMPORTANT]
|
|
154
|
+
> If you `await` this promise, you will wait for the task to finish running, which may defeat the purpose of using a queue for concurrency. See the [Usage](#usage) section for examples.
|
|
155
|
+
|
|
156
|
+
> [!NOTE]
|
|
157
|
+
> If your items can potentially throw an exception, you must handle those errors from the returned Promise or they may be reported as an unhandled Promise rejection and potentially cause your process to exit immediately.
|
|
122
158
|
|
|
123
159
|
##### fn
|
|
124
160
|
|
|
@@ -137,6 +173,12 @@ Default: `0`
|
|
|
137
173
|
|
|
138
174
|
Priority of operation. Operations with greater priority will be scheduled first.
|
|
139
175
|
|
|
176
|
+
##### id
|
|
177
|
+
|
|
178
|
+
Type `string`
|
|
179
|
+
|
|
180
|
+
Unique identifier for the promise function, used to update its priority before execution. If not specified, it is auto-assigned an incrementing BigInt starting from `1n`.
|
|
181
|
+
|
|
140
182
|
##### signal
|
|
141
183
|
|
|
142
184
|
[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) for cancellation of the operation. When aborted, it will be removed from the queue and the `queue.add()` call will reject with an [error](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/reason). If the operation is already running, the signal will need to be handled by the operation itself.
|
|
@@ -192,12 +234,90 @@ Returns a promise that settles when the queue becomes empty.
|
|
|
192
234
|
|
|
193
235
|
Can be called multiple times. Useful if you for example add additional items at a later time.
|
|
194
236
|
|
|
237
|
+
> [!NOTE]
|
|
238
|
+
> The promise returned by `.onEmpty()` resolves **once** when the queue becomes empty. If you want to be notified every time the queue becomes empty, use the `empty` event instead: `queue.on('empty', () => {})`.
|
|
239
|
+
|
|
195
240
|
#### .onIdle()
|
|
196
241
|
|
|
197
242
|
Returns a promise that settles when the queue becomes empty, and all promises have completed; `queue.size === 0 && queue.pending === 0`.
|
|
198
243
|
|
|
199
244
|
The difference with `.onEmpty` is that `.onIdle` guarantees that all work from the queue has finished. `.onEmpty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
|
|
200
245
|
|
|
246
|
+
> [!NOTE]
|
|
247
|
+
> The promise returned by `.onIdle()` resolves **once** when the queue becomes idle. If you want to be notified every time the queue becomes idle, use the `idle` event instead: `queue.on('idle', () => {})`.
|
|
248
|
+
|
|
249
|
+
#### .onPendingZero()
|
|
250
|
+
|
|
251
|
+
Returns a promise that settles when all currently running tasks have completed; `queue.pending === 0`.
|
|
252
|
+
|
|
253
|
+
The difference with `.onIdle` is that `.onPendingZero` only waits for currently running tasks to finish, ignoring queued tasks. This is useful when you want to drain in-flight tasks before mutating shared state.
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
queue.pause();
|
|
257
|
+
await queue.onPendingZero();
|
|
258
|
+
// All running tasks have finished, though the queue may still have items
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
#### .onRateLimit()
|
|
262
|
+
|
|
263
|
+
Returns a promise that settles when the queue becomes rate-limited due to `intervalCap`. If the queue is already rate-limited, the promise resolves immediately.
|
|
264
|
+
|
|
265
|
+
Useful for implementing backpressure to prevent memory issues when producers are faster than consumers.
|
|
266
|
+
|
|
267
|
+
```js
|
|
268
|
+
const queue = new PQueue({intervalCap: 5, interval: 1000});
|
|
269
|
+
|
|
270
|
+
// Add many tasks
|
|
271
|
+
for (let index = 0; index < 10; index++) {
|
|
272
|
+
queue.add(() => someTask());
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
await queue.onRateLimit();
|
|
276
|
+
console.log('Queue is now rate-limited - time for maintenance tasks');
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### .onRateLimitCleared()
|
|
280
|
+
|
|
281
|
+
Returns a promise that settles when the queue is no longer rate-limited. If the queue is not currently rate-limited, the promise resolves immediately.
|
|
282
|
+
|
|
283
|
+
```js
|
|
284
|
+
const queue = new PQueue({intervalCap: 5, interval: 1000});
|
|
285
|
+
|
|
286
|
+
// Wait for rate limiting to be cleared
|
|
287
|
+
await queue.onRateLimitCleared();
|
|
288
|
+
console.log('Rate limit cleared - can add more tasks');
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
#### .onError()
|
|
292
|
+
|
|
293
|
+
Returns a promise that rejects when any task in the queue errors.
|
|
294
|
+
|
|
295
|
+
Use with `Promise.race([queue.onError(), queue.onIdle()])` to fail fast on the first error while still resolving normally when the queue goes idle.
|
|
296
|
+
|
|
297
|
+
> [!IMPORTANT]
|
|
298
|
+
> The promise returned by `add()` still rejects. You must handle each `add()` promise (for example, `.catch(() => {})`) to avoid unhandled rejections.
|
|
299
|
+
|
|
300
|
+
```js
|
|
301
|
+
import PQueue from 'p-queue';
|
|
302
|
+
|
|
303
|
+
const queue = new PQueue({concurrency: 2});
|
|
304
|
+
|
|
305
|
+
queue.add(() => fetchData(1)).catch(() => {});
|
|
306
|
+
queue.add(() => fetchData(2)).catch(() => {});
|
|
307
|
+
queue.add(() => fetchData(3)).catch(() => {});
|
|
308
|
+
|
|
309
|
+
// Stop processing on first error
|
|
310
|
+
try {
|
|
311
|
+
await Promise.race([
|
|
312
|
+
queue.onError(),
|
|
313
|
+
queue.onIdle()
|
|
314
|
+
]);
|
|
315
|
+
} catch (error) {
|
|
316
|
+
queue.pause(); // Stop processing remaining tasks
|
|
317
|
+
console.error('Queue failed:', error);
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
201
321
|
#### .onSizeLessThan(limit)
|
|
202
322
|
|
|
203
323
|
Returns a promise that settles when the queue size is less than the given limit: `queue.size < limit`.
|
|
@@ -236,18 +356,137 @@ console.log(queue.sizeBy({priority: 0}));
|
|
|
236
356
|
//=> 1
|
|
237
357
|
```
|
|
238
358
|
|
|
359
|
+
#### .setPriority(id, priority)
|
|
360
|
+
|
|
361
|
+
Updates the priority of a promise function by its id, affecting its execution order. Requires a defined concurrency limit to take effect.
|
|
362
|
+
|
|
363
|
+
For example, this can be used to prioritize a promise function to run earlier.
|
|
364
|
+
|
|
365
|
+
```js
|
|
366
|
+
import PQueue from 'p-queue';
|
|
367
|
+
|
|
368
|
+
const queue = new PQueue({concurrency: 1});
|
|
369
|
+
|
|
370
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
371
|
+
queue.add(async () => '🦀', {priority: 0, id: '🦀'});
|
|
372
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
373
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
374
|
+
|
|
375
|
+
queue.setPriority('🦀', 2);
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
In this case, the promise function with `id: '🦀'` runs second.
|
|
379
|
+
|
|
380
|
+
You can also deprioritize a promise function to delay its execution:
|
|
381
|
+
|
|
382
|
+
```js
|
|
383
|
+
import PQueue from 'p-queue';
|
|
384
|
+
|
|
385
|
+
const queue = new PQueue({concurrency: 1});
|
|
386
|
+
|
|
387
|
+
queue.add(async () => '🦄', {priority: 1});
|
|
388
|
+
queue.add(async () => '🦀', {priority: 1, id: '🦀'});
|
|
389
|
+
queue.add(async () => '🦄');
|
|
390
|
+
queue.add(async () => '🦄', {priority: 0});
|
|
391
|
+
|
|
392
|
+
queue.setPriority('🦀', -1);
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Here, the promise function with `id: '🦀'` executes last.
|
|
396
|
+
|
|
239
397
|
#### .pending
|
|
240
398
|
|
|
241
399
|
Number of running items (no longer in the queue).
|
|
242
400
|
|
|
243
401
|
#### [.timeout](#timeout)
|
|
244
402
|
|
|
403
|
+
Type: `number | undefined`
|
|
404
|
+
|
|
405
|
+
Get or set the default timeout for all tasks. Can be changed at runtime.
|
|
406
|
+
|
|
407
|
+
Operations will throw a `TimeoutError` if they don't complete within the specified time.
|
|
408
|
+
|
|
409
|
+
The timeout begins when the operation is dequeued and starts execution, not while it's waiting in the queue.
|
|
410
|
+
|
|
411
|
+
```js
|
|
412
|
+
const queue = new PQueue({timeout: 5000});
|
|
413
|
+
|
|
414
|
+
// Change timeout for all future tasks
|
|
415
|
+
queue.timeout = 10000;
|
|
416
|
+
```
|
|
417
|
+
|
|
245
418
|
#### [.concurrency](#concurrency)
|
|
246
419
|
|
|
247
420
|
#### .isPaused
|
|
248
421
|
|
|
249
422
|
Whether the queue is currently paused.
|
|
250
423
|
|
|
424
|
+
#### .isRateLimited
|
|
425
|
+
|
|
426
|
+
Whether the queue is currently rate-limited due to `intervalCap`. Returns `true` when the number of tasks executed in the current interval has reached the `intervalCap` and there are still tasks waiting to be processed.
|
|
427
|
+
|
|
428
|
+
#### .isSaturated
|
|
429
|
+
|
|
430
|
+
Whether the queue is saturated. Returns `true` when:
|
|
431
|
+
- All concurrency slots are occupied and tasks are waiting, OR
|
|
432
|
+
- The queue is rate-limited and tasks are waiting
|
|
433
|
+
|
|
434
|
+
Useful for detecting backpressure and potential hanging tasks.
|
|
435
|
+
|
|
436
|
+
```js
|
|
437
|
+
import PQueue from 'p-queue';
|
|
438
|
+
|
|
439
|
+
const queue = new PQueue({concurrency: 2});
|
|
440
|
+
|
|
441
|
+
// Backpressure handling
|
|
442
|
+
if (queue.isSaturated) {
|
|
443
|
+
console.log('Queue is saturated, waiting for capacity...');
|
|
444
|
+
await queue.onSizeLessThan(queue.concurrency);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Monitoring for stuck tasks
|
|
448
|
+
setInterval(() => {
|
|
449
|
+
if (queue.isSaturated) {
|
|
450
|
+
console.warn(`Queue saturated: ${queue.pending} running, ${queue.size} waiting`);
|
|
451
|
+
}
|
|
452
|
+
}, 60000);
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
#### .runningTasks
|
|
456
|
+
|
|
457
|
+
The tasks currently being executed. Each task includes its `id`, `priority`, `startTime`, and `timeout` (if set).
|
|
458
|
+
|
|
459
|
+
Returns an array of task info objects.
|
|
460
|
+
|
|
461
|
+
```js
|
|
462
|
+
import PQueue from 'p-queue';
|
|
463
|
+
|
|
464
|
+
const queue = new PQueue({concurrency: 2});
|
|
465
|
+
|
|
466
|
+
// Add tasks with IDs for better debugging
|
|
467
|
+
queue.add(() => fetchUser(123), {id: 'user-123'});
|
|
468
|
+
queue.add(() => fetchPosts(456), {id: 'posts-456', priority: 1});
|
|
469
|
+
|
|
470
|
+
// Check what's running
|
|
471
|
+
console.log(queue.runningTasks);
|
|
472
|
+
/*
|
|
473
|
+
[
|
|
474
|
+
{
|
|
475
|
+
id: 'user-123',
|
|
476
|
+
priority: 0,
|
|
477
|
+
startTime: 1759253001716,
|
|
478
|
+
timeout: undefined
|
|
479
|
+
},
|
|
480
|
+
{
|
|
481
|
+
id: 'posts-456',
|
|
482
|
+
priority: 1,
|
|
483
|
+
startTime: 1759253001916,
|
|
484
|
+
timeout: undefined
|
|
485
|
+
}
|
|
486
|
+
]
|
|
487
|
+
*/
|
|
488
|
+
```
|
|
489
|
+
|
|
251
490
|
## Events
|
|
252
491
|
|
|
253
492
|
#### active
|
|
@@ -291,10 +530,9 @@ queue.add(() => Promise.resolve('hello, world!'));
|
|
|
291
530
|
|
|
292
531
|
#### error
|
|
293
532
|
|
|
294
|
-
Emitted if an item throws an error.
|
|
533
|
+
Emitted if an item throws an error. The promise returned by `add()` is still rejected, so you must handle both.
|
|
295
534
|
|
|
296
535
|
```js
|
|
297
|
-
import delay from 'delay';
|
|
298
536
|
import PQueue from 'p-queue';
|
|
299
537
|
|
|
300
538
|
const queue = new PQueue({concurrency: 2});
|
|
@@ -303,7 +541,10 @@ queue.on('error', error => {
|
|
|
303
541
|
console.error(error);
|
|
304
542
|
});
|
|
305
543
|
|
|
306
|
-
|
|
544
|
+
// Handle the promise to prevent unhandled rejection
|
|
545
|
+
queue.add(() => Promise.reject(new Error('error'))).catch(() => {
|
|
546
|
+
// Error already handled by event listener
|
|
547
|
+
});
|
|
307
548
|
```
|
|
308
549
|
|
|
309
550
|
#### empty
|
|
@@ -318,6 +559,12 @@ Emitted every time the queue becomes empty and all promises have completed; `que
|
|
|
318
559
|
|
|
319
560
|
The difference with `empty` is that `idle` guarantees that all work from the queue has finished. `empty` merely signals that the queue is empty, but it could mean that some promises haven't completed yet.
|
|
320
561
|
|
|
562
|
+
#### pendingZero
|
|
563
|
+
|
|
564
|
+
Emitted every time the number of running tasks becomes zero; `queue.pending === 0`.
|
|
565
|
+
|
|
566
|
+
The difference with `idle` is that `pendingZero` is emitted even when the queue still has items waiting to run, whereas `idle` requires both an empty queue and no pending tasks.
|
|
567
|
+
|
|
321
568
|
```js
|
|
322
569
|
import delay from 'delay';
|
|
323
570
|
import PQueue from 'p-queue';
|
|
@@ -376,6 +623,56 @@ await queue.add(() => delay(600));
|
|
|
376
623
|
//=> 'Task is completed. Size: 0 Pending: 0'
|
|
377
624
|
```
|
|
378
625
|
|
|
626
|
+
#### rateLimit
|
|
627
|
+
|
|
628
|
+
Emitted when the queue becomes rate-limited due to `intervalCap`. This happens when the maximum number of tasks allowed per interval has been reached.
|
|
629
|
+
|
|
630
|
+
Useful for implementing backpressure to prevent memory issues when producers are faster than consumers.
|
|
631
|
+
|
|
632
|
+
```js
|
|
633
|
+
import delay from 'delay';
|
|
634
|
+
import PQueue from 'p-queue';
|
|
635
|
+
|
|
636
|
+
const queue = new PQueue({
|
|
637
|
+
intervalCap: 2,
|
|
638
|
+
interval: 1000
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
queue.on('rateLimit', () => {
|
|
642
|
+
console.log('Queue is rate-limited - processing backlog or maintenance tasks');
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
// Add 3 tasks - third one triggers rate limiting
|
|
646
|
+
queue.add(() => delay(100));
|
|
647
|
+
queue.add(() => delay(100));
|
|
648
|
+
queue.add(() => delay(100));
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
#### rateLimitCleared
|
|
652
|
+
|
|
653
|
+
Emitted when the queue is no longer rate-limited—either because the interval reset and new tasks can start, or because the backlog was drained.
|
|
654
|
+
|
|
655
|
+
```js
|
|
656
|
+
import delay from 'delay';
|
|
657
|
+
import PQueue from 'p-queue';
|
|
658
|
+
|
|
659
|
+
const queue = new PQueue({
|
|
660
|
+
intervalCap: 1,
|
|
661
|
+
interval: 1000
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
queue.on('rateLimit', () => {
|
|
665
|
+
console.log('Rate limited - waiting for interval to reset');
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
queue.on('rateLimitCleared', () => {
|
|
669
|
+
console.log('Rate limit cleared - can process more tasks');
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
queue.add(() => delay(100));
|
|
673
|
+
queue.add(() => delay(100)); // This triggers rate limiting
|
|
674
|
+
```
|
|
675
|
+
|
|
379
676
|
## Advanced example
|
|
380
677
|
|
|
381
678
|
A more advanced example to help you understand the flow.
|
|
@@ -446,6 +743,51 @@ $ node example.js
|
|
|
446
743
|
12. All work is done
|
|
447
744
|
```
|
|
448
745
|
|
|
746
|
+
## Handling timeouts
|
|
747
|
+
|
|
748
|
+
You can set a timeout for all tasks or override it per task. When a task times out, a `TimeoutError` is thrown.
|
|
749
|
+
|
|
750
|
+
```js
|
|
751
|
+
import PQueue, {TimeoutError} from 'p-queue';
|
|
752
|
+
|
|
753
|
+
// Set a global timeout for all tasks
|
|
754
|
+
const queue = new PQueue({
|
|
755
|
+
concurrency: 2,
|
|
756
|
+
timeout: 5000, // 5 seconds
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
(async () => {
|
|
760
|
+
// This task will use the global timeout
|
|
761
|
+
try {
|
|
762
|
+
await queue.add(() => fetchData());
|
|
763
|
+
} catch (error) {
|
|
764
|
+
if (error instanceof TimeoutError) {
|
|
765
|
+
console.log('Task timed out after 5 seconds');
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
})();
|
|
769
|
+
|
|
770
|
+
(async () => {
|
|
771
|
+
// Override timeout for a specific task
|
|
772
|
+
try {
|
|
773
|
+
await queue.add(() => slowTask(), {
|
|
774
|
+
timeout: 10000, // 10 seconds for this task only
|
|
775
|
+
});
|
|
776
|
+
} catch (error) {
|
|
777
|
+
if (error instanceof TimeoutError) {
|
|
778
|
+
console.log('Slow task timed out after 10 seconds');
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
})();
|
|
782
|
+
|
|
783
|
+
(async () => {
|
|
784
|
+
// No timeout for this task
|
|
785
|
+
await queue.add(() => verySlowTask(), {
|
|
786
|
+
timeout: undefined,
|
|
787
|
+
});
|
|
788
|
+
})();
|
|
789
|
+
```
|
|
790
|
+
|
|
449
791
|
## Custom QueueClass
|
|
450
792
|
|
|
451
793
|
For implementing more complex scheduling policies, you can provide a QueueClass in the options:
|
|
@@ -486,6 +828,186 @@ const queue = new PQueue({queueClass: QueueClass});
|
|
|
486
828
|
|
|
487
829
|
They are just different constraints. The `concurrency` option limits how many things run at the same time. The `intervalCap` option limits how many things run in total during the interval (over time).
|
|
488
830
|
|
|
831
|
+
#### How do I implement backpressure?
|
|
832
|
+
|
|
833
|
+
Use `.onSizeLessThan()` to prevent the queue from growing unbounded and causing memory issues when producers are faster than consumers:
|
|
834
|
+
|
|
835
|
+
```js
|
|
836
|
+
const queue = new PQueue();
|
|
837
|
+
|
|
838
|
+
// Wait for queue to have space before adding more
|
|
839
|
+
await queue.onSizeLessThan(100);
|
|
840
|
+
queue.add(() => someTask());
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
Note: `.size` counts queued items, while `.pending` counts running items. The total is `queue.size + queue.pending`.
|
|
844
|
+
|
|
845
|
+
You can also use `.onRateLimit()` for backpressure during rate limiting. See the [`.onRateLimit()`](#onratelimit) docs.
|
|
846
|
+
|
|
847
|
+
#### How do I cancel or remove a queued task?
|
|
848
|
+
|
|
849
|
+
Use `AbortSignal` for targeted cancellation. Aborting removes a waiting task and rejects the `.add()` promise. For bulk operations, use `queue.clear()` or share one `AbortController` across tasks.
|
|
850
|
+
|
|
851
|
+
```js
|
|
852
|
+
import PQueue from 'p-queue';
|
|
853
|
+
|
|
854
|
+
const queue = new PQueue();
|
|
855
|
+
const controller = new AbortController();
|
|
856
|
+
|
|
857
|
+
const promise = queue.add(({signal}) => doWork({signal}), {signal: controller.signal});
|
|
858
|
+
|
|
859
|
+
controller.abort(); // Cancels if still queued; running tasks must handle `signal` themselves
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
Direct removal methods are not provided as they would leak internals and risk dangling promises.
|
|
863
|
+
|
|
864
|
+
#### How do I get results in the order they were added?
|
|
865
|
+
|
|
866
|
+
This package executes tasks in priority order, but doesn't guarantee completion order. If you need results in the order they were added, use `Promise.all()`, which maintains the order of the input array:
|
|
867
|
+
|
|
868
|
+
```js
|
|
869
|
+
import PQueue from 'p-queue';
|
|
870
|
+
|
|
871
|
+
const queue = new PQueue({concurrency: 4});
|
|
872
|
+
|
|
873
|
+
const tasks = [
|
|
874
|
+
() => fetchData(1), // May finish third
|
|
875
|
+
() => fetchData(2), // May finish first
|
|
876
|
+
() => fetchData(3), // May finish second
|
|
877
|
+
];
|
|
878
|
+
|
|
879
|
+
const results = await Promise.all(
|
|
880
|
+
tasks.map(task => queue.add(task))
|
|
881
|
+
);
|
|
882
|
+
// results = [result1, result2, result3] ✅ Always in input order
|
|
883
|
+
|
|
884
|
+
// Or more concisely:
|
|
885
|
+
const urls = ['url1', 'url2', 'url3'];
|
|
886
|
+
const results = await Promise.all(
|
|
887
|
+
urls.map(url => queue.add(() => fetch(url)))
|
|
888
|
+
);
|
|
889
|
+
```
|
|
890
|
+
|
|
891
|
+
If you don't need `p-queue`'s advanced features, consider using [`p-map`](https://github.com/sindresorhus/p-map), which is specifically designed for this use case.
|
|
892
|
+
|
|
893
|
+
#### How do I stream results as they complete in order?
|
|
894
|
+
|
|
895
|
+
For progressive results that maintain input order, use [`pMapIterable`](https://github.com/sindresorhus/p-map#pmapiterable) from `p-map`:
|
|
896
|
+
|
|
897
|
+
```js
|
|
898
|
+
import {pMapIterable} from 'p-map';
|
|
899
|
+
|
|
900
|
+
// Stream results in order as they complete
|
|
901
|
+
for await (const result of pMapIterable(items, fetchItem, {concurrency: 4})) {
|
|
902
|
+
console.log(result); // Results arrive in input order
|
|
903
|
+
}
|
|
904
|
+
```
|
|
905
|
+
|
|
906
|
+
You can combine it with `p-queue` when you need priorities or a shared concurrency cap:
|
|
907
|
+
|
|
908
|
+
```js
|
|
909
|
+
import PQueue from 'p-queue';
|
|
910
|
+
import {pMapIterable} from 'p-map';
|
|
911
|
+
|
|
912
|
+
// Let p-queue handle concurrency
|
|
913
|
+
const queue = new PQueue({concurrency: 4});
|
|
914
|
+
|
|
915
|
+
for await (const result of pMapIterable(
|
|
916
|
+
items,
|
|
917
|
+
item => queue.add(() => fetchItem(item), {priority: item.priority})
|
|
918
|
+
)) {
|
|
919
|
+
console.log(result); // Still in input order
|
|
920
|
+
}
|
|
921
|
+
```
|
|
922
|
+
|
|
923
|
+
#### How do I debug a queue that stops processing tasks?
|
|
924
|
+
|
|
925
|
+
If your queue stops processing tasks after extended use, it's likely that some tasks are hanging indefinitely, exhausting the concurrency limit. Use the `.runningTasks` property to identify which specific tasks are stuck.
|
|
926
|
+
|
|
927
|
+
Common causes:
|
|
928
|
+
- Network requests without timeouts
|
|
929
|
+
- Database queries that hang
|
|
930
|
+
- File operations on unresponsive network drives
|
|
931
|
+
- Unhandled promise rejections
|
|
932
|
+
|
|
933
|
+
Debugging steps:
|
|
934
|
+
|
|
935
|
+
```js
|
|
936
|
+
// 1. Add timeouts to prevent hanging
|
|
937
|
+
const queue = new PQueue({
|
|
938
|
+
concurrency: 2,
|
|
939
|
+
timeout: 30000 // 30 seconds
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
// 2. Always add IDs to tasks for debugging
|
|
943
|
+
queue.add(() => processItem(item), {id: `item-${item.id}`});
|
|
944
|
+
|
|
945
|
+
// 3. Monitor for stuck tasks using runningTasks
|
|
946
|
+
setInterval(() => {
|
|
947
|
+
const now = Date.now();
|
|
948
|
+
const stuckTasks = queue.runningTasks.filter(task =>
|
|
949
|
+
now - task.startTime > 30000 // Running for over 30 seconds
|
|
950
|
+
);
|
|
951
|
+
|
|
952
|
+
if (stuckTasks.length > 0) {
|
|
953
|
+
console.error('Stuck tasks:', stuckTasks);
|
|
954
|
+
// Consider aborting or logging more details
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
// Detect saturation (potential hanging if persistent)
|
|
958
|
+
if (queue.isSaturated) {
|
|
959
|
+
console.warn(`Queue saturated: ${queue.pending} running, ${queue.size} waiting`);
|
|
960
|
+
}
|
|
961
|
+
}, 60000);
|
|
962
|
+
|
|
963
|
+
// 4. Track task lifecycle
|
|
964
|
+
queue.on('completed', result => {
|
|
965
|
+
console.log('Task completed');
|
|
966
|
+
});
|
|
967
|
+
queue.on('error', error => {
|
|
968
|
+
console.error('Task failed:', error);
|
|
969
|
+
});
|
|
970
|
+
|
|
971
|
+
// 5. Wrap tasks with debugging
|
|
972
|
+
const debugTask = async (fn, name) => {
|
|
973
|
+
const start = Date.now();
|
|
974
|
+
console.log(`Starting: ${name}`);
|
|
975
|
+
try {
|
|
976
|
+
const result = await fn();
|
|
977
|
+
console.log(`Completed: ${name} (${Date.now() - start}ms)`);
|
|
978
|
+
return result;
|
|
979
|
+
} catch (error) {
|
|
980
|
+
console.error(`Failed: ${name} (${Date.now() - start}ms)`, error);
|
|
981
|
+
throw error;
|
|
982
|
+
}
|
|
983
|
+
};
|
|
984
|
+
|
|
985
|
+
queue.add(() => debugTask(() => fetchData(), 'fetchData'), {id: 'fetchData'});
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
Prevention:
|
|
989
|
+
- Always use timeouts for I/O operations
|
|
990
|
+
- Ensure all async functions properly resolve or reject
|
|
991
|
+
- Use the `timeout` option to enforce task time limits
|
|
992
|
+
- Monitor `queue.size` and `queue.pending` in production
|
|
993
|
+
|
|
994
|
+
#### How do I test code that uses `p-queue` with Jest fake timers?
|
|
995
|
+
|
|
996
|
+
Jest fake timers don't work well with `p-queue` because it uses `queueMicrotask` internally.
|
|
997
|
+
|
|
998
|
+
Workaround:
|
|
999
|
+
|
|
1000
|
+
```js
|
|
1001
|
+
const flushPromises = () => new Promise(resolve => setImmediate(resolve));
|
|
1002
|
+
|
|
1003
|
+
jest.useFakeTimers();
|
|
1004
|
+
|
|
1005
|
+
// ... your test code ...
|
|
1006
|
+
|
|
1007
|
+
await jest.runAllTimersAsync();
|
|
1008
|
+
await flushPromises();
|
|
1009
|
+
```
|
|
1010
|
+
|
|
489
1011
|
## Maintainers
|
|
490
1012
|
|
|
491
1013
|
- [Sindre Sorhus](https://github.com/sindresorhus)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export class TimeoutError extends Error {
|
|
2
2
|
readonly name: 'TimeoutError';
|
|
3
|
-
constructor(message?: string);
|
|
3
|
+
constructor(message?: string, options?: ErrorOptions);
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export type ClearablePromise<T> = {
|
|
@@ -92,9 +92,7 @@ export type Options<ReturnType> = {
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
_Requires Node.js 16 or later._
|
|
95
|
+
Abort the promise.
|
|
98
96
|
|
|
99
97
|
@example
|
|
100
98
|
```
|