koatty_schedule 3.3.1 → 3.3.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.ts CHANGED
@@ -1,12 +1,143 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-06-22 23:42:00
3
+ * @Date: 2025-10-31 17:51:00
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
+ import { Cluster } from 'ioredis';
8
9
  import { Koatty } from 'koatty_core';
9
- import { Settings } from '@sesamecare-oss/redlock';
10
+ import { Lock } from '@sesamecare-oss/redlock';
11
+ import Redis from 'ioredis';
12
+ import type { Settings } from '@sesamecare-oss/redlock';
13
+
14
+ /**
15
+ * Base Redis configuration
16
+ */
17
+ declare interface BaseRedisConfig {
18
+ mode?: RedisMode;
19
+ host?: string;
20
+ port?: number;
21
+ password?: string;
22
+ db?: number;
23
+ keyPrefix?: string;
24
+ connectTimeout?: number;
25
+ commandTimeout?: number;
26
+ maxRetriesPerRequest?: number;
27
+ }
28
+
29
+ /**
30
+ * Abstract distributed lock interface
31
+ * Allows for different lock implementations (RedLock, Zookeeper, etc.)
32
+ */
33
+ export declare interface IDistributedLock {
34
+ /**
35
+ * Initialize the lock system
36
+ */
37
+ initialize(): Promise<void>;
38
+ /**
39
+ * Acquire a distributed lock
40
+ * @param resources - Resource identifiers
41
+ * @param ttl - Time to live in milliseconds
42
+ */
43
+ acquire(resources: string[], ttl: number): Promise<Lock>;
44
+ /**
45
+ * Release a lock
46
+ * @param lock - Lock instance
47
+ */
48
+ release(lock: Lock): Promise<void>;
49
+ /**
50
+ * Extend lock TTL
51
+ * @param lock - Lock instance
52
+ * @param ttl - New TTL in milliseconds
53
+ */
54
+ extend(lock: Lock, ttl: number): Promise<Lock>;
55
+ /**
56
+ * Check if the lock system is ready
57
+ */
58
+ isReady(): boolean;
59
+ /**
60
+ * Get current configuration
61
+ */
62
+ getConfig(): any;
63
+ /**
64
+ * Close and cleanup
65
+ */
66
+ close(): Promise<void>;
67
+ /**
68
+ * Health check
69
+ */
70
+ healthCheck(): Promise<{
71
+ status: 'healthy' | 'unhealthy';
72
+ details: Record<string, any>;
73
+ }>;
74
+ }
75
+
76
+ /**
77
+ * Lock configuration options
78
+ */
79
+ export declare interface ILockOptions extends Partial<Settings> {
80
+ lockTimeOut?: number;
81
+ clockDriftFactor?: number;
82
+ maxRetries?: number;
83
+ retryDelayMs?: number;
84
+ redisConfig?: RedisConfig;
85
+ }
86
+
87
+ /**
88
+ * Abstract Redis client interface
89
+ * Provides unified interface for different Redis implementations
90
+ */
91
+ export declare interface IRedisClient {
92
+ /**
93
+ * Get connection status
94
+ */
95
+ readonly status: string;
96
+ /**
97
+ * Execute Redis command
98
+ * @param command - Command name
99
+ * @param args - Command arguments
100
+ */
101
+ call(command: string, ...args: any[]): Promise<any>;
102
+ /**
103
+ * Set a key-value pair
104
+ * @param key - Key name
105
+ * @param value - Value
106
+ * @param mode - Optional mode (e.g., 'EX' for expiration)
107
+ * @param duration - Optional duration in seconds
108
+ */
109
+ set(key: string, value: string | Buffer, mode?: string, duration?: number): Promise<'OK' | null>;
110
+ /**
111
+ * Get value by key
112
+ * @param key - Key name
113
+ */
114
+ get(key: string): Promise<string | null>;
115
+ /**
116
+ * Delete one or more keys
117
+ * @param keys - Key names
118
+ */
119
+ del(...keys: string[]): Promise<number>;
120
+ /**
121
+ * Check if key exists
122
+ * @param key - Key name
123
+ */
124
+ exists(key: string): Promise<number>;
125
+ /**
126
+ * Evaluate Lua script
127
+ * @param script - Lua script
128
+ * @param numKeys - Number of keys
129
+ * @param args - Script arguments
130
+ */
131
+ eval(script: string, numKeys: number, ...args: any[]): Promise<any>;
132
+ /**
133
+ * Close the connection
134
+ */
135
+ quit(): Promise<'OK'>;
136
+ /**
137
+ * Disconnect immediately
138
+ */
139
+ disconnect(): void;
140
+ }
10
141
 
11
142
  /**
12
143
  * @param options - The options for the scheduled job
@@ -15,14 +146,106 @@ import { Settings } from '@sesamecare-oss/redlock';
15
146
  export declare function KoattyScheduled(options: ScheduledOptions, app: Koatty): Promise<void>;
16
147
 
17
148
  /**
18
- * Redis connection configuration
149
+ * Redis client wrapper that implements IRedisClient interface
150
+ * Wraps ioredis client to provide unified interface
19
151
  */
20
- declare interface RedisConfig {
21
- host?: string;
22
- port?: number;
23
- password?: string;
24
- db?: number;
25
- keyPrefix?: string;
152
+ export declare class RedisClientAdapter implements IRedisClient {
153
+ constructor(client: Redis | Cluster);
154
+ get status(): string;
155
+ call(command: string, ...args: any[]): Promise<any>;
156
+ set(key: string, value: string | Buffer, mode?: string, duration?: number): Promise<'OK' | null>;
157
+ get(key: string): Promise<string | null>;
158
+ del(...keys: string[]): Promise<number>;
159
+ exists(key: string): Promise<number>;
160
+ eval(script: string, numKeys: number, ...args: any[]): Promise<any>;
161
+ quit(): Promise<'OK'>;
162
+ disconnect(): void;
163
+ /**
164
+ * Get underlying Redis/Cluster instance
165
+ * Used for RedLock initialization
166
+ */
167
+ getClient(): Redis | Cluster;
168
+ }
169
+
170
+ /**
171
+ * Cluster configuration
172
+ */
173
+ export declare interface RedisClusterConfig extends BaseRedisConfig {
174
+ mode: RedisMode.CLUSTER;
175
+ nodes: Array<{
176
+ host: string;
177
+ port: number;
178
+ }>;
179
+ redisOptions?: {
180
+ password?: string;
181
+ db?: number;
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Union type for all Redis configurations
187
+ */
188
+ export declare type RedisConfig = RedisStandaloneConfig | RedisSentinelConfig | RedisClusterConfig;
189
+
190
+ /**
191
+ * Redis client factory
192
+ * Creates appropriate Redis client based on configuration
193
+ */
194
+ export declare class RedisFactory {
195
+ /**
196
+ * Create Redis client based on configuration mode
197
+ * @param config - Redis configuration
198
+ * @returns Redis client adapter
199
+ */
200
+ static createClient(config: RedisConfig): RedisClientAdapter;
201
+ /**
202
+ * Create standalone Redis client
203
+ * @param config - Standalone configuration
204
+ */
205
+ /**
206
+ * Create sentinel Redis client
207
+ * @param config - Sentinel configuration
208
+ */
209
+ /**
210
+ * Create cluster Redis client
211
+ * @param config - Cluster configuration
212
+ */
213
+ /**
214
+ * Validate Redis configuration
215
+ * @param config - Redis configuration to validate
216
+ */
217
+ static validateConfig(config: RedisConfig): void;
218
+ }
219
+
220
+ /**
221
+ * Redis connection mode
222
+ */
223
+ export declare enum RedisMode {
224
+ STANDALONE = "standalone",// 单机模式
225
+ SENTINEL = "sentinel",// 哨兵模式
226
+ CLUSTER = "cluster"
227
+ }
228
+
229
+ /**
230
+ * Sentinel configuration
231
+ */
232
+ export declare interface RedisSentinelConfig extends BaseRedisConfig {
233
+ mode: RedisMode.SENTINEL;
234
+ sentinels: Array<{
235
+ host: string;
236
+ port: number;
237
+ }>;
238
+ name: string;
239
+ sentinelPassword?: string;
240
+ }
241
+
242
+ /**
243
+ * Standalone configuration
244
+ */
245
+ export declare interface RedisStandaloneConfig extends BaseRedisConfig {
246
+ mode?: RedisMode.STANDALONE;
247
+ host: string;
248
+ port: number;
26
249
  }
27
250
 
28
251
  /**
@@ -54,6 +277,95 @@ declare interface RedisConfig {
54
277
  */
55
278
  export declare function RedLock(lockName?: string, options?: RedLockMethodOptions): MethodDecorator;
56
279
 
280
+ /**
281
+ * RedLock distributed lock manager
282
+ * Integrated with koatty IOC container
283
+ * Implements singleton pattern for safe instance management
284
+ * Implements IDistributedLock interface for abstraction
285
+ */
286
+ export declare class RedLocker implements IDistributedLock {
287
+ /**
288
+ * Register RedLocker in IOC container
289
+ * @private
290
+ */
291
+ /**
292
+ * Get RedLocker singleton instance with thread-safe initialization
293
+ * @static
294
+ * @param options - RedLock configuration options (only used for first initialization)
295
+ * @returns RedLocker singleton instance
296
+ */
297
+ static getInstance(options?: RedLockOptions): RedLocker;
298
+ /**
299
+ * Reset singleton instance (主要用于测试)
300
+ * @static
301
+ */
302
+ static resetInstance(): void;
303
+ /**
304
+ * Initialize RedLock with Redis connection
305
+ * Uses cached promise to avoid duplicate initialization
306
+ * @private
307
+ */
308
+ initialize(): Promise<void>;
309
+ /**
310
+ * 执行实际的初始化操作
311
+ * @private
312
+ */
313
+ /**
314
+ * Acquire a distributed lock
315
+ * @param resources - Resource identifiers to lock
316
+ * @param ttl - Time to live in milliseconds
317
+ * @returns Promise<Lock>
318
+ */
319
+ acquire(resources: string[], ttl?: number): Promise<Lock>;
320
+ /**
321
+ * Release a lock
322
+ * @param lock - Lock instance to release
323
+ */
324
+ release(lock: Lock): Promise<void>;
325
+ /**
326
+ * Extend a lock's TTL
327
+ * @param lock - Lock instance to extend
328
+ * @param ttl - New TTL in milliseconds
329
+ * @returns Extended lock
330
+ */
331
+ extend(lock: Lock, ttl: number): Promise<Lock>;
332
+ /**
333
+ * Check if RedLocker is initialized
334
+ * @returns true if initialized, false otherwise
335
+ */
336
+ isReady(): boolean;
337
+ /**
338
+ * Get current configuration
339
+ * @returns Current RedLock configuration
340
+ */
341
+ getConfig(): RedLockOptions;
342
+ /**
343
+ * Update configuration (requires reinitialization)
344
+ * @param options - New RedLock options
345
+ */
346
+ updateConfig(options?: Partial<RedLockOptions>): void;
347
+ /**
348
+ * Close Redis connection and cleanup
349
+ */
350
+ close(): Promise<void>;
351
+ /**
352
+ * Get container registration status
353
+ * @returns Registration information
354
+ */
355
+ getContainerInfo(): {
356
+ registered: boolean;
357
+ identifier: string;
358
+ };
359
+ /**
360
+ * Health check for RedLocker
361
+ * @returns Health status
362
+ */
363
+ healthCheck(): Promise<{
364
+ status: 'healthy' | 'unhealthy';
365
+ details: Record<string, any>;
366
+ }>;
367
+ }
368
+
57
369
  /**
58
370
  * RedLock method-level options (excluding Redis connection config)
59
371
  */
@@ -66,12 +378,9 @@ declare interface RedLockMethodOptions {
66
378
 
67
379
  /**
68
380
  * Configuration options for RedLock
381
+ * @deprecated Use ILockOptions from interface instead
69
382
  */
70
- declare interface RedLockOptions extends Partial<Settings> {
71
- lockTimeOut?: number;
72
- clockDriftFactor?: number;
73
- maxRetries?: number;
74
- retryDelayMs?: number;
383
+ export declare interface RedLockOptions extends ILockOptions {
75
384
  redisConfig?: RedisConfig;
76
385
  }
77
386