@cldmv/holdmytask 1.4.0 → 1.6.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 +206 -1
- package/dist/hold-my-task.mjs +569 -33
- package/package.json +1 -1
- package/types/dist/hold-my-task.d.mts +25 -0
- package/types/dist/hold-my-task.d.mts.map +1 -1
package/README.md
CHANGED
|
@@ -265,7 +265,10 @@ This pattern is particularly useful when you need to handle initialization error
|
|
|
265
265
|
- `defaultPriority` (number, default: 0) - Default task priority
|
|
266
266
|
- `maxQueue` (number, default: Infinity) - Maximum queued tasks. Use `-1` for unlimited queue capacity (equivalent to `Infinity`)
|
|
267
267
|
- `delays` (object, default: {}) - **DEPRECATED:** Priority-to-delay mapping for completion delays (use `priorities` instead)
|
|
268
|
-
- `priorities` (object, default: {}) - Priority-specific configuration: `{ [priority]: {
|
|
268
|
+
- `priorities` (object, default: {}) - Priority-specific configuration: `{ [priority]: { concurrency, postDelay, startDelay } }`
|
|
269
|
+
- `concurrency` (number) - Maximum concurrent tasks for this priority (defaults to global concurrency limit)
|
|
270
|
+
- `postDelay` (number) - Delay after task completion before next task of same priority
|
|
271
|
+
- `startDelay` (number) - Delay before task execution (pre-execution delay)
|
|
269
272
|
- `coalescing` (object) - Enhanced coalescing configuration
|
|
270
273
|
- `defaults` (object) - Default settings for all coalescing keys
|
|
271
274
|
- `windowDuration` (number, default: 200) - Window duration in milliseconds
|
|
@@ -480,6 +483,102 @@ const groupInfo = queue.getCoalescingGroup("user-action");
|
|
|
480
483
|
const taskGroup = queue.findCoalescingGroupByTaskId(task1.id);
|
|
481
484
|
```
|
|
482
485
|
|
|
486
|
+
### Debug and Inspection Methods
|
|
487
|
+
|
|
488
|
+
For debugging queue behavior, troubleshooting scheduler issues, or monitoring queue state in production, HoldMyTask provides comprehensive inspection methods:
|
|
489
|
+
|
|
490
|
+
#### Quick Debug Output
|
|
491
|
+
|
|
492
|
+
```javascript
|
|
493
|
+
// Log comprehensive queue state to console
|
|
494
|
+
queue.debugLog(); // Basic summary
|
|
495
|
+
queue.debugLog(true); // Detailed task information
|
|
496
|
+
|
|
497
|
+
// Example output:
|
|
498
|
+
// === HoldMyTask Debug Information ===
|
|
499
|
+
// Timestamp: 2025-11-14T17:45:32.123Z
|
|
500
|
+
// Status: ACTIVE
|
|
501
|
+
// Smart Scheduling: ON
|
|
502
|
+
// Concurrency: 2/5
|
|
503
|
+
//
|
|
504
|
+
// --- Queue State ---
|
|
505
|
+
// Total Tasks: 7
|
|
506
|
+
// Pending: 3
|
|
507
|
+
// Ready: 2
|
|
508
|
+
// Running: 2
|
|
509
|
+
//
|
|
510
|
+
// --- Timer State ---
|
|
511
|
+
// Interval: None
|
|
512
|
+
// Timeout: Active (15)
|
|
513
|
+
// Next Run: 1250ms
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
#### Detailed Inspection
|
|
517
|
+
|
|
518
|
+
```javascript
|
|
519
|
+
// Get comprehensive queue state
|
|
520
|
+
const state = queue.inspect();
|
|
521
|
+
console.log("Total tasks:", state.totals.totalTasks);
|
|
522
|
+
console.log("Running tasks:", state.queues.running.tasks);
|
|
523
|
+
console.log("Next scheduled run:", state.timers.nextRunIn, "ms");
|
|
524
|
+
|
|
525
|
+
// Inspect just the tasks
|
|
526
|
+
const tasks = queue.inspectTasks();
|
|
527
|
+
console.log("Tasks by priority:", tasks.byPriority);
|
|
528
|
+
console.log("Tasks by coalescing key:", tasks.byCoalescingKey);
|
|
529
|
+
|
|
530
|
+
// Inspect scheduler state
|
|
531
|
+
const scheduler = queue.inspectScheduler();
|
|
532
|
+
console.log("Can run task now:", scheduler.nextTask?.canRunNow);
|
|
533
|
+
console.log("Available slots:", scheduler.availableSlots);
|
|
534
|
+
console.log("Active delays:", scheduler.delays.count);
|
|
535
|
+
|
|
536
|
+
// Inspect timers specifically
|
|
537
|
+
const timers = queue.inspectTimers();
|
|
538
|
+
console.log("Scheduler interval active:", timers.schedulerInterval.active);
|
|
539
|
+
console.log("Next run in:", timers.nextRunIn, "ms");
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### Inspection Methods
|
|
543
|
+
|
|
544
|
+
- `inspect()` - Get comprehensive queue state including tasks, timers, scheduler, and coalescing information
|
|
545
|
+
- `inspectTasks()` - Get detailed task information grouped by status, priority, and coalescing key
|
|
546
|
+
- `inspectScheduler()` - Get scheduler state, timing information, and next task details
|
|
547
|
+
- `inspectTimers()` - Get timer state and scheduling information
|
|
548
|
+
- `debugLog(detailed?)` - Log formatted debug information to console
|
|
549
|
+
|
|
550
|
+
**Use Cases:**
|
|
551
|
+
|
|
552
|
+
- **Development**: Debug why tasks aren't running or understand queue behavior
|
|
553
|
+
- **Production Monitoring**: Track queue health and performance metrics
|
|
554
|
+
- **Troubleshooting**: Identify scheduler issues, timer problems, or concurrency bottlenecks
|
|
555
|
+
- **Testing**: Verify queue state in automated tests
|
|
556
|
+
|
|
557
|
+
```javascript
|
|
558
|
+
// Example: Debug scheduler restart issue
|
|
559
|
+
const queue = new HoldMyTask({ concurrency: 2 });
|
|
560
|
+
|
|
561
|
+
// Add some tasks
|
|
562
|
+
queue.enqueue(async () => "task1");
|
|
563
|
+
queue.enqueue(async () => "task2", { priority: 2, start: 5000 });
|
|
564
|
+
|
|
565
|
+
// Check initial state
|
|
566
|
+
console.log("Initial state:");
|
|
567
|
+
queue.debugLog();
|
|
568
|
+
|
|
569
|
+
// Wait for drain
|
|
570
|
+
queue.on("drain", () => {
|
|
571
|
+
console.log("After drain:");
|
|
572
|
+
queue.debugLog();
|
|
573
|
+
|
|
574
|
+
// Add new task
|
|
575
|
+
queue.enqueue(async () => "task3");
|
|
576
|
+
|
|
577
|
+
console.log("After adding new task:");
|
|
578
|
+
queue.debugLog();
|
|
579
|
+
});
|
|
580
|
+
```
|
|
581
|
+
|
|
483
582
|
### Events
|
|
484
583
|
|
|
485
584
|
```javascript
|
|
@@ -749,6 +848,112 @@ const legacyQueue = new HoldMyTask({
|
|
|
749
848
|
- Includes healing mechanism to prevent scheduler stalls
|
|
750
849
|
- Falls back gracefully on complex timing scenarios
|
|
751
850
|
|
|
851
|
+
### Per-Priority Concurrency Limits
|
|
852
|
+
|
|
853
|
+
HoldMyTask supports granular concurrency control at the priority level, allowing you to fine-tune how many tasks of each priority can run simultaneously while still respecting the global concurrency limit.
|
|
854
|
+
|
|
855
|
+
#### Key Benefits
|
|
856
|
+
|
|
857
|
+
- **Database operations** - Run only 1 critical database migration at a time
|
|
858
|
+
- **API rate limiting** - Limit API calls per priority to avoid overwhelming services
|
|
859
|
+
- **Resource management** - Control expensive operations while allowing lightweight tasks
|
|
860
|
+
- **Flexible scaling** - Different priorities can have different concurrency characteristics
|
|
861
|
+
|
|
862
|
+
#### Configuration
|
|
863
|
+
|
|
864
|
+
```javascript
|
|
865
|
+
const queue = new HoldMyTask({
|
|
866
|
+
concurrency: 8, // Global maximum: 8 total tasks across all priorities
|
|
867
|
+
priorities: {
|
|
868
|
+
1: {
|
|
869
|
+
concurrency: 1, // Critical: Only 1 at a time (database migrations, etc.)
|
|
870
|
+
postDelay: 100, // 100ms delay after completion
|
|
871
|
+
startDelay: 0 // No pre-execution delay
|
|
872
|
+
},
|
|
873
|
+
2: {
|
|
874
|
+
concurrency: 3, // Important: Up to 3 at a time (API calls, file processing)
|
|
875
|
+
postDelay: 200, // 200ms delay after completion
|
|
876
|
+
startDelay: 50 // 50ms pre-execution delay
|
|
877
|
+
},
|
|
878
|
+
3: {
|
|
879
|
+
concurrency: 5, // Background: Up to 5 at a time (cleanup, logging, etc.)
|
|
880
|
+
postDelay: 0, // No post-completion delay
|
|
881
|
+
startDelay: 100 // 100ms pre-execution delay
|
|
882
|
+
}
|
|
883
|
+
// Priority 4+ tasks: No specific limit, use global concurrency
|
|
884
|
+
}
|
|
885
|
+
});
|
|
886
|
+
```
|
|
887
|
+
|
|
888
|
+
#### How It Works
|
|
889
|
+
|
|
890
|
+
1. **Global Limit First** - The total running tasks never exceed the global `concurrency` setting
|
|
891
|
+
2. **Per-Priority Limits** - Tasks of each priority respect their individual `concurrency` limit
|
|
892
|
+
3. **Dynamic Scheduling** - If one priority is at its limit, other priorities can still start tasks
|
|
893
|
+
4. **Automatic Fallback** - Priorities without `concurrency` settings use the global limit
|
|
894
|
+
|
|
895
|
+
#### Usage Examples
|
|
896
|
+
|
|
897
|
+
```javascript
|
|
898
|
+
// Database migration (priority 1): Only 1 can run at a time
|
|
899
|
+
await queue.enqueue(
|
|
900
|
+
async () => {
|
|
901
|
+
await runDatabaseMigration();
|
|
902
|
+
},
|
|
903
|
+
{ priority: 1 }
|
|
904
|
+
);
|
|
905
|
+
|
|
906
|
+
// API calls (priority 2): Up to 3 can run concurrently
|
|
907
|
+
await queue.enqueue(
|
|
908
|
+
async () => {
|
|
909
|
+
return await fetchUserData(userId);
|
|
910
|
+
},
|
|
911
|
+
{ priority: 2 }
|
|
912
|
+
);
|
|
913
|
+
|
|
914
|
+
// Background cleanup (priority 3): Up to 5 can run concurrently
|
|
915
|
+
await queue.enqueue(
|
|
916
|
+
async () => {
|
|
917
|
+
await cleanupTempFiles();
|
|
918
|
+
},
|
|
919
|
+
{ priority: 3 }
|
|
920
|
+
);
|
|
921
|
+
|
|
922
|
+
// High-priority urgent task (priority 10): Uses global concurrency limit
|
|
923
|
+
await queue.enqueue(
|
|
924
|
+
async () => {
|
|
925
|
+
await handleEmergencyAlert();
|
|
926
|
+
},
|
|
927
|
+
{ priority: 10 }
|
|
928
|
+
);
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
#### Monitoring Per-Priority Concurrency
|
|
932
|
+
|
|
933
|
+
```javascript
|
|
934
|
+
// Check current concurrency state
|
|
935
|
+
const inspection = queue.inspect();
|
|
936
|
+
console.log("Global concurrency:", inspection.scheduler.currentConcurrency + "/" + inspection.scheduler.concurrency);
|
|
937
|
+
|
|
938
|
+
// Per-priority concurrency information
|
|
939
|
+
Object.entries(inspection.scheduler.priorityConcurrency).forEach(([priority, info]) => {
|
|
940
|
+
console.log(`Priority ${priority}: ${info.running}/${info.limit} (${info.available} available)`);
|
|
941
|
+
});
|
|
942
|
+
|
|
943
|
+
// Or use the scheduler-specific inspection
|
|
944
|
+
const scheduler = queue.inspectScheduler();
|
|
945
|
+
console.log("Priority concurrency limits:", scheduler.priorityConcurrency);
|
|
946
|
+
```
|
|
947
|
+
|
|
948
|
+
#### Example Output
|
|
949
|
+
|
|
950
|
+
```text
|
|
951
|
+
Global concurrency: 6/8
|
|
952
|
+
Priority 1: 1/1 (0 available)
|
|
953
|
+
Priority 2: 2/3 (1 available)
|
|
954
|
+
Priority 3: 3/5 (2 available)
|
|
955
|
+
```
|
|
956
|
+
|
|
752
957
|
### Priority Delays - Advanced Timing Control
|
|
753
958
|
|
|
754
959
|
Priority delays create "cool-down" periods after task completion based on the completed task's priority. This helps prevent resource overwhelming, implement rate limiting, and control timing between operations.
|
package/dist/hold-my-task.mjs
CHANGED
|
@@ -34,6 +34,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
34
34
|
* @param {number} [options.tick=25] - Polling interval in milliseconds when smartScheduling is disabled
|
|
35
35
|
* @param {number} [options.healingInterval=5000] - Self-healing check interval in milliseconds (smart scheduling only)
|
|
36
36
|
* @param {Object} [options.priorities={}] - Priority-specific default configurations
|
|
37
|
+
* @param {number} [options.priorities[priority].concurrency] - Maximum concurrent tasks for this priority (defaults to global concurrency limit)
|
|
38
|
+
* @param {number} [options.priorities[priority].postDelay] - Delay after task completion before next task of same priority
|
|
39
|
+
* @param {number} [options.priorities[priority].startDelay] - Delay before task execution (pre-execution delay)
|
|
37
40
|
* @param {Object} [options.coalescing] - Enhanced coalescing configuration (preferred over flat options)
|
|
38
41
|
* @param {Object} [options.coalescing.defaults] - Default coalescing settings for all keys
|
|
39
42
|
* @param {number} [options.coalescing.defaults.windowDuration=200] - Default window duration in milliseconds
|
|
@@ -52,12 +55,12 @@ export class HoldMyTask extends EventEmitter {
|
|
|
52
55
|
* @example
|
|
53
56
|
* // Enhanced configuration with priority defaults and extended coalescing
|
|
54
57
|
* const queue = new HoldMyTask({
|
|
55
|
-
* concurrency:
|
|
58
|
+
* concurrency: 8, // Global maximum: 8 total tasks across all priorities
|
|
56
59
|
* delays: { 0: 1000, 1: 500 },
|
|
57
60
|
* priorities: {
|
|
58
|
-
* 1: { postDelay: 100, startDelay: 0 }, //
|
|
59
|
-
* 2: { postDelay: 200, startDelay: 50 }, //
|
|
60
|
-
* 3: { postDelay: 0, startDelay: 100 } //
|
|
61
|
+
* 1: { concurrency: 1, postDelay: 100, startDelay: 0 }, // Critical: Only 1 at a time, 100ms post-delay
|
|
62
|
+
* 2: { concurrency: 3, postDelay: 200, startDelay: 50 }, // Important: Up to 3 at a time, 200ms post-delay
|
|
63
|
+
* 3: { concurrency: 5, postDelay: 0, startDelay: 100 } // Background: Up to 5 at a time, 100ms pre-delay
|
|
61
64
|
* },
|
|
62
65
|
* coalescing: {
|
|
63
66
|
* defaults: {
|
|
@@ -323,6 +326,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
323
326
|
});
|
|
324
327
|
|
|
325
328
|
this.running = new Set();
|
|
329
|
+
this.runningByPriority = new Map(); // Track running tasks per priority for concurrency limits
|
|
326
330
|
this.tasks = new Map();
|
|
327
331
|
this.nextId = 1;
|
|
328
332
|
this.enqueueSeq = 1;
|
|
@@ -1141,13 +1145,15 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1141
1145
|
return null;
|
|
1142
1146
|
}
|
|
1143
1147
|
const groupInfo = this.findCoalescingGroupByTaskId(item.id);
|
|
1144
|
-
return groupInfo
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1148
|
+
return groupInfo
|
|
1149
|
+
? {
|
|
1150
|
+
coalescingKey: groupInfo.coalescingKey,
|
|
1151
|
+
groupId: groupInfo.groupId,
|
|
1152
|
+
representativeId: groupInfo.representativeId,
|
|
1153
|
+
taskCount: groupInfo.groupTasks.length,
|
|
1154
|
+
allTaskMetadata: groupInfo.groupTasks.map((t) => ({ id: t.id, metadata: t.metadata }))
|
|
1155
|
+
}
|
|
1156
|
+
: null;
|
|
1151
1157
|
}
|
|
1152
1158
|
};
|
|
1153
1159
|
|
|
@@ -1198,13 +1204,15 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1198
1204
|
return null;
|
|
1199
1205
|
}
|
|
1200
1206
|
const groupInfo = this.findCoalescingGroupByTaskId(item.id);
|
|
1201
|
-
return groupInfo
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1207
|
+
return groupInfo
|
|
1208
|
+
? {
|
|
1209
|
+
coalescingKey: groupInfo.coalescingKey,
|
|
1210
|
+
groupId: groupInfo.groupId,
|
|
1211
|
+
representativeId: groupInfo.representativeId,
|
|
1212
|
+
taskCount: groupInfo.groupTasks.length,
|
|
1213
|
+
allTaskMetadata: groupInfo.groupTasks.map((t) => ({ id: t.id, metadata: t.metadata }))
|
|
1214
|
+
}
|
|
1215
|
+
: null;
|
|
1208
1216
|
},
|
|
1209
1217
|
enumerable: true
|
|
1210
1218
|
});
|
|
@@ -1397,11 +1405,11 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1397
1405
|
* @example
|
|
1398
1406
|
* // Get all groups for a coalescing key
|
|
1399
1407
|
* const groups = queue.getCoalescingGroup('ui.update');
|
|
1400
|
-
*
|
|
1408
|
+
*
|
|
1401
1409
|
* // Get specific group by ID
|
|
1402
1410
|
* const group = queue.getCoalescingGroup('ui.update', '1');
|
|
1403
1411
|
* console.log(group.tasks.size); // Number of tasks in group
|
|
1404
|
-
*
|
|
1412
|
+
*
|
|
1405
1413
|
* // Access individual task metadata
|
|
1406
1414
|
* for (const [taskId, task] of group.tasks) {
|
|
1407
1415
|
* console.log(`Task ${taskId}:`, task.metadata);
|
|
@@ -1415,7 +1423,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1415
1423
|
|
|
1416
1424
|
if (groupId === null) {
|
|
1417
1425
|
// Return all groups for the key
|
|
1418
|
-
return groups.map(group => ({
|
|
1426
|
+
return groups.map((group) => ({
|
|
1419
1427
|
coalescingKey: group.coalescingKey,
|
|
1420
1428
|
groupId: group.groupId,
|
|
1421
1429
|
representativeId: group.representativeId,
|
|
@@ -1434,7 +1442,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1434
1442
|
}
|
|
1435
1443
|
|
|
1436
1444
|
// Find specific group by ID
|
|
1437
|
-
const group = groups.find(g => g.groupId === groupId);
|
|
1445
|
+
const group = groups.find((g) => g.groupId === groupId);
|
|
1438
1446
|
if (!group) {
|
|
1439
1447
|
return null;
|
|
1440
1448
|
}
|
|
@@ -1465,10 +1473,10 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1465
1473
|
* @example
|
|
1466
1474
|
* // Get metadata from all groups for a key
|
|
1467
1475
|
* const allMetadata = queue.getCoalescingGroupMetadata('ui.update');
|
|
1468
|
-
*
|
|
1476
|
+
*
|
|
1469
1477
|
* // Get metadata from specific group
|
|
1470
1478
|
* const groupMetadata = queue.getCoalescingGroupMetadata('ui.update', '1');
|
|
1471
|
-
*
|
|
1479
|
+
*
|
|
1472
1480
|
* // Example output:
|
|
1473
1481
|
* // [
|
|
1474
1482
|
* // { taskId: '123', metadata: { userId: 100, action: 'save' } },
|
|
@@ -1481,7 +1489,7 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1481
1489
|
return [];
|
|
1482
1490
|
}
|
|
1483
1491
|
|
|
1484
|
-
const targetGroups = groupId ? groups.filter(g => g.groupId === groupId) : groups;
|
|
1492
|
+
const targetGroups = groupId ? groups.filter((g) => g.groupId === groupId) : groups;
|
|
1485
1493
|
const metadata = [];
|
|
1486
1494
|
|
|
1487
1495
|
for (const group of targetGroups) {
|
|
@@ -1648,13 +1656,13 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1648
1656
|
nextPending = this.pendingHeap.peek();
|
|
1649
1657
|
}
|
|
1650
1658
|
|
|
1651
|
-
// Start tasks up to concurrency
|
|
1659
|
+
// Start tasks up to concurrency limits (both global and per-priority)
|
|
1652
1660
|
let hasWaitingTasks = false;
|
|
1653
1661
|
const currentTime = this.now();
|
|
1654
1662
|
const delay = this.lastCompletedPriority !== null ? (this.options.priorities[this.lastCompletedPriority]?.postDelay ?? 0) : 0;
|
|
1655
1663
|
const delayActive = delay > 0 && currentTime < this.nextAvailableTime;
|
|
1656
1664
|
|
|
1657
|
-
while (this.
|
|
1665
|
+
while (this.readyHeap.size() > 0) {
|
|
1658
1666
|
const task = this.readyHeap.peek();
|
|
1659
1667
|
if (!task) break;
|
|
1660
1668
|
|
|
@@ -1665,11 +1673,59 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1665
1673
|
continue; // Check next task
|
|
1666
1674
|
}
|
|
1667
1675
|
|
|
1668
|
-
|
|
1676
|
+
// Check concurrency limits (both global and per-priority)
|
|
1677
|
+
const canStartConcurrency = this._canStartTask(task);
|
|
1678
|
+
const canStartDelay = task.bypassDelay || !delayActive;
|
|
1679
|
+
const canStart = canStartConcurrency && canStartDelay;
|
|
1669
1680
|
|
|
1670
1681
|
if (canStart) {
|
|
1671
1682
|
this.readyHeap.pop(); // Remove it from heap
|
|
1672
1683
|
this._startTask(task);
|
|
1684
|
+
} else if (!canStartConcurrency) {
|
|
1685
|
+
// Can't start due to concurrency limits - look for other priorities that can run
|
|
1686
|
+
const heapArray = this.readyHeap.heap.slice(); // Copy heap array
|
|
1687
|
+
let foundStartableTask = false;
|
|
1688
|
+
|
|
1689
|
+
for (let i = 0; i < heapArray.length; i++) {
|
|
1690
|
+
const candidateTask = heapArray[i];
|
|
1691
|
+
|
|
1692
|
+
// Check if candidate task has expired
|
|
1693
|
+
if (candidateTask.expireAt && currentTime >= candidateTask.expireAt) {
|
|
1694
|
+
// Remove expired task and continue searching
|
|
1695
|
+
const remainingTasks = heapArray.filter((t) => t.id !== candidateTask.id);
|
|
1696
|
+
this.readyHeap.heap = [];
|
|
1697
|
+
for (const t of remainingTasks) {
|
|
1698
|
+
this.readyHeap.push(t);
|
|
1699
|
+
}
|
|
1700
|
+
this._expireTask(candidateTask);
|
|
1701
|
+
// Update heapArray for next iteration
|
|
1702
|
+
heapArray.splice(i, 1);
|
|
1703
|
+
i--; // Adjust index since we removed an item
|
|
1704
|
+
continue;
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
// Check if this task can start (considering both concurrency and delay limits)
|
|
1708
|
+
const candidateCanStartConcurrency = this._canStartTask(candidateTask);
|
|
1709
|
+
const candidateCanStartDelay = candidateTask.bypassDelay || !delayActive;
|
|
1710
|
+
|
|
1711
|
+
if (candidateCanStartConcurrency && candidateCanStartDelay) {
|
|
1712
|
+
foundStartableTask = true;
|
|
1713
|
+
// Remove the task from heap and start it
|
|
1714
|
+
const remainingTasks = heapArray.filter((t) => t.id !== candidateTask.id);
|
|
1715
|
+
this.readyHeap.heap = [];
|
|
1716
|
+
for (const t of remainingTasks) {
|
|
1717
|
+
this.readyHeap.push(t);
|
|
1718
|
+
}
|
|
1719
|
+
this._startTask(candidateTask);
|
|
1720
|
+
break;
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
if (!foundStartableTask) {
|
|
1725
|
+
// No tasks can start due to concurrency limits
|
|
1726
|
+
hasWaitingTasks = true;
|
|
1727
|
+
break;
|
|
1728
|
+
}
|
|
1673
1729
|
} else {
|
|
1674
1730
|
// Can't start due to delay - check if there are any bypass tasks in the heap
|
|
1675
1731
|
const heapArray = this.readyHeap.heap.slice(); // Copy heap array
|
|
@@ -1722,6 +1778,30 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1722
1778
|
}
|
|
1723
1779
|
}
|
|
1724
1780
|
|
|
1781
|
+
/**
|
|
1782
|
+
* Checks if a task can start based on both global and per-priority concurrency limits.
|
|
1783
|
+
* @param {Object} task - The task to check
|
|
1784
|
+
* @returns {boolean} True if the task can start, false if concurrency limits prevent it
|
|
1785
|
+
* @private
|
|
1786
|
+
*/
|
|
1787
|
+
_canStartTask(task) {
|
|
1788
|
+
// Check global concurrency limit
|
|
1789
|
+
if (this.running.size >= this.options.concurrency) {
|
|
1790
|
+
return false;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// Check per-priority concurrency limit if configured
|
|
1794
|
+
const priorityConfig = this.options.priorities[task.priority];
|
|
1795
|
+
if (priorityConfig && typeof priorityConfig.concurrency === "number") {
|
|
1796
|
+
const runningForPriority = this.runningByPriority.get(task.priority) || 0;
|
|
1797
|
+
if (runningForPriority >= priorityConfig.concurrency) {
|
|
1798
|
+
return false;
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
return true;
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1725
1805
|
/**
|
|
1726
1806
|
* Handles an expired task by removing it and calling appropriate error handling.
|
|
1727
1807
|
* @param {Object} item - The expired task item
|
|
@@ -1776,9 +1856,12 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1776
1856
|
item.status = "running";
|
|
1777
1857
|
item.startedAt = this.now();
|
|
1778
1858
|
this.running.add(item);
|
|
1779
|
-
this.emit("start", item);
|
|
1780
1859
|
|
|
1781
|
-
//
|
|
1860
|
+
// Track per-priority running count
|
|
1861
|
+
const currentPriorityCount = this.runningByPriority.get(item.priority) || 0;
|
|
1862
|
+
this.runningByPriority.set(item.priority, currentPriorityCount + 1);
|
|
1863
|
+
|
|
1864
|
+
this.emit("start", item); // Create abort controller for timeout
|
|
1782
1865
|
const abortController = new AbortController();
|
|
1783
1866
|
let timeoutId;
|
|
1784
1867
|
|
|
@@ -1880,10 +1963,14 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1880
1963
|
|
|
1881
1964
|
this.running.delete(item);
|
|
1882
1965
|
|
|
1883
|
-
//
|
|
1884
|
-
this.
|
|
1966
|
+
// Update per-priority running count
|
|
1967
|
+
const currentPriorityCount = this.runningByPriority.get(item.priority) || 0;
|
|
1968
|
+
if (currentPriorityCount > 0) {
|
|
1969
|
+
this.runningByPriority.set(item.priority, currentPriorityCount - 1);
|
|
1970
|
+
}
|
|
1885
1971
|
|
|
1886
|
-
//
|
|
1972
|
+
// Clean up all finished tasks (completed, failed, canceled)
|
|
1973
|
+
this.tasks.delete(item.id); // Update delay tracking after task completion
|
|
1887
1974
|
const completedPriority = item.priority;
|
|
1888
1975
|
const taskDelay = item.delay;
|
|
1889
1976
|
const priorityDelay = this.options.priorities[completedPriority]?.postDelay ?? 0;
|
|
@@ -2362,4 +2449,453 @@ export class HoldMyTask extends EventEmitter {
|
|
|
2362
2449
|
hasTask(id) {
|
|
2363
2450
|
return this.has(id);
|
|
2364
2451
|
}
|
|
2452
|
+
|
|
2453
|
+
// ========================================================================================
|
|
2454
|
+
// DEBUG AND INSPECTION METHODS
|
|
2455
|
+
// ========================================================================================
|
|
2456
|
+
|
|
2457
|
+
/**
|
|
2458
|
+
* Get detailed information about the current queue state for debugging.
|
|
2459
|
+
* @returns {Object} Comprehensive queue state information
|
|
2460
|
+
*/
|
|
2461
|
+
inspect() {
|
|
2462
|
+
const now = this.now();
|
|
2463
|
+
|
|
2464
|
+
// Get all tasks from different queues
|
|
2465
|
+
const pendingTasks = [...this.pendingHeap.heap].map((task) => ({
|
|
2466
|
+
id: task.id,
|
|
2467
|
+
priority: task.priority,
|
|
2468
|
+
readyAt: task.readyAt,
|
|
2469
|
+
readyIn: task.readyAt - now,
|
|
2470
|
+
coalescingKey: task.coalescingKey,
|
|
2471
|
+
metadata: task.metadata,
|
|
2472
|
+
status: "pending"
|
|
2473
|
+
}));
|
|
2474
|
+
|
|
2475
|
+
const readyTasks = [...this.readyHeap.heap].map((task) => ({
|
|
2476
|
+
id: task.id,
|
|
2477
|
+
priority: task.priority,
|
|
2478
|
+
coalescingKey: task.coalescingKey,
|
|
2479
|
+
metadata: task.metadata,
|
|
2480
|
+
status: "ready"
|
|
2481
|
+
}));
|
|
2482
|
+
|
|
2483
|
+
const runningTasks = Array.from(this.running).map((task) => ({
|
|
2484
|
+
id: task.id,
|
|
2485
|
+
priority: task.priority,
|
|
2486
|
+
coalescingKey: task.coalescingKey,
|
|
2487
|
+
metadata: task.metadata,
|
|
2488
|
+
startedAt: task.startedAt,
|
|
2489
|
+
runningFor: now - task.startedAt,
|
|
2490
|
+
status: "running"
|
|
2491
|
+
}));
|
|
2492
|
+
|
|
2493
|
+
// Get timer information
|
|
2494
|
+
const timerInfo = this.inspectTimers();
|
|
2495
|
+
|
|
2496
|
+
// Get scheduler state
|
|
2497
|
+
const schedulerState = {
|
|
2498
|
+
isActive: this.isActive,
|
|
2499
|
+
isPaused: !this.isActive,
|
|
2500
|
+
smartScheduling: this.options.smartScheduling,
|
|
2501
|
+
concurrency: this.options.concurrency,
|
|
2502
|
+
currentConcurrency: this.running.size,
|
|
2503
|
+
tick: this.options.tick,
|
|
2504
|
+
nextActiveDelay: this.nextAvailableTime > now ? this.nextAvailableTime - now : null,
|
|
2505
|
+
priorityConcurrency: {}
|
|
2506
|
+
};
|
|
2507
|
+
|
|
2508
|
+
// Add per-priority concurrency information
|
|
2509
|
+
for (const [priority, count] of this.runningByPriority.entries()) {
|
|
2510
|
+
if (count > 0) {
|
|
2511
|
+
const priorityConfig = this.options.priorities[priority];
|
|
2512
|
+
schedulerState.priorityConcurrency[priority] = {
|
|
2513
|
+
running: count,
|
|
2514
|
+
limit: priorityConfig?.concurrency || this.options.concurrency,
|
|
2515
|
+
available: (priorityConfig?.concurrency || this.options.concurrency) - count
|
|
2516
|
+
};
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
|
|
2520
|
+
// Also show configured priority limits even if nothing is running
|
|
2521
|
+
for (const [priority, config] of Object.entries(this.options.priorities)) {
|
|
2522
|
+
if (config.concurrency && !schedulerState.priorityConcurrency[priority]) {
|
|
2523
|
+
schedulerState.priorityConcurrency[priority] = {
|
|
2524
|
+
running: 0,
|
|
2525
|
+
limit: config.concurrency,
|
|
2526
|
+
available: config.concurrency
|
|
2527
|
+
};
|
|
2528
|
+
}
|
|
2529
|
+
}
|
|
2530
|
+
|
|
2531
|
+
// Get coalescing information
|
|
2532
|
+
const coalescingInfo = {};
|
|
2533
|
+
for (const [key, groups] of this.coalescingGroups.entries()) {
|
|
2534
|
+
coalescingInfo[key] = {
|
|
2535
|
+
groupCount: groups.size,
|
|
2536
|
+
totalTasks: Array.from(groups.values()).reduce((sum, group) => sum + group.tasks.size, 0),
|
|
2537
|
+
groups: Array.from(groups.entries()).map(([groupId, group]) => ({
|
|
2538
|
+
groupId,
|
|
2539
|
+
taskCount: group.tasks.size,
|
|
2540
|
+
representativeId: group.representativeId,
|
|
2541
|
+
tasks: Array.from(group.tasks.entries()).map(([_, task]) => ({
|
|
2542
|
+
id: task.id,
|
|
2543
|
+
metadata: task.metadata
|
|
2544
|
+
}))
|
|
2545
|
+
}))
|
|
2546
|
+
};
|
|
2547
|
+
}
|
|
2548
|
+
|
|
2549
|
+
return {
|
|
2550
|
+
timestamp: now,
|
|
2551
|
+
scheduler: schedulerState,
|
|
2552
|
+
timers: timerInfo,
|
|
2553
|
+
queues: {
|
|
2554
|
+
pending: {
|
|
2555
|
+
count: pendingTasks.length,
|
|
2556
|
+
tasks: pendingTasks
|
|
2557
|
+
},
|
|
2558
|
+
ready: {
|
|
2559
|
+
count: readyTasks.length,
|
|
2560
|
+
tasks: readyTasks
|
|
2561
|
+
},
|
|
2562
|
+
running: {
|
|
2563
|
+
count: runningTasks.length,
|
|
2564
|
+
tasks: runningTasks
|
|
2565
|
+
}
|
|
2566
|
+
},
|
|
2567
|
+
totals: {
|
|
2568
|
+
totalTasks: pendingTasks.length + readyTasks.length + runningTasks.length,
|
|
2569
|
+
pendingTasks: pendingTasks.length,
|
|
2570
|
+
readyTasks: readyTasks.length,
|
|
2571
|
+
runningTasks: runningTasks.length
|
|
2572
|
+
},
|
|
2573
|
+
coalescing: coalescingInfo,
|
|
2574
|
+
delays: {
|
|
2575
|
+
activeDelays: this.nextAvailableTime > now ? 1 : 0,
|
|
2576
|
+
delayDetails:
|
|
2577
|
+
this.nextAvailableTime > now
|
|
2578
|
+
? [
|
|
2579
|
+
{
|
|
2580
|
+
priority: this.lastCompletedPriority,
|
|
2581
|
+
expiresAt: this.nextAvailableTime,
|
|
2582
|
+
expiresIn: this.nextAvailableTime - now
|
|
2583
|
+
}
|
|
2584
|
+
]
|
|
2585
|
+
: []
|
|
2586
|
+
}
|
|
2587
|
+
};
|
|
2588
|
+
}
|
|
2589
|
+
|
|
2590
|
+
/**
|
|
2591
|
+
* Get information about active timers and scheduler state.
|
|
2592
|
+
* @returns {Object} Timer and scheduler information
|
|
2593
|
+
*/
|
|
2594
|
+
inspectTimers() {
|
|
2595
|
+
const now = this.now();
|
|
2596
|
+
|
|
2597
|
+
return {
|
|
2598
|
+
schedulerInterval: {
|
|
2599
|
+
id: this.schedulerInterval,
|
|
2600
|
+
active: this.schedulerInterval !== null,
|
|
2601
|
+
type: "interval"
|
|
2602
|
+
},
|
|
2603
|
+
schedulerTimeout: {
|
|
2604
|
+
id: this.schedulerTimeout,
|
|
2605
|
+
active: this.schedulerTimeout !== null,
|
|
2606
|
+
type: "timeout"
|
|
2607
|
+
},
|
|
2608
|
+
nextScheduledRun: this.nextScheduledRun,
|
|
2609
|
+
nextRunIn: this.nextScheduledRun ? this.nextScheduledRun - now : null,
|
|
2610
|
+
smartScheduling: this.options.smartScheduling,
|
|
2611
|
+
tickInterval: this.options.tick
|
|
2612
|
+
};
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
/**
|
|
2616
|
+
* Get a summary of all queued tasks by status.
|
|
2617
|
+
* @returns {Object} Task summary by status
|
|
2618
|
+
*/
|
|
2619
|
+
inspectTasks() {
|
|
2620
|
+
const now = this.now();
|
|
2621
|
+
|
|
2622
|
+
const summary = {
|
|
2623
|
+
pending: [],
|
|
2624
|
+
ready: [],
|
|
2625
|
+
running: [],
|
|
2626
|
+
byPriority: {},
|
|
2627
|
+
byCoalescingKey: {},
|
|
2628
|
+
total: 0
|
|
2629
|
+
};
|
|
2630
|
+
|
|
2631
|
+
// Process pending tasks
|
|
2632
|
+
[...this.pendingHeap.heap].forEach((task) => {
|
|
2633
|
+
const taskInfo = {
|
|
2634
|
+
id: task.id,
|
|
2635
|
+
priority: task.priority,
|
|
2636
|
+
readyAt: task.readyAt,
|
|
2637
|
+
readyIn: task.readyAt - now,
|
|
2638
|
+
coalescingKey: task.coalescingKey,
|
|
2639
|
+
metadata: task.metadata
|
|
2640
|
+
};
|
|
2641
|
+
summary.pending.push(taskInfo);
|
|
2642
|
+
summary.total++;
|
|
2643
|
+
|
|
2644
|
+
// Group by priority
|
|
2645
|
+
if (!summary.byPriority[task.priority]) {
|
|
2646
|
+
summary.byPriority[task.priority] = { pending: 0, ready: 0, running: 0 };
|
|
2647
|
+
}
|
|
2648
|
+
summary.byPriority[task.priority].pending++;
|
|
2649
|
+
|
|
2650
|
+
// Group by coalescing key
|
|
2651
|
+
if (task.coalescingKey) {
|
|
2652
|
+
if (!summary.byCoalescingKey[task.coalescingKey]) {
|
|
2653
|
+
summary.byCoalescingKey[task.coalescingKey] = { pending: 0, ready: 0, running: 0 };
|
|
2654
|
+
}
|
|
2655
|
+
summary.byCoalescingKey[task.coalescingKey].pending++;
|
|
2656
|
+
}
|
|
2657
|
+
});
|
|
2658
|
+
|
|
2659
|
+
// Process ready tasks
|
|
2660
|
+
[...this.readyHeap.heap].forEach((task) => {
|
|
2661
|
+
const taskInfo = {
|
|
2662
|
+
id: task.id,
|
|
2663
|
+
priority: task.priority,
|
|
2664
|
+
coalescingKey: task.coalescingKey,
|
|
2665
|
+
metadata: task.metadata
|
|
2666
|
+
};
|
|
2667
|
+
summary.ready.push(taskInfo);
|
|
2668
|
+
summary.total++;
|
|
2669
|
+
|
|
2670
|
+
// Group by priority
|
|
2671
|
+
if (!summary.byPriority[task.priority]) {
|
|
2672
|
+
summary.byPriority[task.priority] = { pending: 0, ready: 0, running: 0 };
|
|
2673
|
+
}
|
|
2674
|
+
summary.byPriority[task.priority].ready++;
|
|
2675
|
+
|
|
2676
|
+
// Group by coalescing key
|
|
2677
|
+
if (task.coalescingKey) {
|
|
2678
|
+
if (!summary.byCoalescingKey[task.coalescingKey]) {
|
|
2679
|
+
summary.byCoalescingKey[task.coalescingKey] = { pending: 0, ready: 0, running: 0 };
|
|
2680
|
+
}
|
|
2681
|
+
summary.byCoalescingKey[task.coalescingKey].ready++;
|
|
2682
|
+
}
|
|
2683
|
+
});
|
|
2684
|
+
|
|
2685
|
+
// Process running tasks
|
|
2686
|
+
Array.from(this.running).forEach((task) => {
|
|
2687
|
+
const taskInfo = {
|
|
2688
|
+
id: task.id,
|
|
2689
|
+
priority: task.priority,
|
|
2690
|
+
coalescingKey: task.coalescingKey,
|
|
2691
|
+
metadata: task.metadata,
|
|
2692
|
+
startedAt: task.startedAt,
|
|
2693
|
+
runningFor: now - task.startedAt
|
|
2694
|
+
};
|
|
2695
|
+
summary.running.push(taskInfo);
|
|
2696
|
+
summary.total++;
|
|
2697
|
+
|
|
2698
|
+
// Group by priority
|
|
2699
|
+
if (!summary.byPriority[task.priority]) {
|
|
2700
|
+
summary.byPriority[task.priority] = { pending: 0, ready: 0, running: 0 };
|
|
2701
|
+
}
|
|
2702
|
+
summary.byPriority[task.priority].running++;
|
|
2703
|
+
|
|
2704
|
+
// Group by coalescing key
|
|
2705
|
+
if (task.coalescingKey) {
|
|
2706
|
+
if (!summary.byCoalescingKey[task.coalescingKey]) {
|
|
2707
|
+
summary.byCoalescingKey[task.coalescingKey] = { pending: 0, ready: 0, running: 0 };
|
|
2708
|
+
}
|
|
2709
|
+
summary.byCoalescingKey[task.coalescingKey].running++;
|
|
2710
|
+
}
|
|
2711
|
+
});
|
|
2712
|
+
|
|
2713
|
+
return summary;
|
|
2714
|
+
}
|
|
2715
|
+
|
|
2716
|
+
/**
|
|
2717
|
+
* Get detailed information about the scheduler state and timing.
|
|
2718
|
+
* @returns {Object} Scheduler state information
|
|
2719
|
+
*/
|
|
2720
|
+
inspectScheduler() {
|
|
2721
|
+
const now = this.now();
|
|
2722
|
+
|
|
2723
|
+
// Calculate next run time for different scenarios
|
|
2724
|
+
let nextTaskTime = Infinity;
|
|
2725
|
+
let nextTaskInfo = null;
|
|
2726
|
+
|
|
2727
|
+
// Check pending tasks
|
|
2728
|
+
if (this.pendingHeap.size() > 0) {
|
|
2729
|
+
const nextPending = this.pendingHeap.peek();
|
|
2730
|
+
if (nextPending && nextPending.readyAt < nextTaskTime) {
|
|
2731
|
+
nextTaskTime = nextPending.readyAt;
|
|
2732
|
+
nextTaskInfo = {
|
|
2733
|
+
id: nextPending.id,
|
|
2734
|
+
priority: nextPending.priority,
|
|
2735
|
+
readyAt: nextPending.readyAt,
|
|
2736
|
+
source: "pending"
|
|
2737
|
+
};
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
// Check ready tasks
|
|
2742
|
+
if (this.readyHeap.size() > 0) {
|
|
2743
|
+
const nextReady = this.readyHeap.peek();
|
|
2744
|
+
if (nextReady) {
|
|
2745
|
+
nextTaskTime = now;
|
|
2746
|
+
nextTaskInfo = {
|
|
2747
|
+
id: nextReady.id,
|
|
2748
|
+
priority: nextReady.priority,
|
|
2749
|
+
source: "ready"
|
|
2750
|
+
};
|
|
2751
|
+
}
|
|
2752
|
+
}
|
|
2753
|
+
|
|
2754
|
+
// Check active delays
|
|
2755
|
+
let delayInfo = null;
|
|
2756
|
+
if (this.nextAvailableTime > now) {
|
|
2757
|
+
delayInfo = {
|
|
2758
|
+
priority: this.lastCompletedPriority,
|
|
2759
|
+
expiresAt: this.nextAvailableTime
|
|
2760
|
+
};
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
// Get per-priority concurrency information
|
|
2764
|
+
const priorityConcurrency = {};
|
|
2765
|
+
for (const [priority, count] of this.runningByPriority.entries()) {
|
|
2766
|
+
if (count > 0) {
|
|
2767
|
+
const priorityConfig = this.options.priorities[priority];
|
|
2768
|
+
priorityConcurrency[priority] = {
|
|
2769
|
+
running: count,
|
|
2770
|
+
limit: priorityConfig?.concurrency || this.options.concurrency,
|
|
2771
|
+
available: (priorityConfig?.concurrency || this.options.concurrency) - count
|
|
2772
|
+
};
|
|
2773
|
+
}
|
|
2774
|
+
}
|
|
2775
|
+
|
|
2776
|
+
// Also include configured priority limits even if nothing is running
|
|
2777
|
+
for (const [priority, config] of Object.entries(this.options.priorities)) {
|
|
2778
|
+
if (config.concurrency && !priorityConcurrency[priority]) {
|
|
2779
|
+
priorityConcurrency[priority] = {
|
|
2780
|
+
running: 0,
|
|
2781
|
+
limit: config.concurrency,
|
|
2782
|
+
available: config.concurrency
|
|
2783
|
+
};
|
|
2784
|
+
}
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2787
|
+
return {
|
|
2788
|
+
timestamp: now,
|
|
2789
|
+
isActive: this.isActive,
|
|
2790
|
+
smartScheduling: this.options.smartScheduling,
|
|
2791
|
+
concurrency: this.options.concurrency,
|
|
2792
|
+
currentConcurrency: this.running.size,
|
|
2793
|
+
availableSlots: this.options.concurrency - this.running.size,
|
|
2794
|
+
priorityConcurrency: priorityConcurrency,
|
|
2795
|
+
timers: {
|
|
2796
|
+
schedulerInterval: this.schedulerInterval,
|
|
2797
|
+
schedulerTimeout: this.schedulerTimeout,
|
|
2798
|
+
nextScheduledRun: this.nextScheduledRun,
|
|
2799
|
+
nextRunIn: this.nextScheduledRun ? this.nextScheduledRun - now : null
|
|
2800
|
+
},
|
|
2801
|
+
nextTask: nextTaskInfo
|
|
2802
|
+
? {
|
|
2803
|
+
...nextTaskInfo,
|
|
2804
|
+
readyIn: nextTaskTime - now,
|
|
2805
|
+
canRunNow: nextTaskTime <= now && this.running.size < this.options.concurrency
|
|
2806
|
+
}
|
|
2807
|
+
: null,
|
|
2808
|
+
delays: {
|
|
2809
|
+
hasActiveDelays: this.nextAvailableTime > now,
|
|
2810
|
+
count: this.nextAvailableTime > now ? 1 : 0,
|
|
2811
|
+
nextExpiry: delayInfo
|
|
2812
|
+
? {
|
|
2813
|
+
...delayInfo,
|
|
2814
|
+
expiresIn: delayInfo.expiresAt - now
|
|
2815
|
+
}
|
|
2816
|
+
: null
|
|
2817
|
+
},
|
|
2818
|
+
queueCounts: {
|
|
2819
|
+
pending: this.pendingHeap.size(),
|
|
2820
|
+
ready: this.readyHeap.size(),
|
|
2821
|
+
running: this.running.size,
|
|
2822
|
+
total: this.pendingHeap.size() + this.readyHeap.size() + this.running.size
|
|
2823
|
+
}
|
|
2824
|
+
};
|
|
2825
|
+
}
|
|
2826
|
+
|
|
2827
|
+
/**
|
|
2828
|
+
* Log comprehensive queue state to console for debugging.
|
|
2829
|
+
* @param {boolean} [detailed=false] - Whether to include detailed task information
|
|
2830
|
+
*/
|
|
2831
|
+
debugLog(detailed = false) {
|
|
2832
|
+
const inspection = this.inspect();
|
|
2833
|
+
|
|
2834
|
+
console.log("\n=== HoldMyTask Debug Information ===");
|
|
2835
|
+
console.log(`Timestamp: ${new Date(inspection.timestamp).toISOString()}`);
|
|
2836
|
+
console.log(`Status: ${inspection.scheduler.isActive ? "ACTIVE" : "PAUSED"}`);
|
|
2837
|
+
console.log(`Smart Scheduling: ${inspection.scheduler.smartScheduling ? "ON" : "OFF"}`);
|
|
2838
|
+
console.log(`Concurrency: ${inspection.scheduler.currentConcurrency}/${inspection.scheduler.concurrency}`);
|
|
2839
|
+
|
|
2840
|
+
console.log("\n--- Queue State ---");
|
|
2841
|
+
console.log(`Total Tasks: ${inspection.totals.totalTasks}`);
|
|
2842
|
+
console.log(` Pending: ${inspection.totals.pendingTasks}`);
|
|
2843
|
+
console.log(` Ready: ${inspection.totals.readyTasks}`);
|
|
2844
|
+
console.log(` Running: ${inspection.totals.runningTasks}`);
|
|
2845
|
+
|
|
2846
|
+
// Show per-priority concurrency information if configured
|
|
2847
|
+
if (Object.keys(inspection.scheduler.priorityConcurrency).length > 0) {
|
|
2848
|
+
console.log("\n--- Priority Concurrency ---");
|
|
2849
|
+
Object.entries(inspection.scheduler.priorityConcurrency).forEach(([priority, info]) => {
|
|
2850
|
+
console.log(` Priority ${priority}: ${info.running}/${info.limit} (${info.available} available)`);
|
|
2851
|
+
});
|
|
2852
|
+
}
|
|
2853
|
+
|
|
2854
|
+
console.log("\n--- Timer State ---");
|
|
2855
|
+
console.log(`Interval: ${inspection.timers.schedulerInterval.active ? `Active (${inspection.timers.schedulerInterval.id})` : "None"}`);
|
|
2856
|
+
console.log(`Timeout: ${inspection.timers.schedulerTimeout.active ? `Active (${inspection.timers.schedulerTimeout.id})` : "None"}`);
|
|
2857
|
+
if (inspection.timers.nextRunIn !== null) {
|
|
2858
|
+
console.log(`Next Run: ${inspection.timers.nextRunIn}ms`);
|
|
2859
|
+
}
|
|
2860
|
+
|
|
2861
|
+
if (inspection.delays.activeDelays > 0) {
|
|
2862
|
+
console.log("\n--- Active Delays ---");
|
|
2863
|
+
inspection.delays.delayDetails.forEach((delay) => {
|
|
2864
|
+
console.log(` Priority ${delay.priority}: expires in ${delay.expiresIn}ms`);
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
|
|
2868
|
+
if (Object.keys(inspection.coalescing).length > 0) {
|
|
2869
|
+
console.log("\n--- Coalescing Groups ---");
|
|
2870
|
+
Object.entries(inspection.coalescing).forEach(([key, info]) => {
|
|
2871
|
+
console.log(` ${key}: ${info.groupCount} groups, ${info.totalTasks} tasks`);
|
|
2872
|
+
});
|
|
2873
|
+
}
|
|
2874
|
+
|
|
2875
|
+
if (detailed) {
|
|
2876
|
+
console.log("\n--- Detailed Task Information ---");
|
|
2877
|
+
if (inspection.queues.pending.count > 0) {
|
|
2878
|
+
console.log("\nPending Tasks:");
|
|
2879
|
+
inspection.queues.pending.tasks.forEach((task) => {
|
|
2880
|
+
console.log(` ${task.id}: priority ${task.priority}, ready in ${task.readyIn}ms`);
|
|
2881
|
+
});
|
|
2882
|
+
}
|
|
2883
|
+
|
|
2884
|
+
if (inspection.queues.ready.count > 0) {
|
|
2885
|
+
console.log("\nReady Tasks:");
|
|
2886
|
+
inspection.queues.ready.tasks.forEach((task) => {
|
|
2887
|
+
console.log(` ${task.id}: priority ${task.priority}`);
|
|
2888
|
+
});
|
|
2889
|
+
}
|
|
2890
|
+
|
|
2891
|
+
if (inspection.queues.running.count > 0) {
|
|
2892
|
+
console.log("\nRunning Tasks:");
|
|
2893
|
+
inspection.queues.running.tasks.forEach((task) => {
|
|
2894
|
+
console.log(` ${task.id}: priority ${task.priority}, running for ${task.runningFor}ms`);
|
|
2895
|
+
});
|
|
2896
|
+
}
|
|
2897
|
+
}
|
|
2898
|
+
|
|
2899
|
+
console.log("=====================================\n");
|
|
2900
|
+
}
|
|
2365
2901
|
}
|
package/package.json
CHANGED
|
@@ -687,6 +687,31 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
687
687
|
* @returns {boolean} True if task exists, false otherwise
|
|
688
688
|
*/
|
|
689
689
|
hasTask(id: string | number): boolean;
|
|
690
|
+
/**
|
|
691
|
+
* Get detailed information about the current queue state for debugging.
|
|
692
|
+
* @returns {Object} Comprehensive queue state information
|
|
693
|
+
*/
|
|
694
|
+
inspect(): any;
|
|
695
|
+
/**
|
|
696
|
+
* Get information about active timers and scheduler state.
|
|
697
|
+
* @returns {Object} Timer and scheduler information
|
|
698
|
+
*/
|
|
699
|
+
inspectTimers(): any;
|
|
700
|
+
/**
|
|
701
|
+
* Get a summary of all queued tasks by status.
|
|
702
|
+
* @returns {Object} Task summary by status
|
|
703
|
+
*/
|
|
704
|
+
inspectTasks(): any;
|
|
705
|
+
/**
|
|
706
|
+
* Get detailed information about the scheduler state and timing.
|
|
707
|
+
* @returns {Object} Scheduler state information
|
|
708
|
+
*/
|
|
709
|
+
inspectScheduler(): any;
|
|
710
|
+
/**
|
|
711
|
+
* Log comprehensive queue state to console for debugging.
|
|
712
|
+
* @param {boolean} [detailed=false] - Whether to include detailed task information
|
|
713
|
+
*/
|
|
714
|
+
debugLog(detailed?: boolean): void;
|
|
690
715
|
}
|
|
691
716
|
import { EventEmitter } from "events";
|
|
692
717
|
import { MinHeap } from "./utils.mjs";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hold-my-task.d.mts","sourceRoot":"","sources":["../../dist/hold-my-task.mjs"],"names":[],"mappings":"AAgBA;;;;;GAKG;AACH;IACC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6DG;IAEH;;;;;;;;OAQG;IACH,yCAwCC;IAwND;;;;;;;;;OASG;IACH,uBAGC;IAnOD,0BAaC;IAED;;;;OAIG;IACH,wBAGC;IAFA,mBAAqB;IAItB;;;;;OAKG;IACH,yBAUC;IAED;;;;OAIG;IACH,0BAoKC;IA1DA,aAuBC;IAGD,qBAA+D;IAC/D,mBAIE;IAEF,kBAAwB;IACxB,qBAAsB;IACtB,eAAe;IACf,mBAAmB;IACnB,kBAAqB;IACrB,mBAAsB;IACtB,2BAAiC;IACjC,uBAA0B;IAG1B,iCAA4B;IAC5B,gCAA2B;IAC3B,yBAAyB;IAGzB,2BAAsB;IAGtB,gCAAiC;IACjC,yCAA0C;IAC1C,oBAAoB;IAsBrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACH,4CA1CW,cAAe,YAEvB;QAAgC,EAAE,GAA1B,MAAM,GAAC,MAAM;QACI,QAAQ,GAAzB,MAAM;QACW,SAAS,GAA1B,MAAM;QACW,KAAK,GAAtB,MAAM;QACgB,MAAM,GAA5B,WAAW;QACM,OAAO,GAAxB,MAAM;QACW,MAAM,GAAvB,MAAM;QACW,KAAK,GAAtB,MAAM;QACY,WAAW,GAA7B,OAAO;QACU,aAAa,GAA9B,MAAM;QACW,wBAAwB,GAAzC,MAAM;QACW,kBAAkB,GAAnC,MAAM;QACY,2BAA2B,GAA7C,OAAO;QACW,4BAA4B,GAA9C,OAAO;QACK,QAAQ,GAApB,GAAC;KACT,GAAU,kBAAc,CAgN1B;IAED;;;;;;;;;;OAUG;IACH,8BAwEC;IAED;;;;;;;;;OASG;IACH,uCAYC;IAED;;;;;;;;;;;OAWG;IACH,+BAuDC;IAED;;;;;;;;;;OAUG;IACH,8BAiCC;IAED;;;;;;;OAOG;IACH,4CA0BC;IAED;;;;;;;;OAQG;IACH,gCAgIC;IAED;;;;;;;;OAQG;IACH,8BAwCC;IAED;;;;;;;OAOG;IACH,gCAiBC;IAED;;;;;OAKG;IACH,sBAkBC;IAED;;;;;;;OAOG;IACH,
|
|
1
|
+
{"version":3,"file":"hold-my-task.d.mts","sourceRoot":"","sources":["../../dist/hold-my-task.mjs"],"names":[],"mappings":"AAgBA;;;;;GAKG;AACH;IACC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA6DG;IAEH;;;;;;;;OAQG;IACH,yCAwCC;IAwND;;;;;;;;;OASG;IACH,uBAGC;IAnOD,0BAaC;IAED;;;;OAIG;IACH,wBAGC;IAFA,mBAAqB;IAItB;;;;;OAKG;IACH,yBAUC;IAED;;;;OAIG;IACH,0BAoKC;IA1DA,aAuBC;IAGD,qBAA+D;IAC/D,mBAIE;IAEF,kBAAwB;IACxB,qBAAsB;IACtB,eAAe;IACf,mBAAmB;IACnB,kBAAqB;IACrB,mBAAsB;IACtB,2BAAiC;IACjC,uBAA0B;IAG1B,iCAA4B;IAC5B,gCAA2B;IAC3B,yBAAyB;IAGzB,2BAAsB;IAGtB,gCAAiC;IACjC,yCAA0C;IAC1C,oBAAoB;IAsBrB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACH,4CA1CW,cAAe,YAEvB;QAAgC,EAAE,GAA1B,MAAM,GAAC,MAAM;QACI,QAAQ,GAAzB,MAAM;QACW,SAAS,GAA1B,MAAM;QACW,KAAK,GAAtB,MAAM;QACgB,MAAM,GAA5B,WAAW;QACM,OAAO,GAAxB,MAAM;QACW,MAAM,GAAvB,MAAM;QACW,KAAK,GAAtB,MAAM;QACY,WAAW,GAA7B,OAAO;QACU,aAAa,GAA9B,MAAM;QACW,wBAAwB,GAAzC,MAAM;QACW,kBAAkB,GAAnC,MAAM;QACY,2BAA2B,GAA7C,OAAO;QACW,4BAA4B,GAA9C,OAAO;QACK,QAAQ,GAApB,GAAC;KACT,GAAU,kBAAc,CAgN1B;IAED;;;;;;;;;;OAUG;IACH,8BAwEC;IAED;;;;;;;;;OASG;IACH,uCAYC;IAED;;;;;;;;;;;OAWG;IACH,+BAuDC;IAED;;;;;;;;;;OAUG;IACH,8BAiCC;IAED;;;;;;;OAOG;IACH,4CA0BC;IAED;;;;;;;;OAQG;IACH,gCAgIC;IAED;;;;;;;;OAQG;IACH,8BAwCC;IAED;;;;;;;OAOG;IACH,gCAiBC;IAED;;;;;OAKG;IACH,sBAkBC;IAED;;;;;;;OAOG;IACH,0BAgHC;IAED;;;;;;;;OAQG;IACH,WAPW,MAAM,WACN,MAAM,GACJ,OAAO,CAoBnB;IAED;;;;;OAKG;IACH,eAJW,MAAM,GAAC,MAAM,WACb,MAAM,GACJ,OAAO,CAInB;IAED;;;;;;;OAOG;IACH,SALa,IAAI,CAYhB;IAED;;;;;;OAMG;IACH,UALa,IAAI,CA2BhB;IAED;;;;;;;OAOG;IACH,SALa,IAAI,CAuDhB;IAED;;;;;OAKG;IACH,QAJa,MAAM,CAMlB;IAED;;;;;OAKG;IACH,UAJa,MAAM,CAMlB;IAED;;;;;OAKG;IACH,YAJa,MAAM,CAMlB;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,kCAhBW,MAAM,YACN,MAAM,GACJ,cAAa,IAAI,CA8D7B;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,0CAhBW,MAAM,YACN,MAAM,SAmChB;IAED;;;;;;;;;;OAUG;IACH,kCAYC;IAED;;;;;;;;;;;OAWG;IACH,oCAVW,MAAM,GAAC,MAAM,GACX,MAAO,IAAI,CAqCvB;IAED;;;;;;;OAOG;IACH,WALa,IAAI,CAUhB;IAED;;;;;OAKG;IACH,OAJa,MAAM,CAMlB;IAED;;;;;OAKG;IACH,0BA2BC;IAFC,0BAA8D;IAIhE;;;;OAIG;IACH,sBA2FC;IAED;;;;;;OAMG;IACH,oBA0BC;IAED;;;;;;OAMG;IACH,mBAkJC;IAED;;;;OAIG;IACH,oBASC;IAED;;;;;;;;;;;;OAYG;IACH,6BAoDC;IAED;;;;;;;;OAQG;IACH,qBAQC;IAED;;;;;;;;OAQG;IACH,6BAmBC;IAED;;;;;;;OAOG;IACH,6BASC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,sCA7BW,MAAM,UAEd;QAAwB,cAAc,GAA9B,MAAM;QACU,QAAQ,GAAxB,MAAM;QACU,SAAS,GAAzB,MAAM;QACU,UAAU,GAA1B,MAAM;QACU,KAAK,GAArB,MAAM;QACU,KAAK,GAArB,MAAM;QACW,iBAAiB,GAAlC,OAAO;QACU,kBAAkB,GAAnC,OAAO;KACf,GAAU,IAAI,CAyDhB;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,mCAfW,MAAM,0BAmChB;IAED;;;;;;;;;;OAUG;IACH,mCAMC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,4BApBW,MAAM,UAEd;QAAwB,SAAS,GAAzB,MAAM;QACU,UAAU,GAA1B,MAAM;QACU,KAAK,GAArB,MAAM;QACU,KAAK,GAArB,MAAM;KACd,GAAU,IAAI,CA2ChB;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,4BAfW,MAAM,0BA6BhB;IAED;;;;;;;;;;OAUG;IACH,iCAMC;IAED;;;OAGG;IACH,YAFa,IAAI,CAIhB;IAED;;;;;;OAMG;IACH,4CAJW,cAAe,kBAEb,eAAQ,UAAU,CAI9B;IAED;;;;;;OAMG;IACH,uCAJW,cAAe,kBAEb,eAAQ,UAAU,CAI9B;IAED;;;;OAIG;IACH,QAHW,MAAM,GAAC,MAAM,GACX,MAAO,IAAI,CAIvB;IAED;;;;OAIG;IACH,QAHW,MAAM,GAAC,MAAM,GACX,OAAO,CAInB;IAED;;;;OAIG;IACH,YAHW,MAAM,GAAC,MAAM,GACX,MAAO,IAAI,CAIvB;IAED;;;;OAIG;IACH,YAHW,MAAM,GAAC,MAAM,GACX,OAAO,CAInB;IAMD;;;OAGG;IACH,eAuGC;IAED;;;OAGG;IACH,qBAmBC;IAED;;;OAGG;IACH,oBA+FC;IAED;;;OAGG;IACH,wBAgFC;IAED;;;OAGG;IACH,oBAFW,OAAO,QA+DjB;CACD;6BA3rF4B,QAAQ;wBACb,aAAa"}
|