groupmq-plus 1.1.1 → 1.1.3

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/dist/index.d.cts CHANGED
@@ -37,6 +37,7 @@ declare class Job<T = any> {
37
37
  readonly timestamp: number;
38
38
  readonly orderMs?: number;
39
39
  readonly status: Status$1 | 'unknown';
40
+ readonly parentId?: string;
40
41
  constructor(args: {
41
42
  queue: Queue<T>;
42
43
  id: string;
@@ -56,6 +57,7 @@ declare class Job<T = any> {
56
57
  timestamp: number;
57
58
  orderMs?: number;
58
59
  status?: Status$1 | 'unknown';
60
+ parentId?: string;
59
61
  });
60
62
  getState(): Promise<Status$1 | 'stuck' | 'waiting-children' | 'prioritized' | 'unknown'>;
61
63
  toJSON(): {
@@ -89,6 +91,26 @@ declare class Job<T = any> {
89
91
  * @param timeoutMs Optional timeout in milliseconds (0 = no timeout)
90
92
  */
91
93
  waitUntilFinished(timeoutMs?: number): Promise<unknown>;
94
+ /**
95
+ * Get all child jobs of this job (if it's a parent in a flow).
96
+ * @returns Array of child Job instances
97
+ */
98
+ getChildren(): Promise<Job<any>[]>;
99
+ /**
100
+ * Get the return values of all child jobs in a flow.
101
+ * @returns Object mapping child job IDs to their return values
102
+ */
103
+ getChildrenValues(): Promise<Record<string, any>>;
104
+ /**
105
+ * Get the number of remaining child jobs that haven't completed yet.
106
+ * @returns Number of remaining dependencies, or null if not a parent job
107
+ */
108
+ getDependenciesCount(): Promise<number | null>;
109
+ /**
110
+ * Get the parent job of this job (if it's a child in a flow).
111
+ * @returns Parent Job instance, or undefined if no parent or parent was deleted
112
+ */
113
+ getParent(): Promise<Job<any> | undefined>;
92
114
  static fromReserved<T = any>(queue: Queue<T>, reserved: ReservedJob<T>, meta?: {
93
115
  processedOn?: number;
94
116
  finishedOn?: number;
@@ -537,6 +559,18 @@ declare class Queue<T = any> {
537
559
  * @returns An object mapping child job IDs to their results
538
560
  */
539
561
  getFlowResults(parentId: string): Promise<Record<string, any>>;
562
+ /**
563
+ * Gets all child job IDs for a parent job in a flow.
564
+ * @param parentId The ID of the parent job
565
+ * @returns An array of child job IDs
566
+ */
567
+ getFlowChildrenIds(parentId: string): Promise<string[]>;
568
+ /**
569
+ * Gets all child jobs for a parent job in a flow.
570
+ * @param parentId The ID of the parent job
571
+ * @returns An array of Job instances for all children
572
+ */
573
+ getFlowChildren(parentId: string): Promise<Job<any>[]>;
540
574
  private addSingle;
541
575
  private flushBatch;
542
576
  reserve(): Promise<ReservedJob<T> | null>;
@@ -967,10 +1001,10 @@ type WorkerOptions<T> = {
967
1001
  name?: string;
968
1002
  /**
969
1003
  * The function that processes jobs. Must be async and handle job failures gracefully.
970
- * @param job The reserved job to process
1004
+ * @param job The Job instance to process, with access to all Job methods
971
1005
  * @returns Promise that resolves when job is complete
972
1006
  */
973
- handler: (job: ReservedJob<T>) => Promise<unknown>;
1007
+ handler: (job: Job<T>) => Promise<unknown>;
974
1008
  /**
975
1009
  * Heartbeat interval in milliseconds to keep jobs alive during processing.
976
1010
  * Prevents jobs from timing out during long-running operations.
@@ -987,9 +1021,9 @@ type WorkerOptions<T> = {
987
1021
  /**
988
1022
  * Error handler called when job processing fails or worker encounters errors
989
1023
  * @param err The error that occurred
990
- * @param job The job that failed (if applicable)
1024
+ * @param job The Job instance that failed (if applicable)
991
1025
  */
992
- onError?: (err: unknown, job?: ReservedJob<T>) => void;
1026
+ onError?: (err: unknown, job?: Job<T>) => void;
993
1027
  /**
994
1028
  * Maximum number of retry attempts for failed jobs at the worker level.
995
1029
  * This overrides the queue's default maxAttempts setting.
@@ -1256,14 +1290,14 @@ declare class _Worker<T = any> extends TypedEventEmitter<WorkerEvents<T>> {
1256
1290
  * For concurrency > 1, returns the oldest job in progress
1257
1291
  */
1258
1292
  getCurrentJob(): {
1259
- job: ReservedJob<T>;
1293
+ job: Job<T>;
1260
1294
  processingTimeMs: number;
1261
1295
  } | null;
1262
1296
  /**
1263
1297
  * Get information about all currently processing jobs
1264
1298
  */
1265
1299
  getCurrentJobs(): Array<{
1266
- job: ReservedJob<T>;
1300
+ job: Job<T>;
1267
1301
  processingTimeMs: number;
1268
1302
  }>;
1269
1303
  /**
@@ -1315,5 +1349,127 @@ declare function getWorkersStatus<T = any>(workers: Worker<T>[]): {
1315
1349
  }>;
1316
1350
  };
1317
1351
  //#endregion
1318
- export { AddOptions, BackoffStrategy, BullBoardGroupMQAdapter, FlowJob, FlowOptions, GroupMQBullBoardAdapterOptions, Job, Queue, QueueOptions, RepeatOptions, ReservedJob, UnrecoverableError, Worker, WorkerEvents, WorkerOptions, getWorkersStatus, waitForQueueToEmpty };
1352
+ //#region src/strategies/priority-strategy.d.ts
1353
+ type GroupConfig = {
1354
+ priority: number;
1355
+ concurrency: number;
1356
+ };
1357
+ /**
1358
+ * 自定义优先级计算函数
1359
+ * @param groupId 组 ID
1360
+ * @param config 从 Redis 读取的组配置
1361
+ * @returns 优先级数值(数字越大优先级越高)
1362
+ */
1363
+ type OnGetPriority = (groupId: string, config: GroupConfig) => number | Promise<number>;
1364
+ /**
1365
+ * 严格优先级算法
1366
+ * 总是选择优先级最高的组,可能导致低优先级组饥饿
1367
+ */
1368
+ type StrictAlgorithmConfig = {
1369
+ type: 'strict';
1370
+ };
1371
+ /**
1372
+ * 加权随机算法(默认)
1373
+ * 根据优先级计算概率选择,确保低优先级组也有机会被执行
1374
+ */
1375
+ type WeightedRandomAlgorithmConfig = {
1376
+ type: 'weighted-random';
1377
+ /**
1378
+ * 最低优先级组的保底权重比例,默认 0.1(10%)
1379
+ * 例如:VIP 优先级 100,普通用户优先级 1
1380
+ * 普通用户的权重会被提升到 100 * 0.1 = 10,而不是 1
1381
+ * 这样普通用户大约有 10/(100+10) ≈ 9% 的概率被选中
1382
+ */
1383
+ minWeightRatio?: number;
1384
+ };
1385
+ /**
1386
+ * 时间衰减算法
1387
+ * 等待时间越长,优先级加成越高,确保所有任务最终都会被执行
1388
+ */
1389
+ type AgingAlgorithmConfig = {
1390
+ type: 'aging';
1391
+ /**
1392
+ * 每等待多少毫秒增加 1 点优先级,默认 60000(1分钟)
1393
+ * 例如:VIP 优先级 100,普通用户优先级 1
1394
+ * 普通用户等待 100 分钟后,优先级变为 1 + 100 = 101,超过 VIP
1395
+ */
1396
+ intervalMs?: number;
1397
+ };
1398
+ /**
1399
+ * 算法配置类型
1400
+ */
1401
+ type AlgorithmConfig = StrictAlgorithmConfig | WeightedRandomAlgorithmConfig | AgingAlgorithmConfig;
1402
+ interface PriorityStrategyOptions {
1403
+ /**
1404
+ * 调度算法配置,默认 { type: 'weighted-random' }
1405
+ *
1406
+ * @example
1407
+ * // 严格优先级(VIP 绝对优先)
1408
+ * algorithm: { type: 'strict' }
1409
+ *
1410
+ * // 加权随机(默认,平衡公平性)
1411
+ * algorithm: { type: 'weighted-random', minWeightRatio: 0.1 }
1412
+ *
1413
+ * // 时间衰减(确保无饥饿)
1414
+ * algorithm: { type: 'aging', intervalMs: 60000 }
1415
+ */
1416
+ algorithm?: AlgorithmConfig;
1417
+ /** 默认优先级(未配置的组使用此值),默认 1 */
1418
+ defaultPriority?: number;
1419
+ /** 优先级缓存 TTL(毫秒),默认 5000ms。设为 0 禁用缓存 */
1420
+ cacheTtlMs?: number;
1421
+ /**
1422
+ * 自定义优先级计算函数
1423
+ * 如果提供,将使用此函数计算优先级,否则直接使用 config.priority
1424
+ *
1425
+ * @example
1426
+ * // 根据 VIP 等级计算优先级
1427
+ * onGetPriority: (groupId, config) => {
1428
+ * if (groupId.startsWith('vip:')) return config.priority * 10;
1429
+ * return config.priority;
1430
+ * }
1431
+ */
1432
+ onGetPriority?: OnGetPriority;
1433
+ }
1434
+ declare class PriorityStrategy implements DispatchStrategy {
1435
+ /** 本地缓存:groupId -> { priority, expiresAt } */
1436
+ private cache;
1437
+ /** 手动覆盖的优先级(优先级最高) */
1438
+ private overrides;
1439
+ private algorithmConfig;
1440
+ private defaultPriority;
1441
+ private cacheTtlMs;
1442
+ private onGetPriority?;
1443
+ constructor(options?: PriorityStrategyOptions);
1444
+ /**
1445
+ * 手动覆盖某个组的优先级(优先级高于 Redis 配置和 getPriority)
1446
+ * 主要用于测试或临时调整
1447
+ */
1448
+ setPriority(groupId: string, priority: number): void;
1449
+ /**
1450
+ * 清除手动覆盖,恢复使用 Redis 配置或 getPriority
1451
+ */
1452
+ clearPriority(groupId: string): void;
1453
+ /**
1454
+ * 获取组的基础优先级
1455
+ * 优先级来源顺序:overrides > cache > onGetPriority(config) 或 config.priority
1456
+ */
1457
+ private resolvePriority;
1458
+ /**
1459
+ * 严格优先级算法:总是返回优先级最高的组
1460
+ */
1461
+ private selectByStrict;
1462
+ /**
1463
+ * 加权随机算法:根据优先级计算概率选择
1464
+ * 使用 minWeightRatio 确保低优先级组也有机会
1465
+ */
1466
+ private selectByWeightedRandom;
1467
+ /**
1468
+ * 时间衰减算法:等待时间越长,优先级加成越高
1469
+ */
1470
+ private selectByAging;
1471
+ getNextGroup(queue: Queue<any>): Promise<string | null>;
1472
+ }
1473
+ //#endregion
1474
+ export { AddOptions, AgingAlgorithmConfig, AlgorithmConfig, BackoffStrategy, BullBoardGroupMQAdapter, DispatchStrategy, FlowJob, FlowOptions, GroupMQBullBoardAdapterOptions, Job, OnGetPriority, PriorityStrategy, PriorityStrategyOptions, Queue, QueueOptions, RepeatOptions, ReservedJob, StrictAlgorithmConfig, UnrecoverableError, WeightedRandomAlgorithmConfig, Worker, WorkerEvents, WorkerOptions, getWorkersStatus, waitForQueueToEmpty };
1319
1475
  //# sourceMappingURL=index.d.cts.map
package/dist/index.d.ts CHANGED
@@ -37,6 +37,7 @@ declare class Job<T = any> {
37
37
  readonly timestamp: number;
38
38
  readonly orderMs?: number;
39
39
  readonly status: Status$1 | 'unknown';
40
+ readonly parentId?: string;
40
41
  constructor(args: {
41
42
  queue: Queue<T>;
42
43
  id: string;
@@ -56,6 +57,7 @@ declare class Job<T = any> {
56
57
  timestamp: number;
57
58
  orderMs?: number;
58
59
  status?: Status$1 | 'unknown';
60
+ parentId?: string;
59
61
  });
60
62
  getState(): Promise<Status$1 | 'stuck' | 'waiting-children' | 'prioritized' | 'unknown'>;
61
63
  toJSON(): {
@@ -89,6 +91,26 @@ declare class Job<T = any> {
89
91
  * @param timeoutMs Optional timeout in milliseconds (0 = no timeout)
90
92
  */
91
93
  waitUntilFinished(timeoutMs?: number): Promise<unknown>;
94
+ /**
95
+ * Get all child jobs of this job (if it's a parent in a flow).
96
+ * @returns Array of child Job instances
97
+ */
98
+ getChildren(): Promise<Job<any>[]>;
99
+ /**
100
+ * Get the return values of all child jobs in a flow.
101
+ * @returns Object mapping child job IDs to their return values
102
+ */
103
+ getChildrenValues(): Promise<Record<string, any>>;
104
+ /**
105
+ * Get the number of remaining child jobs that haven't completed yet.
106
+ * @returns Number of remaining dependencies, or null if not a parent job
107
+ */
108
+ getDependenciesCount(): Promise<number | null>;
109
+ /**
110
+ * Get the parent job of this job (if it's a child in a flow).
111
+ * @returns Parent Job instance, or undefined if no parent or parent was deleted
112
+ */
113
+ getParent(): Promise<Job<any> | undefined>;
92
114
  static fromReserved<T = any>(queue: Queue<T>, reserved: ReservedJob<T>, meta?: {
93
115
  processedOn?: number;
94
116
  finishedOn?: number;
@@ -537,6 +559,18 @@ declare class Queue<T = any> {
537
559
  * @returns An object mapping child job IDs to their results
538
560
  */
539
561
  getFlowResults(parentId: string): Promise<Record<string, any>>;
562
+ /**
563
+ * Gets all child job IDs for a parent job in a flow.
564
+ * @param parentId The ID of the parent job
565
+ * @returns An array of child job IDs
566
+ */
567
+ getFlowChildrenIds(parentId: string): Promise<string[]>;
568
+ /**
569
+ * Gets all child jobs for a parent job in a flow.
570
+ * @param parentId The ID of the parent job
571
+ * @returns An array of Job instances for all children
572
+ */
573
+ getFlowChildren(parentId: string): Promise<Job<any>[]>;
540
574
  private addSingle;
541
575
  private flushBatch;
542
576
  reserve(): Promise<ReservedJob<T> | null>;
@@ -967,10 +1001,10 @@ type WorkerOptions<T> = {
967
1001
  name?: string;
968
1002
  /**
969
1003
  * The function that processes jobs. Must be async and handle job failures gracefully.
970
- * @param job The reserved job to process
1004
+ * @param job The Job instance to process, with access to all Job methods
971
1005
  * @returns Promise that resolves when job is complete
972
1006
  */
973
- handler: (job: ReservedJob<T>) => Promise<unknown>;
1007
+ handler: (job: Job<T>) => Promise<unknown>;
974
1008
  /**
975
1009
  * Heartbeat interval in milliseconds to keep jobs alive during processing.
976
1010
  * Prevents jobs from timing out during long-running operations.
@@ -987,9 +1021,9 @@ type WorkerOptions<T> = {
987
1021
  /**
988
1022
  * Error handler called when job processing fails or worker encounters errors
989
1023
  * @param err The error that occurred
990
- * @param job The job that failed (if applicable)
1024
+ * @param job The Job instance that failed (if applicable)
991
1025
  */
992
- onError?: (err: unknown, job?: ReservedJob<T>) => void;
1026
+ onError?: (err: unknown, job?: Job<T>) => void;
993
1027
  /**
994
1028
  * Maximum number of retry attempts for failed jobs at the worker level.
995
1029
  * This overrides the queue's default maxAttempts setting.
@@ -1256,14 +1290,14 @@ declare class _Worker<T = any> extends TypedEventEmitter<WorkerEvents<T>> {
1256
1290
  * For concurrency > 1, returns the oldest job in progress
1257
1291
  */
1258
1292
  getCurrentJob(): {
1259
- job: ReservedJob<T>;
1293
+ job: Job<T>;
1260
1294
  processingTimeMs: number;
1261
1295
  } | null;
1262
1296
  /**
1263
1297
  * Get information about all currently processing jobs
1264
1298
  */
1265
1299
  getCurrentJobs(): Array<{
1266
- job: ReservedJob<T>;
1300
+ job: Job<T>;
1267
1301
  processingTimeMs: number;
1268
1302
  }>;
1269
1303
  /**
@@ -1315,5 +1349,127 @@ declare function getWorkersStatus<T = any>(workers: Worker<T>[]): {
1315
1349
  }>;
1316
1350
  };
1317
1351
  //#endregion
1318
- export { AddOptions, BackoffStrategy, BullBoardGroupMQAdapter, FlowJob, FlowOptions, GroupMQBullBoardAdapterOptions, Job, Queue, QueueOptions, RepeatOptions, ReservedJob, UnrecoverableError, Worker, WorkerEvents, WorkerOptions, getWorkersStatus, waitForQueueToEmpty };
1352
+ //#region src/strategies/priority-strategy.d.ts
1353
+ type GroupConfig = {
1354
+ priority: number;
1355
+ concurrency: number;
1356
+ };
1357
+ /**
1358
+ * 自定义优先级计算函数
1359
+ * @param groupId 组 ID
1360
+ * @param config 从 Redis 读取的组配置
1361
+ * @returns 优先级数值(数字越大优先级越高)
1362
+ */
1363
+ type OnGetPriority = (groupId: string, config: GroupConfig) => number | Promise<number>;
1364
+ /**
1365
+ * 严格优先级算法
1366
+ * 总是选择优先级最高的组,可能导致低优先级组饥饿
1367
+ */
1368
+ type StrictAlgorithmConfig = {
1369
+ type: 'strict';
1370
+ };
1371
+ /**
1372
+ * 加权随机算法(默认)
1373
+ * 根据优先级计算概率选择,确保低优先级组也有机会被执行
1374
+ */
1375
+ type WeightedRandomAlgorithmConfig = {
1376
+ type: 'weighted-random';
1377
+ /**
1378
+ * 最低优先级组的保底权重比例,默认 0.1(10%)
1379
+ * 例如:VIP 优先级 100,普通用户优先级 1
1380
+ * 普通用户的权重会被提升到 100 * 0.1 = 10,而不是 1
1381
+ * 这样普通用户大约有 10/(100+10) ≈ 9% 的概率被选中
1382
+ */
1383
+ minWeightRatio?: number;
1384
+ };
1385
+ /**
1386
+ * 时间衰减算法
1387
+ * 等待时间越长,优先级加成越高,确保所有任务最终都会被执行
1388
+ */
1389
+ type AgingAlgorithmConfig = {
1390
+ type: 'aging';
1391
+ /**
1392
+ * 每等待多少毫秒增加 1 点优先级,默认 60000(1分钟)
1393
+ * 例如:VIP 优先级 100,普通用户优先级 1
1394
+ * 普通用户等待 100 分钟后,优先级变为 1 + 100 = 101,超过 VIP
1395
+ */
1396
+ intervalMs?: number;
1397
+ };
1398
+ /**
1399
+ * 算法配置类型
1400
+ */
1401
+ type AlgorithmConfig = StrictAlgorithmConfig | WeightedRandomAlgorithmConfig | AgingAlgorithmConfig;
1402
+ interface PriorityStrategyOptions {
1403
+ /**
1404
+ * 调度算法配置,默认 { type: 'weighted-random' }
1405
+ *
1406
+ * @example
1407
+ * // 严格优先级(VIP 绝对优先)
1408
+ * algorithm: { type: 'strict' }
1409
+ *
1410
+ * // 加权随机(默认,平衡公平性)
1411
+ * algorithm: { type: 'weighted-random', minWeightRatio: 0.1 }
1412
+ *
1413
+ * // 时间衰减(确保无饥饿)
1414
+ * algorithm: { type: 'aging', intervalMs: 60000 }
1415
+ */
1416
+ algorithm?: AlgorithmConfig;
1417
+ /** 默认优先级(未配置的组使用此值),默认 1 */
1418
+ defaultPriority?: number;
1419
+ /** 优先级缓存 TTL(毫秒),默认 5000ms。设为 0 禁用缓存 */
1420
+ cacheTtlMs?: number;
1421
+ /**
1422
+ * 自定义优先级计算函数
1423
+ * 如果提供,将使用此函数计算优先级,否则直接使用 config.priority
1424
+ *
1425
+ * @example
1426
+ * // 根据 VIP 等级计算优先级
1427
+ * onGetPriority: (groupId, config) => {
1428
+ * if (groupId.startsWith('vip:')) return config.priority * 10;
1429
+ * return config.priority;
1430
+ * }
1431
+ */
1432
+ onGetPriority?: OnGetPriority;
1433
+ }
1434
+ declare class PriorityStrategy implements DispatchStrategy {
1435
+ /** 本地缓存:groupId -> { priority, expiresAt } */
1436
+ private cache;
1437
+ /** 手动覆盖的优先级(优先级最高) */
1438
+ private overrides;
1439
+ private algorithmConfig;
1440
+ private defaultPriority;
1441
+ private cacheTtlMs;
1442
+ private onGetPriority?;
1443
+ constructor(options?: PriorityStrategyOptions);
1444
+ /**
1445
+ * 手动覆盖某个组的优先级(优先级高于 Redis 配置和 getPriority)
1446
+ * 主要用于测试或临时调整
1447
+ */
1448
+ setPriority(groupId: string, priority: number): void;
1449
+ /**
1450
+ * 清除手动覆盖,恢复使用 Redis 配置或 getPriority
1451
+ */
1452
+ clearPriority(groupId: string): void;
1453
+ /**
1454
+ * 获取组的基础优先级
1455
+ * 优先级来源顺序:overrides > cache > onGetPriority(config) 或 config.priority
1456
+ */
1457
+ private resolvePriority;
1458
+ /**
1459
+ * 严格优先级算法:总是返回优先级最高的组
1460
+ */
1461
+ private selectByStrict;
1462
+ /**
1463
+ * 加权随机算法:根据优先级计算概率选择
1464
+ * 使用 minWeightRatio 确保低优先级组也有机会
1465
+ */
1466
+ private selectByWeightedRandom;
1467
+ /**
1468
+ * 时间衰减算法:等待时间越长,优先级加成越高
1469
+ */
1470
+ private selectByAging;
1471
+ getNextGroup(queue: Queue<any>): Promise<string | null>;
1472
+ }
1473
+ //#endregion
1474
+ export { AddOptions, AgingAlgorithmConfig, AlgorithmConfig, BackoffStrategy, BullBoardGroupMQAdapter, DispatchStrategy, FlowJob, FlowOptions, GroupMQBullBoardAdapterOptions, Job, OnGetPriority, PriorityStrategy, PriorityStrategyOptions, Queue, QueueOptions, RepeatOptions, ReservedJob, StrictAlgorithmConfig, UnrecoverableError, WeightedRandomAlgorithmConfig, Worker, WorkerEvents, WorkerOptions, getWorkersStatus, waitForQueueToEmpty };
1319
1475
  //# sourceMappingURL=index.d.ts.map