@cldmv/holdmytask 1.3.0 → 1.4.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 +93 -1
- package/dist/hold-my-task.mjs +321 -14
- package/package.json +1 -1
- package/types/dist/hold-my-task.d.mts +63 -0
- package/types/dist/hold-my-task.d.mts.map +1 -1
package/README.md
CHANGED
|
@@ -304,7 +304,7 @@ Adds a task to the queue.
|
|
|
304
304
|
- `start` (number) - Milliseconds from now when the task should be ready to run (convenience for timestamp calculation)
|
|
305
305
|
- `coalescingKey` (string) - Tasks with the same coalescing key can be merged for efficiency
|
|
306
306
|
- `mustRunBy` (number) - Absolute timestamp by which the task must execute (overrides coalescing delays)
|
|
307
|
-
- `metadata` (any) - Custom metadata
|
|
307
|
+
- `metadata` (any) - Custom metadata attached to the task. Individual metadata is always directly accessible via the returned task handle
|
|
308
308
|
|
|
309
309
|
**Returns:** TaskHandle object with:
|
|
310
310
|
|
|
@@ -315,9 +315,51 @@ Adds a task to the queue.
|
|
|
315
315
|
- `finishedAt` - When task finished
|
|
316
316
|
- `result` - Task result (if completed)
|
|
317
317
|
- `error` - Task error (if failed)
|
|
318
|
+
- `metadata` - Custom metadata attached to this task
|
|
319
|
+
- `coalescingInfo` - Coalescing group information (null for non-coalesced tasks)
|
|
318
320
|
|
|
319
321
|
When `callback` is omitted, returns a Promise that resolves with the task result or rejects with an error.
|
|
320
322
|
|
|
323
|
+
#### Accessing Task Metadata
|
|
324
|
+
|
|
325
|
+
Every task handle provides direct access to its metadata and coalescing information:
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
// Create a task with metadata
|
|
329
|
+
const task = queue.enqueue(
|
|
330
|
+
async () => {
|
|
331
|
+
return "task completed";
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
metadata: { userId: 123, action: "save", document: "report.pdf" },
|
|
335
|
+
coalescingKey: "user-actions"
|
|
336
|
+
}
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
// Direct access to metadata - works for both regular and coalesced tasks
|
|
340
|
+
console.log(task.metadata);
|
|
341
|
+
// Output: { userId: 123, action: 'save', document: 'report.pdf' }
|
|
342
|
+
|
|
343
|
+
// For coalesced tasks, get information about the coalescing group
|
|
344
|
+
if (task.coalescingInfo) {
|
|
345
|
+
console.log("Coalescing key:", task.coalescingInfo.coalescingKey);
|
|
346
|
+
console.log("Group size:", task.coalescingInfo.taskCount);
|
|
347
|
+
console.log("All task metadata in group:", task.coalescingInfo.allTaskMetadata);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Works the same way with Promise API
|
|
351
|
+
const result = await task;
|
|
352
|
+
console.log("Still accessible after completion:", task.metadata);
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
**Key Points:**
|
|
356
|
+
|
|
357
|
+
- `task.metadata` - Returns the original metadata you attached to the task
|
|
358
|
+
- `task.coalescingInfo` - Returns coalescing group details (null for non-coalesced tasks)
|
|
359
|
+
- Available on both callback-style task handles and Promise-style task handles
|
|
360
|
+
- Accessible before, during, and after task execution
|
|
361
|
+
- For coalesced tasks, each task retains its individual metadata
|
|
362
|
+
|
|
321
363
|
#### Method Aliases
|
|
322
364
|
|
|
323
365
|
For compatibility with common queue system naming conventions, the following aliases are available:
|
|
@@ -388,6 +430,56 @@ queue.enqueue(task2, { id: "duplicate" }); // throws: Task ID "duplicate" alread
|
|
|
388
430
|
- `getCoalescingConfig(coalescingKey, taskOptions?)` - Get effective configuration for a specific coalescing key
|
|
389
431
|
- `getCoalescingConfigurations()` - Get all configured coalescing keys and their settings
|
|
390
432
|
|
|
433
|
+
#### Coalescing Group Inspection Methods
|
|
434
|
+
|
|
435
|
+
- `getCoalescingGroup(coalescingKey, groupId?)` - Get detailed information about coalescing groups
|
|
436
|
+
- `coalescingKey` (string) - The coalescing key to query
|
|
437
|
+
- `groupId` (string, optional) - Specific group ID, or omit to get all groups for the key
|
|
438
|
+
- Returns group info with task details and metadata
|
|
439
|
+
- `getCoalescingGroupMetadata(coalescingKey, groupId?)` - Get metadata from all tasks in coalescing groups
|
|
440
|
+
- Returns array of `{ taskId, groupId, metadata }` objects
|
|
441
|
+
- `getCoalescingGroupsSummary()` - Get summary of all active coalescing groups
|
|
442
|
+
- Returns object with `{ [coalescingKey]: { groupCount, totalTasks } }`
|
|
443
|
+
- `findCoalescingGroupByTaskId(taskId)` - Find which coalescing group contains a specific task
|
|
444
|
+
- Returns group info including the task's metadata and other group members
|
|
445
|
+
|
|
446
|
+
**Note:** For most use cases, the direct `task.metadata` and `task.coalescingInfo` properties provide easier access to task information. The coalescing group inspection methods are useful for bulk operations, monitoring, debugging, or when you need to query groups by coalescing key without having individual task references.
|
|
447
|
+
|
|
448
|
+
```javascript
|
|
449
|
+
// Example: Direct access to task metadata and coalescing info
|
|
450
|
+
const task1 = queue.enqueue(async () => "result", {
|
|
451
|
+
coalescingKey: "user-action",
|
|
452
|
+
metadata: { userId: 123, action: "save" }
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
const task2 = queue.enqueue(async () => "result", {
|
|
456
|
+
coalescingKey: "user-action",
|
|
457
|
+
metadata: { userId: 456, action: "delete" }
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
// Direct access - no additional API calls needed!
|
|
461
|
+
console.log("Task 1 metadata:", task1.metadata); // { userId: 123, action: 'save' }
|
|
462
|
+
console.log("Task 2 metadata:", task2.metadata); // { userId: 456, action: 'delete' }
|
|
463
|
+
|
|
464
|
+
// Access coalescing information directly from the task
|
|
465
|
+
console.log("Coalescing info:", task1.coalescingInfo);
|
|
466
|
+
// {
|
|
467
|
+
// coalescingKey: 'user-action',
|
|
468
|
+
// groupId: '1',
|
|
469
|
+
// representativeId: '3',
|
|
470
|
+
// taskCount: 2,
|
|
471
|
+
// allTaskMetadata: [
|
|
472
|
+
// { id: '1', metadata: { userId: 123, action: 'save' } },
|
|
473
|
+
// { id: '2', metadata: { userId: 456, action: 'delete' } }
|
|
474
|
+
// ]
|
|
475
|
+
// }
|
|
476
|
+
|
|
477
|
+
// Advanced: Use inspection methods for bulk operations
|
|
478
|
+
const metadata = queue.getCoalescingGroupMetadata("user-action");
|
|
479
|
+
const groupInfo = queue.getCoalescingGroup("user-action");
|
|
480
|
+
const taskGroup = queue.findCoalescingGroupByTaskId(task1.id);
|
|
481
|
+
```
|
|
482
|
+
|
|
391
483
|
### Events
|
|
392
484
|
|
|
393
485
|
```javascript
|
package/dist/hold-my-task.mjs
CHANGED
|
@@ -519,6 +519,12 @@ export class HoldMyTask extends EventEmitter {
|
|
|
519
519
|
},
|
|
520
520
|
get error() {
|
|
521
521
|
return tasks.get(id)?.error;
|
|
522
|
+
},
|
|
523
|
+
get metadata() {
|
|
524
|
+
return tasks.get(id)?.metadata;
|
|
525
|
+
},
|
|
526
|
+
get coalescingInfo() {
|
|
527
|
+
return null; // Regular tasks don't have coalescing info
|
|
522
528
|
}
|
|
523
529
|
};
|
|
524
530
|
|
|
@@ -529,15 +535,58 @@ export class HoldMyTask extends EventEmitter {
|
|
|
529
535
|
item.reject = reject;
|
|
530
536
|
});
|
|
531
537
|
|
|
532
|
-
//
|
|
533
|
-
|
|
538
|
+
// Store promise reference on item so we can update properties when task completes
|
|
539
|
+
item.promise = promise;
|
|
540
|
+
|
|
541
|
+
// Attach task handle properties to the promise with cached fallback getters
|
|
534
542
|
Object.defineProperty(promise, "id", { value: id, enumerable: true });
|
|
535
543
|
Object.defineProperty(promise, "cancel", { value: (reason) => this.cancel(id, reason), enumerable: true });
|
|
536
|
-
Object.defineProperty(promise, "status", {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
544
|
+
Object.defineProperty(promise, "status", {
|
|
545
|
+
value: () => {
|
|
546
|
+
const currentItem = this.tasks.get(id);
|
|
547
|
+
return currentItem ? currentItem.status : (promise._status ?? "canceled");
|
|
548
|
+
},
|
|
549
|
+
enumerable: true
|
|
550
|
+
});
|
|
551
|
+
Object.defineProperty(promise, "startedAt", {
|
|
552
|
+
get: () => {
|
|
553
|
+
const currentItem = this.tasks.get(id);
|
|
554
|
+
return currentItem ? currentItem.startedAt : promise._startedAt;
|
|
555
|
+
},
|
|
556
|
+
enumerable: true
|
|
557
|
+
});
|
|
558
|
+
Object.defineProperty(promise, "finishedAt", {
|
|
559
|
+
get: () => {
|
|
560
|
+
const currentItem = this.tasks.get(id);
|
|
561
|
+
return currentItem ? currentItem.finishedAt : promise._finishedAt;
|
|
562
|
+
},
|
|
563
|
+
enumerable: true
|
|
564
|
+
});
|
|
565
|
+
Object.defineProperty(promise, "result", {
|
|
566
|
+
get: () => {
|
|
567
|
+
const currentItem = this.tasks.get(id);
|
|
568
|
+
return currentItem ? currentItem.result : promise._result;
|
|
569
|
+
},
|
|
570
|
+
enumerable: true
|
|
571
|
+
});
|
|
572
|
+
Object.defineProperty(promise, "error", {
|
|
573
|
+
get: () => {
|
|
574
|
+
const currentItem = this.tasks.get(id);
|
|
575
|
+
return currentItem ? currentItem.error : promise._error;
|
|
576
|
+
},
|
|
577
|
+
enumerable: true
|
|
578
|
+
});
|
|
579
|
+
Object.defineProperty(promise, "metadata", {
|
|
580
|
+
get: () => {
|
|
581
|
+
const currentItem = this.tasks.get(id);
|
|
582
|
+
return currentItem ? currentItem.metadata : undefined;
|
|
583
|
+
},
|
|
584
|
+
enumerable: true
|
|
585
|
+
});
|
|
586
|
+
Object.defineProperty(promise, "coalescingInfo", {
|
|
587
|
+
get: () => null, // Regular tasks don't have coalescing info
|
|
588
|
+
enumerable: true
|
|
589
|
+
});
|
|
541
590
|
|
|
542
591
|
return promise;
|
|
543
592
|
}
|
|
@@ -1078,6 +1127,27 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1078
1127
|
},
|
|
1079
1128
|
get error() {
|
|
1080
1129
|
return item.error;
|
|
1130
|
+
},
|
|
1131
|
+
get metadata() {
|
|
1132
|
+
// For coalescing tasks, get metadata from coalescing group
|
|
1133
|
+
if (item.coalescingKey) {
|
|
1134
|
+
const groupInfo = this.findCoalescingGroupByTaskId(item.id);
|
|
1135
|
+
return groupInfo ? groupInfo.task.metadata : item.metadata;
|
|
1136
|
+
}
|
|
1137
|
+
return item.metadata;
|
|
1138
|
+
},
|
|
1139
|
+
get coalescingInfo() {
|
|
1140
|
+
if (!item.coalescingKey) {
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1143
|
+
const groupInfo = this.findCoalescingGroupByTaskId(item.id);
|
|
1144
|
+
return groupInfo ? {
|
|
1145
|
+
coalescingKey: groupInfo.coalescingKey,
|
|
1146
|
+
groupId: groupInfo.groupId,
|
|
1147
|
+
representativeId: groupInfo.representativeId,
|
|
1148
|
+
taskCount: groupInfo.groupTasks.length,
|
|
1149
|
+
allTaskMetadata: groupInfo.groupTasks.map(t => ({ id: t.id, metadata: t.metadata }))
|
|
1150
|
+
} : null;
|
|
1081
1151
|
}
|
|
1082
1152
|
};
|
|
1083
1153
|
|
|
@@ -1088,14 +1158,56 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1088
1158
|
item.reject = reject;
|
|
1089
1159
|
});
|
|
1090
1160
|
|
|
1161
|
+
// Store promise reference on item so we can update properties when task completes
|
|
1162
|
+
item.promise = promise;
|
|
1163
|
+
|
|
1091
1164
|
// Attach task handle properties to the promise
|
|
1092
1165
|
Object.defineProperty(promise, "id", { value: item.id, enumerable: true });
|
|
1093
1166
|
Object.defineProperty(promise, "cancel", { value: taskHandle.cancel, enumerable: true });
|
|
1094
1167
|
Object.defineProperty(promise, "status", { value: taskHandle.status, enumerable: true });
|
|
1095
|
-
Object.defineProperty(promise, "startedAt", {
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1168
|
+
Object.defineProperty(promise, "startedAt", {
|
|
1169
|
+
get: () => (promise._startedAt !== undefined ? promise._startedAt : item.startedAt),
|
|
1170
|
+
enumerable: true
|
|
1171
|
+
});
|
|
1172
|
+
Object.defineProperty(promise, "finishedAt", {
|
|
1173
|
+
get: () => (promise._finishedAt !== undefined ? promise._finishedAt : item.finishedAt),
|
|
1174
|
+
enumerable: true
|
|
1175
|
+
});
|
|
1176
|
+
Object.defineProperty(promise, "result", {
|
|
1177
|
+
get: () => (promise._result !== undefined ? promise._result : item.result),
|
|
1178
|
+
enumerable: true
|
|
1179
|
+
});
|
|
1180
|
+
Object.defineProperty(promise, "error", {
|
|
1181
|
+
get: () => (promise._error !== undefined ? promise._error : item.error),
|
|
1182
|
+
enumerable: true
|
|
1183
|
+
});
|
|
1184
|
+
Object.defineProperty(promise, "metadata", {
|
|
1185
|
+
get: () => {
|
|
1186
|
+
// For coalescing tasks, get metadata from coalescing group
|
|
1187
|
+
if (item.coalescingKey) {
|
|
1188
|
+
const groupInfo = this.findCoalescingGroupByTaskId(item.id);
|
|
1189
|
+
return groupInfo ? groupInfo.task.metadata : item.metadata;
|
|
1190
|
+
}
|
|
1191
|
+
return item.metadata;
|
|
1192
|
+
},
|
|
1193
|
+
enumerable: true
|
|
1194
|
+
});
|
|
1195
|
+
Object.defineProperty(promise, "coalescingInfo", {
|
|
1196
|
+
get: () => {
|
|
1197
|
+
if (!item.coalescingKey) {
|
|
1198
|
+
return null;
|
|
1199
|
+
}
|
|
1200
|
+
const groupInfo = this.findCoalescingGroupByTaskId(item.id);
|
|
1201
|
+
return groupInfo ? {
|
|
1202
|
+
coalescingKey: groupInfo.coalescingKey,
|
|
1203
|
+
groupId: groupInfo.groupId,
|
|
1204
|
+
representativeId: groupInfo.representativeId,
|
|
1205
|
+
taskCount: groupInfo.groupTasks.length,
|
|
1206
|
+
allTaskMetadata: groupInfo.groupTasks.map(t => ({ id: t.id, metadata: t.metadata }))
|
|
1207
|
+
} : null;
|
|
1208
|
+
},
|
|
1209
|
+
enumerable: true
|
|
1210
|
+
});
|
|
1099
1211
|
|
|
1100
1212
|
return promise;
|
|
1101
1213
|
}
|
|
@@ -1277,6 +1389,181 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1277
1389
|
return this.running.size;
|
|
1278
1390
|
}
|
|
1279
1391
|
|
|
1392
|
+
/**
|
|
1393
|
+
* Gets information about a coalescing group by key and group ID.
|
|
1394
|
+
* @param {string} coalescingKey - The coalescing key
|
|
1395
|
+
* @param {string} [groupId] - Optional group ID. If omitted, returns all groups for the key
|
|
1396
|
+
* @returns {Object|Array|null} Group info object, array of groups, or null if not found
|
|
1397
|
+
* @example
|
|
1398
|
+
* // Get all groups for a coalescing key
|
|
1399
|
+
* const groups = queue.getCoalescingGroup('ui.update');
|
|
1400
|
+
*
|
|
1401
|
+
* // Get specific group by ID
|
|
1402
|
+
* const group = queue.getCoalescingGroup('ui.update', '1');
|
|
1403
|
+
* console.log(group.tasks.size); // Number of tasks in group
|
|
1404
|
+
*
|
|
1405
|
+
* // Access individual task metadata
|
|
1406
|
+
* for (const [taskId, task] of group.tasks) {
|
|
1407
|
+
* console.log(`Task ${taskId}:`, task.metadata);
|
|
1408
|
+
* }
|
|
1409
|
+
*/
|
|
1410
|
+
getCoalescingGroup(coalescingKey, groupId = null) {
|
|
1411
|
+
const groups = this.coalescingGroups.get(coalescingKey);
|
|
1412
|
+
if (!groups || groups.length === 0) {
|
|
1413
|
+
return null;
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
if (groupId === null) {
|
|
1417
|
+
// Return all groups for the key
|
|
1418
|
+
return groups.map(group => ({
|
|
1419
|
+
coalescingKey: group.coalescingKey,
|
|
1420
|
+
groupId: group.groupId,
|
|
1421
|
+
representativeId: group.representativeId,
|
|
1422
|
+
windowEnd: group.windowEnd,
|
|
1423
|
+
mustRunBy: group.mustRunBy,
|
|
1424
|
+
taskCount: group.tasks.size,
|
|
1425
|
+
tasks: Array.from(group.tasks.entries()).map(([id, task]) => ({
|
|
1426
|
+
id,
|
|
1427
|
+
metadata: task.metadata,
|
|
1428
|
+
status: task.status,
|
|
1429
|
+
priority: task.priority,
|
|
1430
|
+
readyAt: task.readyAt,
|
|
1431
|
+
coalescingKey: task.coalescingKey
|
|
1432
|
+
}))
|
|
1433
|
+
}));
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
// Find specific group by ID
|
|
1437
|
+
const group = groups.find(g => g.groupId === groupId);
|
|
1438
|
+
if (!group) {
|
|
1439
|
+
return null;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
return {
|
|
1443
|
+
coalescingKey: group.coalescingKey,
|
|
1444
|
+
groupId: group.groupId,
|
|
1445
|
+
representativeId: group.representativeId,
|
|
1446
|
+
windowEnd: group.windowEnd,
|
|
1447
|
+
mustRunBy: group.mustRunBy,
|
|
1448
|
+
taskCount: group.tasks.size,
|
|
1449
|
+
tasks: Array.from(group.tasks.entries()).map(([id, task]) => ({
|
|
1450
|
+
id,
|
|
1451
|
+
metadata: task.metadata,
|
|
1452
|
+
status: task.status,
|
|
1453
|
+
priority: task.priority,
|
|
1454
|
+
readyAt: task.readyAt,
|
|
1455
|
+
coalescingKey: task.coalescingKey
|
|
1456
|
+
}))
|
|
1457
|
+
};
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
/**
|
|
1461
|
+
* Gets metadata for all tasks in a coalescing group.
|
|
1462
|
+
* @param {string} coalescingKey - The coalescing key
|
|
1463
|
+
* @param {string} [groupId] - Optional group ID. If omitted, returns metadata from all groups for the key
|
|
1464
|
+
* @returns {Array} Array of metadata objects with task IDs
|
|
1465
|
+
* @example
|
|
1466
|
+
* // Get metadata from all groups for a key
|
|
1467
|
+
* const allMetadata = queue.getCoalescingGroupMetadata('ui.update');
|
|
1468
|
+
*
|
|
1469
|
+
* // Get metadata from specific group
|
|
1470
|
+
* const groupMetadata = queue.getCoalescingGroupMetadata('ui.update', '1');
|
|
1471
|
+
*
|
|
1472
|
+
* // Example output:
|
|
1473
|
+
* // [
|
|
1474
|
+
* // { taskId: '123', metadata: { userId: 100, action: 'save' } },
|
|
1475
|
+
* // { taskId: '124', metadata: { userId: 200, action: 'delete' } }
|
|
1476
|
+
* // ]
|
|
1477
|
+
*/
|
|
1478
|
+
getCoalescingGroupMetadata(coalescingKey, groupId = null) {
|
|
1479
|
+
const groups = this.coalescingGroups.get(coalescingKey);
|
|
1480
|
+
if (!groups || groups.length === 0) {
|
|
1481
|
+
return [];
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
const targetGroups = groupId ? groups.filter(g => g.groupId === groupId) : groups;
|
|
1485
|
+
const metadata = [];
|
|
1486
|
+
|
|
1487
|
+
for (const group of targetGroups) {
|
|
1488
|
+
for (const [taskId, task] of group.tasks) {
|
|
1489
|
+
metadata.push({
|
|
1490
|
+
taskId,
|
|
1491
|
+
groupId: group.groupId,
|
|
1492
|
+
metadata: task.metadata
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
return metadata;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
/**
|
|
1501
|
+
* Gets a summary of all active coalescing groups.
|
|
1502
|
+
* @returns {Object} Summary object with coalescing key stats
|
|
1503
|
+
* @example
|
|
1504
|
+
* const summary = queue.getCoalescingGroupsSummary();
|
|
1505
|
+
* console.log(summary);
|
|
1506
|
+
* // {
|
|
1507
|
+
* // 'ui.update': { groupCount: 2, totalTasks: 5 },
|
|
1508
|
+
* // 'api.batch': { groupCount: 1, totalTasks: 3 }
|
|
1509
|
+
* // }
|
|
1510
|
+
*/
|
|
1511
|
+
getCoalescingGroupsSummary() {
|
|
1512
|
+
const summary = {};
|
|
1513
|
+
|
|
1514
|
+
for (const [coalescingKey, groups] of this.coalescingGroups) {
|
|
1515
|
+
const totalTasks = groups.reduce((sum, group) => sum + group.tasks.size, 0);
|
|
1516
|
+
summary[coalescingKey] = {
|
|
1517
|
+
groupCount: groups.length,
|
|
1518
|
+
totalTasks
|
|
1519
|
+
};
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
return summary;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
/**
|
|
1526
|
+
* Finds the coalescing group that contains a specific task ID.
|
|
1527
|
+
* @param {string|number} taskId - The task ID to search for
|
|
1528
|
+
* @returns {Object|null} Group information including the task's metadata, or null if not found
|
|
1529
|
+
* @example
|
|
1530
|
+
* const groupInfo = queue.findCoalescingGroupByTaskId('123');
|
|
1531
|
+
* if (groupInfo) {
|
|
1532
|
+
* console.log('Task is in group:', groupInfo.groupId);
|
|
1533
|
+
* console.log('Task metadata:', groupInfo.task.metadata);
|
|
1534
|
+
* console.log('Other tasks in group:', groupInfo.groupTasks.length);
|
|
1535
|
+
* }
|
|
1536
|
+
*/
|
|
1537
|
+
findCoalescingGroupByTaskId(taskId) {
|
|
1538
|
+
for (const [coalescingKey, groups] of this.coalescingGroups) {
|
|
1539
|
+
for (const group of groups) {
|
|
1540
|
+
const task = group.tasks.get(taskId);
|
|
1541
|
+
if (task) {
|
|
1542
|
+
return {
|
|
1543
|
+
coalescingKey,
|
|
1544
|
+
groupId: group.groupId,
|
|
1545
|
+
representativeId: group.representativeId,
|
|
1546
|
+
windowEnd: group.windowEnd,
|
|
1547
|
+
mustRunBy: group.mustRunBy,
|
|
1548
|
+
task: {
|
|
1549
|
+
id: taskId,
|
|
1550
|
+
metadata: task.metadata,
|
|
1551
|
+
status: task.status,
|
|
1552
|
+
priority: task.priority,
|
|
1553
|
+
readyAt: task.readyAt
|
|
1554
|
+
},
|
|
1555
|
+
groupTasks: Array.from(group.tasks.entries()).map(([id, t]) => ({
|
|
1556
|
+
id,
|
|
1557
|
+
metadata: t.metadata,
|
|
1558
|
+
status: t.status
|
|
1559
|
+
}))
|
|
1560
|
+
};
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return null;
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1280
1567
|
/**
|
|
1281
1568
|
* Destroys the queue, canceling all tasks and stopping the scheduler.
|
|
1282
1569
|
* Once destroyed, the queue cannot be reused.
|
|
@@ -1523,6 +1810,16 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1523
1810
|
|
|
1524
1811
|
this.emit("success", item);
|
|
1525
1812
|
|
|
1813
|
+
// Store final values on promise handle before resolving (for promise API)
|
|
1814
|
+
if (item.promise) {
|
|
1815
|
+
// Store final values so getters can access them after task deletion
|
|
1816
|
+
item.promise._result = item.result;
|
|
1817
|
+
item.promise._error = item.error;
|
|
1818
|
+
item.promise._startedAt = item.startedAt;
|
|
1819
|
+
item.promise._finishedAt = item.finishedAt;
|
|
1820
|
+
item.promise._status = item.status;
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1526
1823
|
// Call completion callback or resolve promise
|
|
1527
1824
|
if (item.callback) {
|
|
1528
1825
|
try {
|
|
@@ -1548,6 +1845,16 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1548
1845
|
this.emit("error", item);
|
|
1549
1846
|
}
|
|
1550
1847
|
|
|
1848
|
+
// Store final values on promise handle before rejecting (for promise API)
|
|
1849
|
+
if (item.promise) {
|
|
1850
|
+
// Store final values so getters can access them after task deletion
|
|
1851
|
+
item.promise._result = item.result;
|
|
1852
|
+
item.promise._error = item.error;
|
|
1853
|
+
item.promise._startedAt = item.startedAt;
|
|
1854
|
+
item.promise._finishedAt = item.finishedAt;
|
|
1855
|
+
item.promise._status = item.status;
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1551
1858
|
// Call completion callback or reject promise
|
|
1552
1859
|
if (item.callback) {
|
|
1553
1860
|
try {
|
|
@@ -1572,9 +1879,9 @@ export class HoldMyTask extends EventEmitter {
|
|
|
1572
1879
|
}
|
|
1573
1880
|
|
|
1574
1881
|
this.running.delete(item);
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1882
|
+
|
|
1883
|
+
// Clean up all finished tasks (completed, failed, canceled)
|
|
1884
|
+
this.tasks.delete(item.id);
|
|
1578
1885
|
|
|
1579
1886
|
// Update delay tracking after task completion
|
|
1580
1887
|
const completedPriority = item.priority;
|
package/package.json
CHANGED
|
@@ -355,6 +355,69 @@ export class HoldMyTask extends EventEmitter<[never]> {
|
|
|
355
355
|
* const runningTasks = queue.inflight(); // 2
|
|
356
356
|
*/
|
|
357
357
|
inflight(): number;
|
|
358
|
+
/**
|
|
359
|
+
* Gets information about a coalescing group by key and group ID.
|
|
360
|
+
* @param {string} coalescingKey - The coalescing key
|
|
361
|
+
* @param {string} [groupId] - Optional group ID. If omitted, returns all groups for the key
|
|
362
|
+
* @returns {Object|Array|null} Group info object, array of groups, or null if not found
|
|
363
|
+
* @example
|
|
364
|
+
* // Get all groups for a coalescing key
|
|
365
|
+
* const groups = queue.getCoalescingGroup('ui.update');
|
|
366
|
+
*
|
|
367
|
+
* // Get specific group by ID
|
|
368
|
+
* const group = queue.getCoalescingGroup('ui.update', '1');
|
|
369
|
+
* console.log(group.tasks.size); // Number of tasks in group
|
|
370
|
+
*
|
|
371
|
+
* // Access individual task metadata
|
|
372
|
+
* for (const [taskId, task] of group.tasks) {
|
|
373
|
+
* console.log(`Task ${taskId}:`, task.metadata);
|
|
374
|
+
* }
|
|
375
|
+
*/
|
|
376
|
+
getCoalescingGroup(coalescingKey: string, groupId?: string): any | any[] | null;
|
|
377
|
+
/**
|
|
378
|
+
* Gets metadata for all tasks in a coalescing group.
|
|
379
|
+
* @param {string} coalescingKey - The coalescing key
|
|
380
|
+
* @param {string} [groupId] - Optional group ID. If omitted, returns metadata from all groups for the key
|
|
381
|
+
* @returns {Array} Array of metadata objects with task IDs
|
|
382
|
+
* @example
|
|
383
|
+
* // Get metadata from all groups for a key
|
|
384
|
+
* const allMetadata = queue.getCoalescingGroupMetadata('ui.update');
|
|
385
|
+
*
|
|
386
|
+
* // Get metadata from specific group
|
|
387
|
+
* const groupMetadata = queue.getCoalescingGroupMetadata('ui.update', '1');
|
|
388
|
+
*
|
|
389
|
+
* // Example output:
|
|
390
|
+
* // [
|
|
391
|
+
* // { taskId: '123', metadata: { userId: 100, action: 'save' } },
|
|
392
|
+
* // { taskId: '124', metadata: { userId: 200, action: 'delete' } }
|
|
393
|
+
* // ]
|
|
394
|
+
*/
|
|
395
|
+
getCoalescingGroupMetadata(coalescingKey: string, groupId?: string): any[];
|
|
396
|
+
/**
|
|
397
|
+
* Gets a summary of all active coalescing groups.
|
|
398
|
+
* @returns {Object} Summary object with coalescing key stats
|
|
399
|
+
* @example
|
|
400
|
+
* const summary = queue.getCoalescingGroupsSummary();
|
|
401
|
+
* console.log(summary);
|
|
402
|
+
* // {
|
|
403
|
+
* // 'ui.update': { groupCount: 2, totalTasks: 5 },
|
|
404
|
+
* // 'api.batch': { groupCount: 1, totalTasks: 3 }
|
|
405
|
+
* // }
|
|
406
|
+
*/
|
|
407
|
+
getCoalescingGroupsSummary(): any;
|
|
408
|
+
/**
|
|
409
|
+
* Finds the coalescing group that contains a specific task ID.
|
|
410
|
+
* @param {string|number} taskId - The task ID to search for
|
|
411
|
+
* @returns {Object|null} Group information including the task's metadata, or null if not found
|
|
412
|
+
* @example
|
|
413
|
+
* const groupInfo = queue.findCoalescingGroupByTaskId('123');
|
|
414
|
+
* if (groupInfo) {
|
|
415
|
+
* console.log('Task is in group:', groupInfo.groupId);
|
|
416
|
+
* console.log('Task metadata:', groupInfo.task.metadata);
|
|
417
|
+
* console.log('Other tasks in group:', groupInfo.groupTasks.length);
|
|
418
|
+
* }
|
|
419
|
+
*/
|
|
420
|
+
findCoalescingGroupByTaskId(taskId: string | number): any | null;
|
|
358
421
|
/**
|
|
359
422
|
* Destroys the queue, canceling all tasks and stopping the scheduler.
|
|
360
423
|
* Once destroyed, the queue cannot be reused.
|
|
@@ -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,wBAG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