@taicode/common-server 1.0.15 → 1.0.16
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/output/redis-queue/redis-queue-batch-consumer.d.ts +22 -2
- package/output/redis-queue/redis-queue-batch-consumer.d.ts.map +1 -1
- package/output/redis-queue/redis-queue-batch-consumer.js +20 -0
- package/output/redis-queue/redis-queue-consumer.d.ts +20 -2
- package/output/redis-queue/redis-queue-consumer.d.ts.map +1 -1
- package/output/redis-queue/redis-queue-consumer.js +18 -0
- package/output/redis-queue/redis-queue-provider.d.ts +19 -6
- package/output/redis-queue/redis-queue-provider.d.ts.map +1 -1
- package/output/redis-queue/redis-queue-provider.js +14 -1
- package/output/redis-queue/test-helpers.d.ts +4 -4
- package/output/redis-queue/test-helpers.d.ts.map +1 -1
- package/output/redis-queue/types.d.ts +44 -19
- package/output/redis-queue/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -5,10 +5,12 @@ import type { BatchConsumerConfig, QueueStats, RedisQueueRegistry } from './type
|
|
|
5
5
|
*
|
|
6
6
|
* 批量消费任务,每次处理多条
|
|
7
7
|
*
|
|
8
|
+
* @template R Registry 类型,默认为 RedisQueueRegistry
|
|
8
9
|
* @template K 队列键名类型
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* ```ts
|
|
13
|
+
* // 方式 1: 使用默认 RedisQueueRegistry
|
|
12
14
|
* const consumer = new RedisQueueBatchConsumer('email-queue', {
|
|
13
15
|
* redisUrl: 'redis://localhost:6379',
|
|
14
16
|
* batchSize: 50,
|
|
@@ -18,6 +20,24 @@ import type { BatchConsumerConfig, QueueStats, RedisQueueRegistry } from './type
|
|
|
18
20
|
* }
|
|
19
21
|
* })
|
|
20
22
|
*
|
|
23
|
+
* // 方式 2: 使用自定义 Registry
|
|
24
|
+
* interface MyRegistry {
|
|
25
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const consumer = new RedisQueueBatchConsumer<MyRegistry, 'order-queue'>(
|
|
29
|
+
* 'order-queue',
|
|
30
|
+
* {
|
|
31
|
+
* redisUrl: 'redis://localhost:6379',
|
|
32
|
+
* batchSize: 50,
|
|
33
|
+
* handler: async (dataList) => {
|
|
34
|
+
* const total = dataList.reduce((sum, d) => sum + d.amount, 0)
|
|
35
|
+
* console.log('Total amount:', total)
|
|
36
|
+
* return catchIt(() => {})
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* )
|
|
40
|
+
*
|
|
21
41
|
* await consumer.connect() // 自动开始消费
|
|
22
42
|
*
|
|
23
43
|
* // 获取统计信息
|
|
@@ -27,14 +47,14 @@ import type { BatchConsumerConfig, QueueStats, RedisQueueRegistry } from './type
|
|
|
27
47
|
* consumer.disconnect()
|
|
28
48
|
* ```
|
|
29
49
|
*/
|
|
30
|
-
export declare class RedisQueueBatchConsumer<K extends keyof
|
|
50
|
+
export declare class RedisQueueBatchConsumer<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string> extends RedisQueueCommon {
|
|
31
51
|
private consumerRunning;
|
|
32
52
|
private consumerInterval;
|
|
33
53
|
private recoveryInterval;
|
|
34
54
|
private processingBatches;
|
|
35
55
|
private processingPromises;
|
|
36
56
|
private readonly config;
|
|
37
|
-
constructor(queueKey: K, config: BatchConsumerConfig<K>);
|
|
57
|
+
constructor(queueKey: K, config: BatchConsumerConfig<R[K]>);
|
|
38
58
|
protected getLogPrefix(): string;
|
|
39
59
|
/**
|
|
40
60
|
* 连接 Redis 并自动启动消费者
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-queue-batch-consumer.d.ts","sourceRoot":"","sources":["../../source/redis-queue/redis-queue-batch-consumer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAA0B,UAAU,EAAE,kBAAkB,EAAE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AI,OAAO,CAAC,OAAO,CAAC;CAMjC"}
|
|
@@ -5,10 +5,12 @@ import { RedisQueueCommon } from './redis-queue-common';
|
|
|
5
5
|
*
|
|
6
6
|
* 批量消费任务,每次处理多条
|
|
7
7
|
*
|
|
8
|
+
* @template R Registry 类型,默认为 RedisQueueRegistry
|
|
8
9
|
* @template K 队列键名类型
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* ```ts
|
|
13
|
+
* // 方式 1: 使用默认 RedisQueueRegistry
|
|
12
14
|
* const consumer = new RedisQueueBatchConsumer('email-queue', {
|
|
13
15
|
* redisUrl: 'redis://localhost:6379',
|
|
14
16
|
* batchSize: 50,
|
|
@@ -18,6 +20,24 @@ import { RedisQueueCommon } from './redis-queue-common';
|
|
|
18
20
|
* }
|
|
19
21
|
* })
|
|
20
22
|
*
|
|
23
|
+
* // 方式 2: 使用自定义 Registry
|
|
24
|
+
* interface MyRegistry {
|
|
25
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const consumer = new RedisQueueBatchConsumer<MyRegistry, 'order-queue'>(
|
|
29
|
+
* 'order-queue',
|
|
30
|
+
* {
|
|
31
|
+
* redisUrl: 'redis://localhost:6379',
|
|
32
|
+
* batchSize: 50,
|
|
33
|
+
* handler: async (dataList) => {
|
|
34
|
+
* const total = dataList.reduce((sum, d) => sum + d.amount, 0)
|
|
35
|
+
* console.log('Total amount:', total)
|
|
36
|
+
* return catchIt(() => {})
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* )
|
|
40
|
+
*
|
|
21
41
|
* await consumer.connect() // 自动开始消费
|
|
22
42
|
*
|
|
23
43
|
* // 获取统计信息
|
|
@@ -5,10 +5,12 @@ import type { RedisQueueConsumerConfig, RedisQueueRegistry, QueueStats } from '.
|
|
|
5
5
|
*
|
|
6
6
|
* 只负责消费任务,不包含入队逻辑
|
|
7
7
|
*
|
|
8
|
+
* @template R Registry 类型,默认为 RedisQueueRegistry
|
|
8
9
|
* @template K 队列键名类型
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* ```ts
|
|
13
|
+
* // 方式 1: 使用默认 RedisQueueRegistry
|
|
12
14
|
* const consumer = new RedisQueueConsumer('email-queue', {
|
|
13
15
|
* redisUrl: 'redis://localhost:6379',
|
|
14
16
|
* concurrency: 5,
|
|
@@ -18,6 +20,22 @@ import type { RedisQueueConsumerConfig, RedisQueueRegistry, QueueStats } from '.
|
|
|
18
20
|
* }
|
|
19
21
|
* })
|
|
20
22
|
*
|
|
23
|
+
* // 方式 2: 使用自定义 Registry
|
|
24
|
+
* interface MyRegistry {
|
|
25
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const consumer = new RedisQueueConsumer<MyRegistry, 'order-queue'>(
|
|
29
|
+
* 'order-queue',
|
|
30
|
+
* {
|
|
31
|
+
* redisUrl: 'redis://localhost:6379',
|
|
32
|
+
* handler: async (data) => {
|
|
33
|
+
* console.log(data.orderId, data.amount)
|
|
34
|
+
* return catchIt(() => {})
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* )
|
|
38
|
+
*
|
|
21
39
|
* await consumer.connect() // 自动开始消费
|
|
22
40
|
*
|
|
23
41
|
* // 获取统计信息
|
|
@@ -27,14 +45,14 @@ import type { RedisQueueConsumerConfig, RedisQueueRegistry, QueueStats } from '.
|
|
|
27
45
|
* consumer.disconnect()
|
|
28
46
|
* ```
|
|
29
47
|
*/
|
|
30
|
-
export declare class RedisQueueConsumer<K extends keyof
|
|
48
|
+
export declare class RedisQueueConsumer<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string> extends RedisQueueCommon {
|
|
31
49
|
private consumerRunning;
|
|
32
50
|
private consumerInterval;
|
|
33
51
|
private recoveryInterval;
|
|
34
52
|
private processingTasks;
|
|
35
53
|
private processingPromises;
|
|
36
54
|
private readonly config;
|
|
37
|
-
constructor(queueKey: K, config: RedisQueueConsumerConfig<K>);
|
|
55
|
+
constructor(queueKey: K, config: RedisQueueConsumerConfig<R[K]>);
|
|
38
56
|
protected getLogPrefix(): string;
|
|
39
57
|
/**
|
|
40
58
|
* 连接 Redis 并自动启动消费者
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-queue-consumer.d.ts","sourceRoot":"","sources":["../../source/redis-queue/redis-queue-consumer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,wBAAwB,EAAe,kBAAkB,EAAgB,UAAU,EAAE,MAAM,SAAS,CAAA;AAElH
|
|
1
|
+
{"version":3,"file":"redis-queue-consumer.d.ts","sourceRoot":"","sources":["../../source/redis-queue/redis-queue-consumer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,wBAAwB,EAAe,kBAAkB,EAAgB,UAAU,EAAE,MAAM,SAAS,CAAA;AAElH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,qBAAa,kBAAkB,CAC7B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,kBAAkB,EAClD,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,CAC7C,SAAQ,gBAAgB;IACxB,OAAO,CAAC,eAAe,CAAQ;IAC/B,OAAO,CAAC,gBAAgB,CAA8B;IACtD,OAAO,CAAC,gBAAgB,CAA8B;IACtD,OAAO,CAAC,eAAe,CAAI;IAC3B,OAAO,CAAC,kBAAkB,CAA2B;IAErD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAOtB;gBAEW,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,wBAAwB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAuB/D,SAAS,CAAC,YAAY,IAAI,MAAM;IAIhC;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAO9B;;;OAGG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAgBjC;;OAEG;YACW,WAAW;IA2FzB;;OAEG;IACH,OAAO,CAAC,aAAa;IAoBrB;;OAEG;IACH,OAAO,CAAC,YAAY;IAQpB;;OAEG;IACH,OAAO,CAAC,aAAa;IA4ErB;;OAEG;IACH,OAAO,CAAC,YAAY;IASpB;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC;IAevC;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC;CAMjC"}
|
|
@@ -5,10 +5,12 @@ import { RedisQueueCommon } from './redis-queue-common';
|
|
|
5
5
|
*
|
|
6
6
|
* 只负责消费任务,不包含入队逻辑
|
|
7
7
|
*
|
|
8
|
+
* @template R Registry 类型,默认为 RedisQueueRegistry
|
|
8
9
|
* @template K 队列键名类型
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* ```ts
|
|
13
|
+
* // 方式 1: 使用默认 RedisQueueRegistry
|
|
12
14
|
* const consumer = new RedisQueueConsumer('email-queue', {
|
|
13
15
|
* redisUrl: 'redis://localhost:6379',
|
|
14
16
|
* concurrency: 5,
|
|
@@ -18,6 +20,22 @@ import { RedisQueueCommon } from './redis-queue-common';
|
|
|
18
20
|
* }
|
|
19
21
|
* })
|
|
20
22
|
*
|
|
23
|
+
* // 方式 2: 使用自定义 Registry
|
|
24
|
+
* interface MyRegistry {
|
|
25
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* const consumer = new RedisQueueConsumer<MyRegistry, 'order-queue'>(
|
|
29
|
+
* 'order-queue',
|
|
30
|
+
* {
|
|
31
|
+
* redisUrl: 'redis://localhost:6379',
|
|
32
|
+
* handler: async (data) => {
|
|
33
|
+
* console.log(data.orderId, data.amount)
|
|
34
|
+
* return catchIt(() => {})
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* )
|
|
38
|
+
*
|
|
21
39
|
* await consumer.connect() // 自动开始消费
|
|
22
40
|
*
|
|
23
41
|
* // 获取统计信息
|
|
@@ -5,11 +5,12 @@ import type { RedisQueueRegistry, RedisQueueProviderConfig, Task, QueueStats } f
|
|
|
5
5
|
*
|
|
6
6
|
* 只负责任务的入队和队列管理,不包含消费逻辑
|
|
7
7
|
*
|
|
8
|
+
* @template R Registry 类型,默认为 RedisQueueRegistry
|
|
8
9
|
* @template K 队列键类型
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* ```ts
|
|
12
|
-
* //
|
|
13
|
+
* // 方式 1: 使用默认 RedisQueueRegistry (需要提前声明模块扩展)
|
|
13
14
|
* const provider = new RedisQueueProvider('email-queue', {
|
|
14
15
|
* redisUrl: 'redis://localhost:6379'
|
|
15
16
|
* })
|
|
@@ -19,11 +20,23 @@ import type { RedisQueueRegistry, RedisQueueProviderConfig, Task, QueueStats } f
|
|
|
19
20
|
* subject: 'Hello',
|
|
20
21
|
* body: 'World'
|
|
21
22
|
* })
|
|
23
|
+
*
|
|
24
|
+
* // 方式 2: 使用自定义 Registry
|
|
25
|
+
* interface MyRegistry {
|
|
26
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* const provider = new RedisQueueProvider<MyRegistry, 'order-queue'>(
|
|
30
|
+
* 'order-queue',
|
|
31
|
+
* { redisUrl: 'redis://localhost:6379' }
|
|
32
|
+
* )
|
|
33
|
+
*
|
|
34
|
+
* await provider.enqueue({ orderId: '123', amount: 100 })
|
|
22
35
|
* ```
|
|
23
36
|
*/
|
|
24
|
-
export declare class RedisQueueProvider<K extends keyof
|
|
37
|
+
export declare class RedisQueueProvider<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string> extends RedisQueueCommon {
|
|
25
38
|
private readonly processingDelay;
|
|
26
|
-
constructor(queueKey: K, config?: RedisQueueProviderConfig
|
|
39
|
+
constructor(queueKey: K, config?: RedisQueueProviderConfig);
|
|
27
40
|
protected getLogPrefix(): string;
|
|
28
41
|
/**
|
|
29
42
|
* 将任务推入队列(支持单个或批量)
|
|
@@ -37,12 +50,12 @@ export declare class RedisQueueProvider<K extends keyof RedisQueueRegistry> exte
|
|
|
37
50
|
* await provider.enqueue({ id: 'email-123', to: 'user@example.com' })
|
|
38
51
|
* await provider.enqueue({ id: 'email-123', to: 'user@example.com' }) // 会被跳过
|
|
39
52
|
*/
|
|
40
|
-
enqueue(data:
|
|
41
|
-
enqueue(data:
|
|
53
|
+
enqueue(data: R[K][]): Promise<string[]>;
|
|
54
|
+
enqueue(data: R[K]): Promise<string>;
|
|
42
55
|
/**
|
|
43
56
|
* 获取任务详情
|
|
44
57
|
*/
|
|
45
|
-
getTask(taskId: string): Promise<Task<
|
|
58
|
+
getTask(taskId: string): Promise<Task<R[K]> | null>;
|
|
46
59
|
/**
|
|
47
60
|
* 获取队列统计信息(O(1) 时间复杂度)
|
|
48
61
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redis-queue-provider.d.ts","sourceRoot":"","sources":["../../source/redis-queue/redis-queue-provider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAE7F
|
|
1
|
+
{"version":3,"file":"redis-queue-provider.d.ts","sourceRoot":"","sources":["../../source/redis-queue/redis-queue-provider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,kBAAkB,EAAE,wBAAwB,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAE7F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,qBAAa,kBAAkB,CAC7B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,kBAAkB,EAClD,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,CAC7C,SAAQ,gBAAgB;IACxB,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAQ;gBAE5B,QAAQ,EAAE,CAAC,EAAE,MAAM,GAAE,wBAA6B;IAU9D,SAAS,CAAC,YAAY,IAAI,MAAM;IAMhC;;;;;;;;;;;OAWG;IACG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IACxC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IA0F1C;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAWzD;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC;IAevC;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAmD7B"}
|
|
@@ -5,11 +5,12 @@ import { RedisQueueCommon } from './redis-queue-common';
|
|
|
5
5
|
*
|
|
6
6
|
* 只负责任务的入队和队列管理,不包含消费逻辑
|
|
7
7
|
*
|
|
8
|
+
* @template R Registry 类型,默认为 RedisQueueRegistry
|
|
8
9
|
* @template K 队列键类型
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* ```ts
|
|
12
|
-
* //
|
|
13
|
+
* // 方式 1: 使用默认 RedisQueueRegistry (需要提前声明模块扩展)
|
|
13
14
|
* const provider = new RedisQueueProvider('email-queue', {
|
|
14
15
|
* redisUrl: 'redis://localhost:6379'
|
|
15
16
|
* })
|
|
@@ -19,6 +20,18 @@ import { RedisQueueCommon } from './redis-queue-common';
|
|
|
19
20
|
* subject: 'Hello',
|
|
20
21
|
* body: 'World'
|
|
21
22
|
* })
|
|
23
|
+
*
|
|
24
|
+
* // 方式 2: 使用自定义 Registry
|
|
25
|
+
* interface MyRegistry {
|
|
26
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* const provider = new RedisQueueProvider<MyRegistry, 'order-queue'>(
|
|
30
|
+
* 'order-queue',
|
|
31
|
+
* { redisUrl: 'redis://localhost:6379' }
|
|
32
|
+
* )
|
|
33
|
+
*
|
|
34
|
+
* await provider.enqueue({ orderId: '123', amount: 100 })
|
|
22
35
|
* ```
|
|
23
36
|
*/
|
|
24
37
|
export class RedisQueueProvider extends RedisQueueCommon {
|
|
@@ -38,7 +38,7 @@ import type { RedisQueueRegistry, Task } from './types';
|
|
|
38
38
|
* })
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
export declare function dispatchQueueTask<K extends keyof
|
|
41
|
+
export declare function dispatchQueueTask<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string>(consumer: RedisQueueConsumer<R, K> | RedisQueueBatchConsumer<R, K>, taskIds: string[]): Promise<void>;
|
|
42
42
|
/**
|
|
43
43
|
* 测试辅助函数:等待队列达到指定状态
|
|
44
44
|
*
|
|
@@ -56,7 +56,7 @@ export declare function dispatchQueueTask<K extends keyof RedisQueueRegistry>(co
|
|
|
56
56
|
* await waitQueueCompletion(provider, stats => stats.completed >= 10, { timeout: 5000 })
|
|
57
57
|
* ```
|
|
58
58
|
*/
|
|
59
|
-
export declare function waitQueueCompletion<K extends keyof
|
|
59
|
+
export declare function waitQueueCompletion<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string>(instance: RedisQueueProvider<R, K> | RedisQueueConsumer<R, K> | RedisQueueBatchConsumer<R, K>, predicate: (stats: {
|
|
60
60
|
pending: number;
|
|
61
61
|
processing: number;
|
|
62
62
|
completed: number;
|
|
@@ -88,7 +88,7 @@ export declare function waitQueueCompletion<K extends keyof RedisQueueRegistry>(
|
|
|
88
88
|
* })
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
|
-
export declare function clearQueue<K extends keyof
|
|
91
|
+
export declare function clearQueue<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string>(instance: RedisQueueProvider<R, K> | RedisQueueConsumer<R, K> | RedisQueueBatchConsumer<R, K>): Promise<void>;
|
|
92
92
|
/**
|
|
93
93
|
* 测试辅助函数:获取队列中的所有任务详情
|
|
94
94
|
*
|
|
@@ -108,5 +108,5 @@ export declare function clearQueue<K extends keyof RedisQueueRegistry>(instance:
|
|
|
108
108
|
* expect(failedTasks[0].data).toMatchObject({ id: 123 })
|
|
109
109
|
* ```
|
|
110
110
|
*/
|
|
111
|
-
export declare function getQueueTasks<K extends keyof
|
|
111
|
+
export declare function getQueueTasks<R extends Record<string, any> = RedisQueueRegistry, K extends keyof R & string = keyof R & string>(provider: RedisQueueProvider<R, K>, status?: 'pending' | 'processing' | 'completed' | 'failed'): Promise<Task<R[K]>[]>;
|
|
112
112
|
//# sourceMappingURL=test-helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../../source/redis-queue/test-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAA;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAChE,OAAO,KAAK,EAAE,kBAAkB,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAsB,iBAAiB,
|
|
1
|
+
{"version":3,"file":"test-helpers.d.ts","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AAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAO,GACpD,OAAO,CAAC,IAAI,CAAC,CAiBf;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,UAAU,CAC9B,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,kBAAkB,EAClD,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,EAE7C,QAAQ,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,GAC5F,OAAO,CAAC,IAAI,CAAC,CAcf;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,aAAa,CACjC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,kBAAkB,EAClD,CAAC,SAAS,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM,CAAC,GAAG,MAAM,EAE7C,QAAQ,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,EAClC,MAAM,CAAC,EAAE,SAAS,GAAG,YAAY,GAAG,WAAW,GAAG,QAAQ,GACzD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAmCvB"}
|
|
@@ -1,35 +1,60 @@
|
|
|
1
1
|
import { CatchResult } from '@taicode/common-base';
|
|
2
2
|
import type { RedisClientType } from 'redis';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* 队列注册表(默认)
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* 支持两种使用方式:
|
|
7
|
+
*
|
|
8
|
+
* **方式 1: 模块声明扩展(常规,全局生效)**
|
|
7
9
|
*
|
|
8
10
|
* @example
|
|
9
11
|
* ```ts
|
|
10
|
-
* //
|
|
11
|
-
*
|
|
12
|
+
* // types/redis-queue.d.ts
|
|
13
|
+
* import '@taicode/common-server'
|
|
14
|
+
*
|
|
15
|
+
* declare module '@taicode/common-server/redis-queue' {
|
|
12
16
|
* interface RedisQueueRegistry {
|
|
13
17
|
* 'email-queue': { to: string; subject: string; body: string }
|
|
14
18
|
* 'sms-queue': { phone: string; message: string }
|
|
15
|
-
* 'payment-queue': { orderId: string; amount: number }
|
|
16
19
|
* }
|
|
17
20
|
* }
|
|
18
21
|
*
|
|
19
|
-
* //
|
|
20
|
-
* const provider = new RedisQueueProvider({
|
|
21
|
-
*
|
|
22
|
-
* })
|
|
23
|
-
*
|
|
24
|
-
* const consumer = new RedisQueueConsumer({
|
|
25
|
-
* queueKey: 'email-queue',
|
|
22
|
+
* // 使用时自动类型检查
|
|
23
|
+
* const provider = new RedisQueueProvider('email-queue', { redisUrl: '...' })
|
|
24
|
+
* const consumer = new RedisQueueConsumer('email-queue', {
|
|
26
25
|
* handler: async (data) => {
|
|
27
|
-
* // data
|
|
26
|
+
* // data 类型自动推断为 { to: string; subject: string; body: string }
|
|
28
27
|
* console.log(data.to)
|
|
29
|
-
* return catchIt(() => {})
|
|
30
28
|
* }
|
|
31
29
|
* })
|
|
32
30
|
* ```
|
|
31
|
+
*
|
|
32
|
+
* **方式 2: 泛型参数(灵活,局部使用)**
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* // 定义自定义 Registry
|
|
37
|
+
* interface MyQueueRegistry {
|
|
38
|
+
* 'order-queue': { orderId: string; amount: number }
|
|
39
|
+
* 'notify-queue': { userId: string; message: string }
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* // 使用时传入泛型参数
|
|
43
|
+
* const provider = new RedisQueueProvider<MyQueueRegistry>(
|
|
44
|
+
* 'order-queue',
|
|
45
|
+
* { redisUrl: '...' }
|
|
46
|
+
* )
|
|
47
|
+
*
|
|
48
|
+
* const consumer = new RedisQueueConsumer<MyQueueRegistry>(
|
|
49
|
+
* 'order-queue',
|
|
50
|
+
* {
|
|
51
|
+
* handler: async (data) => {
|
|
52
|
+
* // data 类型自动推断为 { orderId: string; amount: number }
|
|
53
|
+
* console.log(data.orderId)
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
* )
|
|
57
|
+
* ```
|
|
33
58
|
*/
|
|
34
59
|
export interface RedisQueueRegistry {
|
|
35
60
|
}
|
|
@@ -77,7 +102,7 @@ export interface RedisQueueCommonConfig {
|
|
|
77
102
|
/**
|
|
78
103
|
* 队列生产者配置
|
|
79
104
|
*/
|
|
80
|
-
export interface RedisQueueProviderConfig
|
|
105
|
+
export interface RedisQueueProviderConfig {
|
|
81
106
|
redisUrl?: string;
|
|
82
107
|
redisClient?: RedisClientType;
|
|
83
108
|
cleanupDelay?: number;
|
|
@@ -86,10 +111,10 @@ export interface RedisQueueProviderConfig<K extends keyof RedisQueueRegistry = k
|
|
|
86
111
|
/**
|
|
87
112
|
* 队列消费者配置
|
|
88
113
|
*/
|
|
89
|
-
export interface RedisQueueConsumerConfig<
|
|
114
|
+
export interface RedisQueueConsumerConfig<T extends TaskData = TaskData> {
|
|
90
115
|
redisUrl?: string;
|
|
91
116
|
redisClient?: RedisClientType;
|
|
92
|
-
handler: TaskHandler<
|
|
117
|
+
handler: TaskHandler<T>;
|
|
93
118
|
maxRetries?: number;
|
|
94
119
|
concurrency?: number;
|
|
95
120
|
consumerInterval?: number;
|
|
@@ -103,10 +128,10 @@ export type TaskHandler<T extends TaskData = TaskData> = (data: T) => Promise<Ca
|
|
|
103
128
|
/**
|
|
104
129
|
* 批量消费者配置
|
|
105
130
|
*/
|
|
106
|
-
export interface BatchConsumerConfig<
|
|
131
|
+
export interface BatchConsumerConfig<T extends TaskData = TaskData> {
|
|
107
132
|
redisUrl?: string;
|
|
108
133
|
redisClient?: RedisClientType;
|
|
109
|
-
handler: BatchTaskHandler<
|
|
134
|
+
handler: BatchTaskHandler<T>;
|
|
110
135
|
batchSize?: number;
|
|
111
136
|
maxRetries?: number;
|
|
112
137
|
concurrency?: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../source/redis-queue/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAA;AAE5C
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../source/redis-queue/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAA;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AAEH,MAAM,WAAW,kBAAkB;CAElC;AAED;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,SAAS,GAAG,YAAY,GAAG,WAAW,GAAG,QAAQ,CAAA;AAEtE;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG;IAAE,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,CAAA;AAE1D;;GAEG;AACH,MAAM,WAAW,IAAI,CAAC,CAAC,GAAG,QAAQ;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,CAAC,CAAA;IACP,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CA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