@lyrolab/nest-shared 1.1.0 → 1.2.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/CHANGELOG.md +2 -2
- package/dist/queue/decorators/queue.decorator.d.ts +5 -1
- package/dist/queue/decorators/queue.decorator.js.map +1 -1
- package/dist/queue/processors/queue.processor.d.ts +6 -2
- package/dist/queue/processors/queue.processor.js +43 -5
- package/dist/queue/processors/queue.processor.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# [1.
|
|
1
|
+
# [1.2.0](https://github.com/lyrolab/nest-shared/compare/v1.1.1...v1.2.0) (2025-04-13)
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
### Features
|
|
5
5
|
|
|
6
|
-
* add
|
|
6
|
+
* add cron support ([5a1a4e0](https://github.com/lyrolab/nest-shared/commit/5a1a4e057548f34cce952ee5be774f8f26e42918))
|
|
7
7
|
|
|
8
8
|
## [1.0.1](https://github.com/lyrolab/nest-shared/compare/v1.0.0...v1.0.1) (2025-04-10)
|
|
9
9
|
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type JobProcessorMetadata = {
|
|
2
|
+
name: string;
|
|
3
|
+
cron?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const JobProcessor: import("@nestjs/core").DiscoverableDecorator<string | JobProcessorMetadata>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.decorator.js","sourceRoot":"","sources":["../../../src/queue/decorators/queue.decorator.ts"],"names":[],"mappings":";;;AAAA,uCAA+C;
|
|
1
|
+
{"version":3,"file":"queue.decorator.js","sourceRoot":"","sources":["../../../src/queue/decorators/queue.decorator.ts"],"names":[],"mappings":";;;AAAA,uCAA+C;AAelC,QAAA,YAAY,GAAG,uBAAgB,CAAC,eAAe,EAEzD,CAAA"}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { WorkerHost } from "@nestjs/bullmq";
|
|
2
2
|
import { DiscoveryService } from "@nestjs/core";
|
|
3
3
|
import { Job, Queue } from "bullmq";
|
|
4
|
-
|
|
4
|
+
import { OnModuleInit } from "@nestjs/common";
|
|
5
|
+
export declare class QueueProcessor extends WorkerHost implements OnModuleInit {
|
|
5
6
|
private readonly discoveryService;
|
|
6
7
|
private readonly queue;
|
|
7
8
|
constructor(discoveryService: DiscoveryService, queue: Queue);
|
|
9
|
+
onModuleInit(): Promise<void>;
|
|
8
10
|
process(job: Job): Promise<void>;
|
|
9
|
-
private
|
|
11
|
+
private getJobConfigurations;
|
|
12
|
+
private scheduleJobs;
|
|
10
13
|
private shouldProcessJob;
|
|
14
|
+
private getJobProcessorByName;
|
|
11
15
|
}
|
|
@@ -18,6 +18,7 @@ const core_1 = require("@nestjs/core");
|
|
|
18
18
|
const bullmq_2 = require("bullmq");
|
|
19
19
|
const queue_decorator_1 = require("../decorators/queue.decorator");
|
|
20
20
|
const queue_constants_1 = require("../queue.constants");
|
|
21
|
+
const common_1 = require("@nestjs/common");
|
|
21
22
|
let QueueProcessor = class QueueProcessor extends bullmq_1.WorkerHost {
|
|
22
23
|
discoveryService;
|
|
23
24
|
queue;
|
|
@@ -26,8 +27,12 @@ let QueueProcessor = class QueueProcessor extends bullmq_1.WorkerHost {
|
|
|
26
27
|
this.discoveryService = discoveryService;
|
|
27
28
|
this.queue = queue;
|
|
28
29
|
}
|
|
30
|
+
async onModuleInit() {
|
|
31
|
+
const jobConfigs = this.getJobConfigurations();
|
|
32
|
+
await this.scheduleJobs(jobConfigs);
|
|
33
|
+
}
|
|
29
34
|
async process(job) {
|
|
30
|
-
const jobProcessor = this.
|
|
35
|
+
const jobProcessor = this.getJobProcessorByName(job.name);
|
|
31
36
|
if (!jobProcessor)
|
|
32
37
|
return;
|
|
33
38
|
const shouldProcess = await this.shouldProcessJob(job);
|
|
@@ -40,21 +45,54 @@ let QueueProcessor = class QueueProcessor extends bullmq_1.WorkerHost {
|
|
|
40
45
|
console.error(error);
|
|
41
46
|
}
|
|
42
47
|
}
|
|
43
|
-
|
|
48
|
+
getJobConfigurations() {
|
|
44
49
|
return this.discoveryService
|
|
45
50
|
.getProviders({ metadataKey: queue_decorator_1.JobProcessor.KEY })
|
|
46
|
-
.
|
|
51
|
+
.map((provider) => {
|
|
52
|
+
const metadata = this.discoveryService.getMetadataByDecorator(queue_decorator_1.JobProcessor, provider);
|
|
53
|
+
if (!metadata)
|
|
54
|
+
return null;
|
|
55
|
+
if (typeof metadata === "string") {
|
|
56
|
+
return { name: metadata };
|
|
57
|
+
}
|
|
58
|
+
return { name: metadata.name, cron: metadata.cron };
|
|
59
|
+
})
|
|
60
|
+
.filter((job) => job !== null);
|
|
61
|
+
}
|
|
62
|
+
async scheduleJobs(jobConfigs) {
|
|
63
|
+
const schedulingPromises = jobConfigs
|
|
64
|
+
.filter((job) => job.cron)
|
|
65
|
+
.map(async ({ name, cron }) => {
|
|
66
|
+
const existingSchedulers = await this.queue.getJobSchedulers();
|
|
67
|
+
const outdatedSchedulers = existingSchedulers.filter((scheduler) => scheduler.name === name && scheduler.pattern !== cron);
|
|
68
|
+
await Promise.allSettled(outdatedSchedulers.map((scheduler) => this.queue.removeJobScheduler(scheduler.key)));
|
|
69
|
+
await this.queue.upsertJobScheduler(name, { pattern: cron }, { name });
|
|
70
|
+
});
|
|
71
|
+
await Promise.all(schedulingPromises);
|
|
47
72
|
}
|
|
48
73
|
async shouldProcessJob(job) {
|
|
49
74
|
if (!job.repeatJobKey)
|
|
50
75
|
return true;
|
|
51
76
|
const activeJobs = await this.queue.getActive();
|
|
52
|
-
const
|
|
53
|
-
return
|
|
77
|
+
const activeJobsOfSameType = activeJobs.filter((activeJob) => activeJob.name === job.name && activeJob.id !== job.id);
|
|
78
|
+
return activeJobsOfSameType.length === 0;
|
|
79
|
+
}
|
|
80
|
+
getJobProcessorByName(jobName) {
|
|
81
|
+
const providerWithProcessor = this.discoveryService
|
|
82
|
+
.getProviders({ metadataKey: queue_decorator_1.JobProcessor.KEY })
|
|
83
|
+
.find((provider) => {
|
|
84
|
+
const metadata = this.discoveryService.getMetadataByDecorator(queue_decorator_1.JobProcessor, provider);
|
|
85
|
+
if (!metadata)
|
|
86
|
+
return false;
|
|
87
|
+
const name = typeof metadata === "string" ? metadata : metadata.name;
|
|
88
|
+
return name === jobName;
|
|
89
|
+
});
|
|
90
|
+
return providerWithProcessor?.instance ?? null;
|
|
54
91
|
}
|
|
55
92
|
};
|
|
56
93
|
exports.QueueProcessor = QueueProcessor;
|
|
57
94
|
exports.QueueProcessor = QueueProcessor = __decorate([
|
|
95
|
+
(0, common_1.Injectable)(),
|
|
58
96
|
(0, bullmq_1.Processor)(queue_constants_1.DEFAULT_QUEUE, { concurrency: 100 }),
|
|
59
97
|
__param(1, (0, bullmq_1.InjectQueue)(queue_constants_1.DEFAULT_QUEUE)),
|
|
60
98
|
__metadata("design:paramtypes", [core_1.DiscoveryService,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queue.processor.js","sourceRoot":"","sources":["../../../src/queue/processors/queue.processor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmE;AACnE,uCAA+C;AAC/C,mCAAmC;
|
|
1
|
+
{"version":3,"file":"queue.processor.js","sourceRoot":"","sources":["../../../src/queue/processors/queue.processor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmE;AACnE,uCAA+C;AAC/C,mCAAmC;AAEnC,mEAA4D;AAC5D,wDAAkD;AAClD,2CAAyD;AAIlD,IAAM,cAAc,GAApB,MAAM,cAAe,SAAQ,mBAAU;IAEzB;IAC4B;IAF/C,YACmB,gBAAkC,EACN,KAAY;QAEzD,KAAK,EAAE,CAAA;QAHU,qBAAgB,GAAhB,gBAAgB,CAAkB;QACN,UAAK,GAAL,KAAK,CAAO;IAG3D,CAAC;IAED,KAAK,CAAC,YAAY;QACh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