@nest-omni/core 2.0.1-9 → 3.1.1-11
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/common/boilerplate.polyfill.d.ts +11 -0
- package/common/boilerplate.polyfill.js +57 -0
- package/common/dto/page-options.dto.d.ts +1 -1
- package/common/index.d.ts +0 -1
- package/common/index.js +0 -1
- package/decorators/controller.decorator.d.ts +1 -1
- package/decorators/property.decorators.js +1 -1
- package/decorators/timestamp-column.decorator.d.ts +1 -1
- package/decorators/user_auth.decorator.d.ts +1 -1
- package/health-checker/health-checker.controller.d.ts +2 -4
- package/health-checker/health-checker.controller.js +2 -5
- package/health-checker/health-checker.module.js +2 -3
- package/health-checker/index.d.ts +2 -0
- package/health-checker/index.js +18 -0
- package/helpers/date.helper.js +10 -10
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/package.json +142 -46
- package/setup/bootstrap.setup.js +37 -87
- package/setup/index.d.ts +5 -0
- package/setup/index.js +5 -0
- package/setup/mode.setup.d.ts +12 -0
- package/setup/mode.setup.js +60 -0
- package/setup/redis-lock.decorator.d.ts +5 -0
- package/setup/redis-lock.decorator.js +78 -0
- package/setup/redis-lock.service.d.ts +59 -0
- package/setup/redis-lock.service.js +362 -0
- package/setup/schedule.decorator.d.ts +23 -0
- package/setup/schedule.decorator.examples.d.ts +18 -0
- package/setup/schedule.decorator.examples.js +255 -0
- package/setup/schedule.decorator.js +233 -0
- package/setup/worker.decorator.d.ts +14 -0
- package/setup/worker.decorator.js +130 -0
- package/shared/serviceRegistryModule.js +17 -19
- package/shared/services/api-config.service.d.ts +9 -8
- package/shared/services/api-config.service.js +55 -46
- package/validator-json/default.js +9 -1
- package/validators/is-exists.validator.js +0 -2
- package/validators/is-unique.validator.js +0 -2
- package/validators/skip-empty.validator.d.ts +1 -1
- package/common/abstract-client.service.d.ts +0 -16
- package/common/abstract-client.service.js +0 -35
- package/health-checker/health-indicators/service.indicator.d.ts +0 -8
- package/health-checker/health-indicators/service.indicator.js +0 -66
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
12
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
13
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
14
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
15
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
16
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
17
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
var RedisLockService_1;
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.RedisLockService = void 0;
|
|
23
|
+
const common_1 = require("@nestjs/common");
|
|
24
|
+
const ioredis_1 = require("ioredis");
|
|
25
|
+
let RedisLockService = RedisLockService_1 = class RedisLockService {
|
|
26
|
+
constructor(redisOptions) {
|
|
27
|
+
this.logger = new common_1.Logger(RedisLockService_1.name);
|
|
28
|
+
this.redis = null;
|
|
29
|
+
this.isConnected = false;
|
|
30
|
+
this.isConnecting = false;
|
|
31
|
+
this.connectionPromise = null;
|
|
32
|
+
this.defaultOptions = {
|
|
33
|
+
ttl: 300000,
|
|
34
|
+
retryCount: 0,
|
|
35
|
+
retryDelay: 100,
|
|
36
|
+
keyPrefix: 'lock',
|
|
37
|
+
throwOnFailure: false,
|
|
38
|
+
autoExtend: 0,
|
|
39
|
+
};
|
|
40
|
+
this.defaultRedisOptions = {
|
|
41
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
42
|
+
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
|
43
|
+
password: process.env.REDIS_PASSWORD,
|
|
44
|
+
db: parseInt(process.env.REDIS_DB || '0', 10),
|
|
45
|
+
retryDelay: 2000,
|
|
46
|
+
maxRetries: 10,
|
|
47
|
+
reconnectOnError: (err) => {
|
|
48
|
+
this.logger.warn('Redis reconnection error:', err.message);
|
|
49
|
+
return true;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
this.redisOptions = redisOptions ||
|
|
53
|
+
RedisLockService_1.globalConfig || Object.assign({}, this.defaultRedisOptions);
|
|
54
|
+
if (!RedisLockService_1.globalInstance) {
|
|
55
|
+
RedisLockService_1.globalInstance = this;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
static initializeGlobalInstance(config) {
|
|
59
|
+
if (!RedisLockService_1.globalInstance) {
|
|
60
|
+
new RedisLockService_1(config || RedisLockService_1.globalConfig);
|
|
61
|
+
}
|
|
62
|
+
return RedisLockService_1.globalInstance;
|
|
63
|
+
}
|
|
64
|
+
static getGlobalInstance() {
|
|
65
|
+
return RedisLockService_1.globalInstance;
|
|
66
|
+
}
|
|
67
|
+
static getOrCreateGlobalInstance() {
|
|
68
|
+
if (!RedisLockService_1.globalInstance) {
|
|
69
|
+
if (!RedisLockService_1.globalConfig) {
|
|
70
|
+
RedisLockService_1.globalConfig = {
|
|
71
|
+
host: process.env.REDIS_HOST || 'localhost',
|
|
72
|
+
port: parseInt(process.env.REDIS_PORT || '6379', 10),
|
|
73
|
+
password: process.env.REDIS_PASSWORD,
|
|
74
|
+
db: parseInt(process.env.REDIS_DB || '0', 10),
|
|
75
|
+
retryDelay: 2000,
|
|
76
|
+
maxRetries: 10,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return RedisLockService_1.initializeGlobalInstance();
|
|
80
|
+
}
|
|
81
|
+
return RedisLockService_1.globalInstance;
|
|
82
|
+
}
|
|
83
|
+
static setGlobalInstance(instance) {
|
|
84
|
+
RedisLockService_1.globalInstance = instance;
|
|
85
|
+
}
|
|
86
|
+
onModuleInit() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
this.logger.log('RedisLockService initialized, will connect on first use');
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
onModuleDestroy() {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
if (this.redis) {
|
|
94
|
+
yield this.redis.quit();
|
|
95
|
+
this.isConnected = false;
|
|
96
|
+
this.logger.log('Redis connection closed');
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
acquireLock(key_1) {
|
|
101
|
+
return __awaiter(this, arguments, void 0, function* (key, options = {}) {
|
|
102
|
+
const redis = yield this.getRedis();
|
|
103
|
+
const opts = Object.assign(Object.assign({}, this.defaultOptions), options);
|
|
104
|
+
const lockKey = this.buildLockKey(key, opts.keyPrefix);
|
|
105
|
+
const lockValue = this.generateLockValue();
|
|
106
|
+
let attempts = 0;
|
|
107
|
+
const maxAttempts = opts.retryCount + 1;
|
|
108
|
+
while (attempts < maxAttempts) {
|
|
109
|
+
try {
|
|
110
|
+
const result = yield redis.set(lockKey, lockValue, 'PX', opts.ttl, 'NX');
|
|
111
|
+
if (result === 'OK') {
|
|
112
|
+
this.logger.debug(`Lock acquired: ${lockKey} (value: ${lockValue}, ttl: ${opts.ttl}ms)`);
|
|
113
|
+
let autoExtendTimer;
|
|
114
|
+
if (opts.autoExtend && opts.autoExtend > 0) {
|
|
115
|
+
autoExtendTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
try {
|
|
117
|
+
yield this.extendLock(key, lockValue, opts.ttl, opts.keyPrefix);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
this.logger.error(`Failed to auto-extend lock ${lockKey}:`, error);
|
|
121
|
+
}
|
|
122
|
+
}), opts.autoExtend);
|
|
123
|
+
}
|
|
124
|
+
return { acquired: true, lockValue, autoExtendTimer };
|
|
125
|
+
}
|
|
126
|
+
attempts++;
|
|
127
|
+
if (attempts < maxAttempts) {
|
|
128
|
+
this.logger.debug(`Lock acquisition failed for ${lockKey}, retrying... (${attempts}/${opts.retryCount})`);
|
|
129
|
+
yield this.sleep(opts.retryDelay);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
const errorMessage = `Error acquiring lock for ${lockKey}: ${error.message}`;
|
|
134
|
+
this.logger.error(errorMessage, error.stack);
|
|
135
|
+
if (opts.throwOnFailure) {
|
|
136
|
+
throw new Error(errorMessage);
|
|
137
|
+
}
|
|
138
|
+
return { acquired: false, error: errorMessage };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const failureMessage = `Failed to acquire lock for ${lockKey} after ${attempts} attempts`;
|
|
142
|
+
this.logger.warn(failureMessage);
|
|
143
|
+
if (opts.throwOnFailure) {
|
|
144
|
+
throw new Error(failureMessage);
|
|
145
|
+
}
|
|
146
|
+
return { acquired: false, error: failureMessage };
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
releaseLock(key_1, lockValue_1) {
|
|
150
|
+
return __awaiter(this, arguments, void 0, function* (key, lockValue, keyPrefix = 'lock') {
|
|
151
|
+
const redis = yield this.getRedis();
|
|
152
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
153
|
+
try {
|
|
154
|
+
const script = `
|
|
155
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
156
|
+
return redis.call("del", KEYS[1])
|
|
157
|
+
else
|
|
158
|
+
return 0
|
|
159
|
+
end
|
|
160
|
+
`;
|
|
161
|
+
const result = yield redis.eval(script, 1, lockKey, lockValue);
|
|
162
|
+
if (result === 1) {
|
|
163
|
+
this.logger.debug(`Lock released: ${lockKey}`);
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
this.logger.warn(`Lock release failed: ${lockKey} (lock value mismatch or already expired)`);
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
this.logger.error(`Error releasing lock for ${lockKey}: ${error.message}`, error.stack);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
extendLock(key_1, lockValue_1, ttl_1) {
|
|
178
|
+
return __awaiter(this, arguments, void 0, function* (key, lockValue, ttl, keyPrefix = 'lock') {
|
|
179
|
+
const redis = yield this.getRedis();
|
|
180
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
181
|
+
try {
|
|
182
|
+
const script = `
|
|
183
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
184
|
+
return redis.call("pexpire", KEYS[1], ARGV[2])
|
|
185
|
+
else
|
|
186
|
+
return 0
|
|
187
|
+
end
|
|
188
|
+
`;
|
|
189
|
+
const result = yield redis.eval(script, 1, lockKey, lockValue, ttl);
|
|
190
|
+
if (result === 1) {
|
|
191
|
+
this.logger.debug(`Lock extended: ${lockKey} (new ttl: ${ttl}ms)`);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
this.logger.warn(`Lock extension failed: ${lockKey} (lock value mismatch or doesn't exist)`);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
this.logger.error(`Error extending lock for ${lockKey}: ${error.message}`, error.stack);
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
isLocked(key_1) {
|
|
206
|
+
return __awaiter(this, arguments, void 0, function* (key, keyPrefix = 'lock') {
|
|
207
|
+
const redis = yield this.getRedis();
|
|
208
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
209
|
+
try {
|
|
210
|
+
const exists = yield redis.exists(lockKey);
|
|
211
|
+
return exists === 1;
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
this.logger.error(`Error checking lock existence for ${lockKey}: ${error.message}`, error.stack);
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
withLock(key_1, fn_1) {
|
|
220
|
+
return __awaiter(this, arguments, void 0, function* (key, fn, options = {}) {
|
|
221
|
+
const lockResult = yield this.acquireLock(key, options);
|
|
222
|
+
if (!lockResult.acquired) {
|
|
223
|
+
this.logger.warn(`Could not acquire lock for ${key}, skipping execution`);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
return yield fn();
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
this.logger.error(`Error executing function with lock ${key}:`, error);
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
if (lockResult.autoExtendTimer) {
|
|
235
|
+
clearInterval(lockResult.autoExtendTimer);
|
|
236
|
+
}
|
|
237
|
+
yield this.releaseLock(key, lockResult.lockValue, options.keyPrefix);
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
getLockInfo(key_1) {
|
|
242
|
+
return __awaiter(this, arguments, void 0, function* (key, keyPrefix = 'lock') {
|
|
243
|
+
const redis = yield this.getRedis();
|
|
244
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
245
|
+
try {
|
|
246
|
+
const [value, ttl] = yield Promise.all([
|
|
247
|
+
redis.get(lockKey),
|
|
248
|
+
redis.pttl(lockKey),
|
|
249
|
+
]);
|
|
250
|
+
return {
|
|
251
|
+
value,
|
|
252
|
+
ttl: ttl >= 0 ? ttl : null,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
this.logger.error(`Error getting lock info for ${lockKey}: ${error.message}`, error.stack);
|
|
257
|
+
return { value: null, ttl: null };
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
forceRelease(key_1) {
|
|
262
|
+
return __awaiter(this, arguments, void 0, function* (key, keyPrefix = 'lock') {
|
|
263
|
+
const redis = yield this.getRedis();
|
|
264
|
+
const lockKey = this.buildLockKey(key, keyPrefix);
|
|
265
|
+
try {
|
|
266
|
+
const result = yield redis.del(lockKey);
|
|
267
|
+
if (result === 1) {
|
|
268
|
+
this.logger.debug(`Lock forcefully released: ${lockKey}`);
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
this.logger.debug(`No lock found to forcefully release: ${lockKey}`);
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
this.logger.error(`Error force releasing lock for ${lockKey}: ${error.message}`, error.stack);
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
getRedisOptions() {
|
|
283
|
+
return Object.assign({}, this.redisOptions);
|
|
284
|
+
}
|
|
285
|
+
getRedis() {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
if (this.redis && this.isConnected) {
|
|
288
|
+
return this.redis;
|
|
289
|
+
}
|
|
290
|
+
if (this.isConnecting && this.connectionPromise) {
|
|
291
|
+
return this.connectionPromise;
|
|
292
|
+
}
|
|
293
|
+
this.isConnecting = true;
|
|
294
|
+
this.connectionPromise = this.createRedisConnection()
|
|
295
|
+
.then((redis) => {
|
|
296
|
+
this.isConnecting = false;
|
|
297
|
+
this.isConnected = true;
|
|
298
|
+
return redis;
|
|
299
|
+
})
|
|
300
|
+
.catch((error) => {
|
|
301
|
+
this.isConnecting = false;
|
|
302
|
+
this.connectionPromise = null;
|
|
303
|
+
throw error;
|
|
304
|
+
});
|
|
305
|
+
return this.connectionPromise;
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
createRedisConnection() {
|
|
309
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
310
|
+
return new Promise((resolve, reject) => {
|
|
311
|
+
const redis = new ioredis_1.default({
|
|
312
|
+
host: this.redisOptions.host,
|
|
313
|
+
port: this.redisOptions.port,
|
|
314
|
+
password: this.redisOptions.password,
|
|
315
|
+
db: this.redisOptions.db,
|
|
316
|
+
retryStrategy: (times) => {
|
|
317
|
+
this.logger.warn(`Redis connection retry attempt ${times}`);
|
|
318
|
+
if (times > (this.redisOptions.maxRetries || 10)) {
|
|
319
|
+
this.logger.error('Max retries reached, giving up on Redis connection');
|
|
320
|
+
this.isConnected = false;
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
const delay = Math.min(times * (this.redisOptions.retryDelay || 2000), 30000);
|
|
324
|
+
this.logger.log(`Retrying Redis connection in ${delay}ms`);
|
|
325
|
+
return delay;
|
|
326
|
+
},
|
|
327
|
+
reconnectOnError: this.redisOptions.reconnectOnError,
|
|
328
|
+
});
|
|
329
|
+
const onConnect = () => {
|
|
330
|
+
this.logger.log(`Connected to Redis at ${this.redisOptions.host}:${this.redisOptions.port}`);
|
|
331
|
+
this.isConnected = true;
|
|
332
|
+
resolve(redis);
|
|
333
|
+
};
|
|
334
|
+
const onError = (err) => {
|
|
335
|
+
this.logger.error('Redis connection error:', err.message);
|
|
336
|
+
if (!this.isConnected) {
|
|
337
|
+
reject(err);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
redis.once('connect', onConnect);
|
|
341
|
+
redis.once('error', onError);
|
|
342
|
+
this.redis = redis;
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
generateLockValue() {
|
|
347
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 15)}-${process.pid}`;
|
|
348
|
+
}
|
|
349
|
+
buildLockKey(key, prefix = 'lock') {
|
|
350
|
+
return `${prefix}:${key}`;
|
|
351
|
+
}
|
|
352
|
+
sleep(ms) {
|
|
353
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
exports.RedisLockService = RedisLockService;
|
|
357
|
+
RedisLockService.globalInstance = null;
|
|
358
|
+
RedisLockService.globalConfig = null;
|
|
359
|
+
exports.RedisLockService = RedisLockService = RedisLockService_1 = __decorate([
|
|
360
|
+
(0, common_1.Injectable)(),
|
|
361
|
+
__metadata("design:paramtypes", [Object])
|
|
362
|
+
], RedisLockService);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CronOptions } from '@nestjs/schedule';
|
|
2
|
+
export interface ScheduleWithLockOptions {
|
|
3
|
+
lockTtl?: number;
|
|
4
|
+
retryCount?: number;
|
|
5
|
+
retryDelay?: number;
|
|
6
|
+
useExponentialBackoff?: boolean;
|
|
7
|
+
maxRetryDelay?: number;
|
|
8
|
+
logExecution?: boolean;
|
|
9
|
+
lockKeyPrefix?: string;
|
|
10
|
+
autoExtendLock?: number;
|
|
11
|
+
onSuccess?: (duration: number) => void | Promise<void>;
|
|
12
|
+
onError?: (error: Error) => void | Promise<void>;
|
|
13
|
+
skipIfLocked?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function WorkerCron(cronTime: string | Date, options?: CronOptions): MethodDecorator;
|
|
16
|
+
export declare function WorkerInterval(timeout: number, name?: string): MethodDecorator;
|
|
17
|
+
export declare function WorkerTimeout(timeout: number, name?: string): MethodDecorator;
|
|
18
|
+
export declare function WorkerCronWithLock(cronTime: string | Date, lockKeyOrOptions: string | ScheduleWithLockOptions, lockTtl?: number, cronOptions?: CronOptions): MethodDecorator;
|
|
19
|
+
export declare function WorkerIntervalWithLock(timeout: number, lockKeyOrOptions: string | ScheduleWithLockOptions, lockTtl?: number): MethodDecorator;
|
|
20
|
+
export declare function WorkerTimeoutWithLock(timeout: number, lockKeyOrOptions: string | ScheduleWithLockOptions, lockTtl?: number): MethodDecorator;
|
|
21
|
+
export declare function WorkerCronSmart(cronTime: string | Date, options?: ScheduleWithLockOptions, cronOptions?: CronOptions): MethodDecorator;
|
|
22
|
+
export declare function WorkerIntervalSmart(timeout: number, options?: ScheduleWithLockOptions): MethodDecorator;
|
|
23
|
+
export declare function WorkerCronAdvanced(cronTime: string | Date, lockKeyOrOptions: string | ScheduleWithLockOptions, options?: ScheduleWithLockOptions, cronOptions?: CronOptions): MethodDecorator;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare class SchedulerExamples {
|
|
2
|
+
dailyBasicTask(): Promise<void>;
|
|
3
|
+
sixHourlyTaskWithLock(): Promise<void>;
|
|
4
|
+
twoHourlyTaskWithOptions(): Promise<void>;
|
|
5
|
+
hourlySmartTask(): Promise<void>;
|
|
6
|
+
nightlySmartTask(): Promise<void>;
|
|
7
|
+
weeklyReportTask(): Promise<void>;
|
|
8
|
+
monthlyReportTask(): Promise<void>;
|
|
9
|
+
checkStatusWithLock(): Promise<void>;
|
|
10
|
+
checkStatusWithOptions(): Promise<void>;
|
|
11
|
+
checkStatusSmart(): Promise<void>;
|
|
12
|
+
syncDataSmart(): Promise<void>;
|
|
13
|
+
initializeWithLock(): Promise<void>;
|
|
14
|
+
warmUpWithOptions(): Promise<void>;
|
|
15
|
+
weekdayTask(): Promise<void>;
|
|
16
|
+
processDataWithRetry(): Promise<void>;
|
|
17
|
+
private performDataProcessing;
|
|
18
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
12
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
13
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
14
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
15
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
16
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
17
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.SchedulerExamples = void 0;
|
|
22
|
+
const common_1 = require("@nestjs/common");
|
|
23
|
+
const schedule_decorator_1 = require("./schedule.decorator");
|
|
24
|
+
let SchedulerExamples = class SchedulerExamples {
|
|
25
|
+
dailyBasicTask() {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
console.log('执行基本定时任务');
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
sixHourlyTaskWithLock() {
|
|
31
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
32
|
+
console.log('执行带锁的定时任务');
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
twoHourlyTaskWithOptions() {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
console.log('执行带选项的定时任务');
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
hourlySmartTask() {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
console.log('执行智能定时任务');
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
nightlySmartTask() {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
console.log('执行夜间智能定时任务');
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
weeklyReportTask() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
console.log('生成周报');
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
monthlyReportTask() {
|
|
56
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
console.log('生成月报');
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
checkStatusWithLock() {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
console.log('检查系统状态');
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
checkStatusWithOptions() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
console.log('检查系统状态(带选项)');
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
checkStatusSmart() {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
console.log('智能检查系统状态');
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
syncDataSmart() {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
console.log('智能同步数据');
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
initializeWithLock() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
console.log('初始化系统');
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
warmUpWithOptions() {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
console.log('系统预热');
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
weekdayTask() {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
console.log('执行工作日任务');
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
processDataWithRetry() {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
console.log('开始处理数据');
|
|
98
|
+
try {
|
|
99
|
+
yield this.performDataProcessing();
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error('数据处理失败:', error);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
performDataProcessing() {
|
|
108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
+
console.log('数据处理完成');
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
exports.SchedulerExamples = SchedulerExamples;
|
|
114
|
+
__decorate([
|
|
115
|
+
(0, schedule_decorator_1.WorkerCron)('0 0 * * *'),
|
|
116
|
+
__metadata("design:type", Function),
|
|
117
|
+
__metadata("design:paramtypes", []),
|
|
118
|
+
__metadata("design:returntype", Promise)
|
|
119
|
+
], SchedulerExamples.prototype, "dailyBasicTask", null);
|
|
120
|
+
__decorate([
|
|
121
|
+
(0, schedule_decorator_1.WorkerCronWithLock)('0 */6 * * *', 'six-hourly-task', 3600000),
|
|
122
|
+
__metadata("design:type", Function),
|
|
123
|
+
__metadata("design:paramtypes", []),
|
|
124
|
+
__metadata("design:returntype", Promise)
|
|
125
|
+
], SchedulerExamples.prototype, "sixHourlyTaskWithLock", null);
|
|
126
|
+
__decorate([
|
|
127
|
+
(0, schedule_decorator_1.WorkerCronWithLock)('0 */2 * * *', {
|
|
128
|
+
lockTtl: 5400000,
|
|
129
|
+
logExecution: true,
|
|
130
|
+
onError: (error) => console.error('任务执行失败:', error),
|
|
131
|
+
}),
|
|
132
|
+
__metadata("design:type", Function),
|
|
133
|
+
__metadata("design:paramtypes", []),
|
|
134
|
+
__metadata("design:returntype", Promise)
|
|
135
|
+
], SchedulerExamples.prototype, "twoHourlyTaskWithOptions", null);
|
|
136
|
+
__decorate([
|
|
137
|
+
(0, schedule_decorator_1.WorkerCronSmart)('0 * * * *'),
|
|
138
|
+
__metadata("design:type", Function),
|
|
139
|
+
__metadata("design:paramtypes", []),
|
|
140
|
+
__metadata("design:returntype", Promise)
|
|
141
|
+
], SchedulerExamples.prototype, "hourlySmartTask", null);
|
|
142
|
+
__decorate([
|
|
143
|
+
(0, schedule_decorator_1.WorkerCronSmart)('30 2 * * *', {
|
|
144
|
+
lockTtl: 3600000,
|
|
145
|
+
logExecution: true,
|
|
146
|
+
onError: (error) => console.error('夜间任务失败:', error),
|
|
147
|
+
onSuccess: (duration) => console.log(`夜间任务完成,耗时: ${duration}ms`),
|
|
148
|
+
}),
|
|
149
|
+
__metadata("design:type", Function),
|
|
150
|
+
__metadata("design:paramtypes", []),
|
|
151
|
+
__metadata("design:returntype", Promise)
|
|
152
|
+
], SchedulerExamples.prototype, "nightlySmartTask", null);
|
|
153
|
+
__decorate([
|
|
154
|
+
(0, schedule_decorator_1.WorkerCronAdvanced)('0 0 * * 0', 'weekly-report', {
|
|
155
|
+
lockTtl: 7200000,
|
|
156
|
+
logExecution: true,
|
|
157
|
+
onError: (error) => console.error('周报生成失败:', error),
|
|
158
|
+
}),
|
|
159
|
+
__metadata("design:type", Function),
|
|
160
|
+
__metadata("design:paramtypes", []),
|
|
161
|
+
__metadata("design:returntype", Promise)
|
|
162
|
+
], SchedulerExamples.prototype, "weeklyReportTask", null);
|
|
163
|
+
__decorate([
|
|
164
|
+
(0, schedule_decorator_1.WorkerCronAdvanced)('0 0 1 * *', {
|
|
165
|
+
lockTtl: 14400000,
|
|
166
|
+
logExecution: true,
|
|
167
|
+
onError: (error) => console.error('月报生成失败:', error),
|
|
168
|
+
onSuccess: (duration) => console.log(`月报生成完成,耗时: ${duration}ms`),
|
|
169
|
+
}),
|
|
170
|
+
__metadata("design:type", Function),
|
|
171
|
+
__metadata("design:paramtypes", []),
|
|
172
|
+
__metadata("design:returntype", Promise)
|
|
173
|
+
], SchedulerExamples.prototype, "monthlyReportTask", null);
|
|
174
|
+
__decorate([
|
|
175
|
+
(0, schedule_decorator_1.WorkerIntervalWithLock)(60000, 'status-check'),
|
|
176
|
+
__metadata("design:type", Function),
|
|
177
|
+
__metadata("design:paramtypes", []),
|
|
178
|
+
__metadata("design:returntype", Promise)
|
|
179
|
+
], SchedulerExamples.prototype, "checkStatusWithLock", null);
|
|
180
|
+
__decorate([
|
|
181
|
+
(0, schedule_decorator_1.WorkerIntervalWithLock)(300000, {
|
|
182
|
+
lockTtl: 240000,
|
|
183
|
+
logExecution: true,
|
|
184
|
+
onError: (error) => console.error('状态检查失败:', error),
|
|
185
|
+
}),
|
|
186
|
+
__metadata("design:type", Function),
|
|
187
|
+
__metadata("design:paramtypes", []),
|
|
188
|
+
__metadata("design:returntype", Promise)
|
|
189
|
+
], SchedulerExamples.prototype, "checkStatusWithOptions", null);
|
|
190
|
+
__decorate([
|
|
191
|
+
(0, schedule_decorator_1.WorkerIntervalSmart)(120000),
|
|
192
|
+
__metadata("design:type", Function),
|
|
193
|
+
__metadata("design:paramtypes", []),
|
|
194
|
+
__metadata("design:returntype", Promise)
|
|
195
|
+
], SchedulerExamples.prototype, "checkStatusSmart", null);
|
|
196
|
+
__decorate([
|
|
197
|
+
(0, schedule_decorator_1.WorkerIntervalSmart)(600000, {
|
|
198
|
+
lockTtl: 480000,
|
|
199
|
+
logExecution: true,
|
|
200
|
+
onError: (error) => console.error('数据同步失败:', error),
|
|
201
|
+
onSuccess: (duration) => console.log(`数据同步完成,耗时: ${duration}ms`),
|
|
202
|
+
}),
|
|
203
|
+
__metadata("design:type", Function),
|
|
204
|
+
__metadata("design:paramtypes", []),
|
|
205
|
+
__metadata("design:returntype", Promise)
|
|
206
|
+
], SchedulerExamples.prototype, "syncDataSmart", null);
|
|
207
|
+
__decorate([
|
|
208
|
+
(0, schedule_decorator_1.WorkerTimeoutWithLock)(5000, 'init-task', 10000),
|
|
209
|
+
__metadata("design:type", Function),
|
|
210
|
+
__metadata("design:paramtypes", []),
|
|
211
|
+
__metadata("design:returntype", Promise)
|
|
212
|
+
], SchedulerExamples.prototype, "initializeWithLock", null);
|
|
213
|
+
__decorate([
|
|
214
|
+
(0, schedule_decorator_1.WorkerTimeoutWithLock)(10000, {
|
|
215
|
+
lockTtl: 30000,
|
|
216
|
+
logExecution: true,
|
|
217
|
+
onError: (error) => console.error('预热任务失败:', error),
|
|
218
|
+
}),
|
|
219
|
+
__metadata("design:type", Function),
|
|
220
|
+
__metadata("design:paramtypes", []),
|
|
221
|
+
__metadata("design:returntype", Promise)
|
|
222
|
+
], SchedulerExamples.prototype, "warmUpWithOptions", null);
|
|
223
|
+
__decorate([
|
|
224
|
+
(0, schedule_decorator_1.WorkerCronSmart)('0 9 * * 1-5', {
|
|
225
|
+
lockTtl: 3600000,
|
|
226
|
+
logExecution: true,
|
|
227
|
+
onError: (error) => console.error('工作日任务失败:', error),
|
|
228
|
+
onSuccess: (duration) => {
|
|
229
|
+
console.log(`工作日任务完成,耗时: ${duration}ms`);
|
|
230
|
+
},
|
|
231
|
+
}),
|
|
232
|
+
__metadata("design:type", Function),
|
|
233
|
+
__metadata("design:paramtypes", []),
|
|
234
|
+
__metadata("design:returntype", Promise)
|
|
235
|
+
], SchedulerExamples.prototype, "weekdayTask", null);
|
|
236
|
+
__decorate([
|
|
237
|
+
(0, schedule_decorator_1.WorkerCronSmart)('0 */30 * * * *', {
|
|
238
|
+
lockTtl: 1500000,
|
|
239
|
+
logExecution: true,
|
|
240
|
+
onError: (error) => __awaiter(void 0, void 0, void 0, function* () {
|
|
241
|
+
console.error('数据处理任务失败:', error);
|
|
242
|
+
}),
|
|
243
|
+
onSuccess: (duration) => {
|
|
244
|
+
if (duration > 60000) {
|
|
245
|
+
console.warn(`数据处理任务耗时较长: ${duration}ms`);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
}),
|
|
249
|
+
__metadata("design:type", Function),
|
|
250
|
+
__metadata("design:paramtypes", []),
|
|
251
|
+
__metadata("design:returntype", Promise)
|
|
252
|
+
], SchedulerExamples.prototype, "processDataWithRetry", null);
|
|
253
|
+
exports.SchedulerExamples = SchedulerExamples = __decorate([
|
|
254
|
+
(0, common_1.Injectable)()
|
|
255
|
+
], SchedulerExamples);
|