@my-nestjs-kit/queues 0.0.0-stage → 0.1.0-dev.20261129020438
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/LICENSE +21 -0
- package/README.md +24 -2
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.js +18 -0
- package/dist/decorators/processor-meta.decorator.d.ts +10 -0
- package/dist/decorators/processor-meta.decorator.js +19 -0
- package/dist/decorators/queue-service.decorator.d.ts +11 -0
- package/dist/decorators/queue-service.decorator.js +15 -0
- package/dist/dynamic-queues/dynamic-queue-names.d.ts +12 -0
- package/dist/dynamic-queues/dynamic-queue-names.js +49 -0
- package/dist/dynamic-queues/dynamic-queue-processor.decorator.d.ts +23 -0
- package/dist/dynamic-queues/dynamic-queue-processor.decorator.js +29 -0
- package/dist/dynamic-queues/dynamic-queue-processors.explorer.d.ts +12 -0
- package/dist/dynamic-queues/dynamic-queue-processors.explorer.js +57 -0
- package/dist/dynamic-queues/dynamic-queues-channel.d.ts +21 -0
- package/dist/dynamic-queues/dynamic-queues-channel.js +55 -0
- package/dist/dynamic-queues/dynamic-queues-factory.d.ts +88 -0
- package/dist/dynamic-queues/dynamic-queues-factory.js +162 -0
- package/dist/dynamic-queues/dynamic-queues-module.options.d.ts +76 -0
- package/dist/dynamic-queues/dynamic-queues-module.options.js +2 -0
- package/dist/dynamic-queues/dynamic-queues-state.d.ts +79 -0
- package/dist/dynamic-queues/dynamic-queues-state.js +142 -0
- package/dist/dynamic-queues/dynamic-queues.constants.d.ts +10 -0
- package/dist/dynamic-queues/dynamic-queues.constants.js +14 -0
- package/dist/dynamic-queues/dynamic-queues.manager.d.ts +85 -0
- package/dist/dynamic-queues/dynamic-queues.manager.js +404 -0
- package/dist/dynamic-queues/dynamic-queues.module.d.ts +16 -0
- package/dist/dynamic-queues/dynamic-queues.module.js +76 -0
- package/dist/dynamic-queues/dynamic-queues.registry.d.ts +18 -0
- package/dist/dynamic-queues/dynamic-queues.registry.js +55 -0
- package/dist/dynamic-queues/dynamic-queues.types.d.ts +81 -0
- package/dist/dynamic-queues/dynamic-queues.types.js +2 -0
- package/dist/dynamic-queues/index.d.ts +13 -0
- package/dist/dynamic-queues/index.js +33 -0
- package/dist/dynamic-queues/proxy-queues.binder.d.ts +17 -0
- package/dist/dynamic-queues/proxy-queues.binder.js +63 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +30 -0
- package/dist/jobs.types.d.ts +4 -0
- package/dist/jobs.types.js +2 -0
- package/dist/middlewares/middlewares.types.d.ts +10 -0
- package/dist/middlewares/middlewares.types.js +6 -0
- package/dist/process/index.d.ts +1 -0
- package/dist/process/index.js +17 -0
- package/dist/process/process.decorator.d.ts +13 -0
- package/dist/process/process.decorator.js +45 -0
- package/dist/processor-base.abstract.d.ts +39 -0
- package/dist/processor-base.abstract.js +125 -0
- package/dist/queue-service.base.d.ts +28 -0
- package/dist/queue-service.base.js +73 -0
- package/dist/queues.constants.d.ts +3 -0
- package/dist/queues.constants.js +6 -0
- package/dist/queues.module.d.ts +11 -0
- package/dist/queues.module.js +58 -0
- package/dist/queues.types.d.ts +99 -0
- package/dist/queues.types.js +57 -0
- package/dist/types/index.d.ts +17 -0
- package/dist/types/index.js +2 -0
- package/package.json +102 -3
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.DynamicQueueEvent = exports.DYNAMIC_JOB_ANY = exports.DYNAMIC_QUEUES_CHANNEL = void 0;
|
|
18
|
+
__exportStar(require("./dynamic-queues.types"), exports);
|
|
19
|
+
__exportStar(require("./dynamic-queue-names"), exports);
|
|
20
|
+
__exportStar(require("./dynamic-queues-state"), exports);
|
|
21
|
+
__exportStar(require("./dynamic-queues-factory"), exports);
|
|
22
|
+
__exportStar(require("./dynamic-queues-module.options"), exports);
|
|
23
|
+
__exportStar(require("./dynamic-queue-processor.decorator"), exports);
|
|
24
|
+
__exportStar(require("./dynamic-queues.registry"), exports);
|
|
25
|
+
__exportStar(require("./dynamic-queue-processors.explorer"), exports);
|
|
26
|
+
__exportStar(require("./dynamic-queues.manager"), exports);
|
|
27
|
+
__exportStar(require("./dynamic-queues-channel"), exports);
|
|
28
|
+
__exportStar(require("./proxy-queues.binder"), exports);
|
|
29
|
+
__exportStar(require("./dynamic-queues.module"), exports);
|
|
30
|
+
var dynamic_queues_constants_1 = require("./dynamic-queues.constants");
|
|
31
|
+
Object.defineProperty(exports, "DYNAMIC_QUEUES_CHANNEL", { enumerable: true, get: function () { return dynamic_queues_constants_1.DYNAMIC_QUEUES_CHANNEL; } });
|
|
32
|
+
Object.defineProperty(exports, "DYNAMIC_JOB_ANY", { enumerable: true, get: function () { return dynamic_queues_constants_1.DYNAMIC_JOB_ANY; } });
|
|
33
|
+
Object.defineProperty(exports, "DynamicQueueEvent", { enumerable: true, get: function () { return dynamic_queues_constants_1.DynamicQueueEvent; } });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
+
import { DiscoveryService } from '@nestjs/core';
|
|
3
|
+
import type { DynamicQueuesModuleOptions } from './dynamic-queues-module.options';
|
|
4
|
+
/**
|
|
5
|
+
* With `bindProxies`, gives every `@QueueService({ hasProxy: true })` provider a
|
|
6
|
+
* `proxy__<type>` queue; the service routes jobs with `__proxy.enabled` there.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ProxyQueuesBinder implements OnApplicationBootstrap, OnApplicationShutdown {
|
|
9
|
+
private readonly discovery;
|
|
10
|
+
private readonly options;
|
|
11
|
+
private readonly logger;
|
|
12
|
+
private readonly bound;
|
|
13
|
+
constructor(discovery: DiscoveryService, options: DynamicQueuesModuleOptions);
|
|
14
|
+
onApplicationBootstrap(): void;
|
|
15
|
+
onApplicationShutdown(): Promise<void>;
|
|
16
|
+
bind(): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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 __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ProxyQueuesBinder = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const core_1 = require("@nestjs/core");
|
|
18
|
+
const queue_service_decorator_1 = require("../decorators/queue-service.decorator");
|
|
19
|
+
const dynamic_queues_constants_1 = require("./dynamic-queues.constants");
|
|
20
|
+
const dynamic_queue_names_1 = require("./dynamic-queue-names");
|
|
21
|
+
/**
|
|
22
|
+
* With `bindProxies`, gives every `@QueueService({ hasProxy: true })` provider a
|
|
23
|
+
* `proxy__<type>` queue; the service routes jobs with `__proxy.enabled` there.
|
|
24
|
+
*/
|
|
25
|
+
let ProxyQueuesBinder = class ProxyQueuesBinder {
|
|
26
|
+
constructor(discovery, options) {
|
|
27
|
+
this.discovery = discovery;
|
|
28
|
+
this.options = options;
|
|
29
|
+
this.logger = new common_1.Logger('ProxyQueues');
|
|
30
|
+
this.bound = [];
|
|
31
|
+
}
|
|
32
|
+
onApplicationBootstrap() {
|
|
33
|
+
if (this.options.bindProxies)
|
|
34
|
+
this.bind();
|
|
35
|
+
}
|
|
36
|
+
async onApplicationShutdown() {
|
|
37
|
+
await Promise.allSettled(this.bound.map((q) => q.close()));
|
|
38
|
+
}
|
|
39
|
+
bind() {
|
|
40
|
+
for (const { instance } of this.discovery.getProviders()) {
|
|
41
|
+
if (!instance || typeof instance !== 'object')
|
|
42
|
+
continue;
|
|
43
|
+
const meta = (0, queue_service_decorator_1.getQueueServiceMeta)(instance);
|
|
44
|
+
if (!meta?.hasProxy)
|
|
45
|
+
continue;
|
|
46
|
+
if (typeof instance.registerProxy !== 'function') {
|
|
47
|
+
this.logger.warn(`${instance.constructor.name} has hasProxy but no registerProxy()`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const name = (0, dynamic_queue_names_1.toProxyQueueName)(meta.type ?? meta.queue);
|
|
51
|
+
const queue = this.options.factory.createQueue(name);
|
|
52
|
+
instance.registerProxy(queue);
|
|
53
|
+
this.bound.push(queue);
|
|
54
|
+
this.logger.log(`Bound ${name} to ${instance.constructor.name}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.ProxyQueuesBinder = ProxyQueuesBinder;
|
|
59
|
+
exports.ProxyQueuesBinder = ProxyQueuesBinder = __decorate([
|
|
60
|
+
(0, common_1.Injectable)(),
|
|
61
|
+
__param(1, (0, common_1.Inject)(dynamic_queues_constants_1.DYNAMIC_QUEUES_MODULE_OPTIONS)),
|
|
62
|
+
__metadata("design:paramtypes", [core_1.DiscoveryService, Object])
|
|
63
|
+
], ProxyQueuesBinder);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './process';
|
|
2
|
+
export * from './queues.types';
|
|
3
|
+
export * from './jobs.types';
|
|
4
|
+
export * from './types';
|
|
5
|
+
export * from './decorators';
|
|
6
|
+
export * from './middlewares/middlewares.types';
|
|
7
|
+
export * from './queue-service.base';
|
|
8
|
+
export * from './processor-base.abstract';
|
|
9
|
+
export * from './queues.module';
|
|
10
|
+
export * from './dynamic-queues';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @my-nestjs-kit/queues — BullMQ queue service / processor bases with @Process handlers, and per-id dynamic
|
|
3
|
+
// queues created at runtime (formerly @my-nestjs-kit/nest-kit/dynamic-queues).
|
|
4
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
+
if (k2 === undefined) k2 = k;
|
|
6
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(o, k2, desc);
|
|
11
|
+
}) : (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
o[k2] = m[k];
|
|
14
|
+
}));
|
|
15
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
16
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
17
|
+
};
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
// queue bases (formerly in the nest-kit root entry)
|
|
20
|
+
__exportStar(require("./process"), exports);
|
|
21
|
+
__exportStar(require("./queues.types"), exports);
|
|
22
|
+
__exportStar(require("./jobs.types"), exports);
|
|
23
|
+
__exportStar(require("./types"), exports);
|
|
24
|
+
__exportStar(require("./decorators"), exports);
|
|
25
|
+
__exportStar(require("./middlewares/middlewares.types"), exports);
|
|
26
|
+
__exportStar(require("./queue-service.base"), exports);
|
|
27
|
+
__exportStar(require("./processor-base.abstract"), exports);
|
|
28
|
+
__exportStar(require("./queues.module"), exports);
|
|
29
|
+
// dynamic queues
|
|
30
|
+
__exportStar(require("./dynamic-queues"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { QueuedJob } from '../queues.types';
|
|
2
|
+
import { JobExecutionContext } from '../types';
|
|
3
|
+
export type QueueMiddlewareNextFn = (error?: Error) => Promise<void>;
|
|
4
|
+
interface QueueMiddlewareFn<TJob extends QueuedJob> {
|
|
5
|
+
(context: JobExecutionContext<TJob>, next: QueueMiddlewareNextFn): any | Promise<any>;
|
|
6
|
+
}
|
|
7
|
+
export declare abstract class QueueMiddleware<Job extends QueuedJob = any> {
|
|
8
|
+
abstract use: QueueMiddlewareFn<Job>;
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './process.decorator';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./process.decorator"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { JobsOptions } from 'bullmq';
|
|
2
|
+
export declare const JOB_PROCESS_META: unique symbol;
|
|
3
|
+
export interface JobProcessMeta {
|
|
4
|
+
name: string;
|
|
5
|
+
opt?: JobsOptions;
|
|
6
|
+
}
|
|
7
|
+
export type JobProcessFullMeta = JobProcessMeta & {
|
|
8
|
+
mdwrs?: any[];
|
|
9
|
+
};
|
|
10
|
+
export declare function Process(name: string, opt?: JobProcessMeta['opt']): <TFunction extends Function, Y>(target: object | TFunction, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
11
|
+
export declare function getProcessesMetaDict(target: any): Record<string, JobProcessFullMeta>;
|
|
12
|
+
export declare function getProcessMeta(target: any, methodName: string): JobProcessMeta | undefined;
|
|
13
|
+
export declare function UseProcessMiddlewares(...mdws: any[]): import("@nestjs/common").CustomDecorator<string>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UseProcessMiddlewares = exports.getProcessMeta = exports.getProcessesMetaDict = exports.Process = exports.JOB_PROCESS_META = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.JOB_PROCESS_META = Symbol('JOB_PROCESS_META');
|
|
6
|
+
const PROCESS_META_KEY_PREFIX = 'process_meta';
|
|
7
|
+
const toProcessMetaKey = (methodName) => `${PROCESS_META_KEY_PREFIX}:${methodName}`;
|
|
8
|
+
const isProcessMetaKey = (key) => key.startsWith(PROCESS_META_KEY_PREFIX);
|
|
9
|
+
function Process(name, opt) {
|
|
10
|
+
const meta = {
|
|
11
|
+
name,
|
|
12
|
+
opt,
|
|
13
|
+
};
|
|
14
|
+
return (0, common_1.applyDecorators)((0, common_1.SetMetadata)(exports.JOB_PROCESS_META, meta), (target, methodName, _descriptor) => {
|
|
15
|
+
return Reflect.defineMetadata(toProcessMetaKey(methodName), true, target);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
exports.Process = Process;
|
|
19
|
+
function getProcessesMetaDict(target) {
|
|
20
|
+
return Reflect.getMetadataKeys(target)
|
|
21
|
+
.filter(isProcessMetaKey)
|
|
22
|
+
.reduce((acc, nextKey) => {
|
|
23
|
+
acc[nextKey] = Reflect.getMetadata(nextKey, target);
|
|
24
|
+
const [_, methodName] = nextKey.split(':');
|
|
25
|
+
const method = Object.getPrototypeOf(target)[methodName];
|
|
26
|
+
const mdwrs = Reflect.getMetadata('use_process_middlewares_meta', method);
|
|
27
|
+
if (mdwrs) {
|
|
28
|
+
acc[nextKey].mdwrs = mdwrs;
|
|
29
|
+
}
|
|
30
|
+
return acc;
|
|
31
|
+
}, {});
|
|
32
|
+
}
|
|
33
|
+
exports.getProcessesMetaDict = getProcessesMetaDict;
|
|
34
|
+
// SetMetadata on a method decorator stores the meta on the method function itself (descriptor.value)
|
|
35
|
+
function getProcessMeta(target, methodName) {
|
|
36
|
+
const method = target?.[methodName];
|
|
37
|
+
return typeof method === 'function'
|
|
38
|
+
? Reflect.getMetadata(exports.JOB_PROCESS_META, method)
|
|
39
|
+
: undefined;
|
|
40
|
+
}
|
|
41
|
+
exports.getProcessMeta = getProcessMeta;
|
|
42
|
+
function UseProcessMiddlewares(...mdws) {
|
|
43
|
+
return (0, common_1.SetMetadata)('use_process_middlewares_meta', mdws);
|
|
44
|
+
}
|
|
45
|
+
exports.UseProcessMiddlewares = UseProcessMiddlewares;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Logger, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { AppHttpExceptionResponse, HttpSuccessRes } from '@my-nestjs-kit/core';
|
|
3
|
+
import { JobsDataDictBase, JobsMappedType, QueuedJob } from './queues.types';
|
|
4
|
+
import { ValuesOf } from '@my-nestjs-kit/core';
|
|
5
|
+
import { QueueMiddleware } from './middlewares/middlewares.types';
|
|
6
|
+
import { WorkerHost } from '@nestjs/bullmq';
|
|
7
|
+
import { Job } from 'bullmq';
|
|
8
|
+
import { JobExecutionContext } from './types';
|
|
9
|
+
export interface QueueProcessorServiceOptions {
|
|
10
|
+
queue: string;
|
|
11
|
+
messages?: {
|
|
12
|
+
onFail?: string;
|
|
13
|
+
onAdd?: string;
|
|
14
|
+
onComplete?: string;
|
|
15
|
+
};
|
|
16
|
+
middleware?: QueueMiddleware[];
|
|
17
|
+
}
|
|
18
|
+
export type JobProcessExecutor<TJob extends QueuedJob, RT = any> = (job: TJob, ctx: JobExecutionContext<TJob>) => Promise<RT>;
|
|
19
|
+
/**
|
|
20
|
+
* Kit version of the ERP ProcessorBase: same `@Process(name)` dispatch and middleware chain, without the ERP
|
|
21
|
+
* ALS / queues-registry / proxy-queue wiring. Handlers are discovered across the prototype chain, called bound
|
|
22
|
+
* to the processor and awaited, so a throwing handler fails the BullMQ job.
|
|
23
|
+
*/
|
|
24
|
+
export declare abstract class ProcessorBase<JobsMap extends JobsMappedType<JobsDataDictBase> = JobsMappedType<JobsDataDictBase>> extends WorkerHost implements OnModuleInit {
|
|
25
|
+
protected readonly options: QueueProcessorServiceOptions;
|
|
26
|
+
protected readonly _logger: Logger;
|
|
27
|
+
protected _middleware: QueueMiddleware[];
|
|
28
|
+
protected constructor(options: QueueProcessorServiceOptions, middleware?: QueueMiddleware[]);
|
|
29
|
+
private handlers;
|
|
30
|
+
onModuleInit(): void;
|
|
31
|
+
private discovery;
|
|
32
|
+
process(job: Job, token?: string): Promise<any>;
|
|
33
|
+
onCompletedJob(job: ValuesOf<JobsMap>, result: HttpSuccessRes): void;
|
|
34
|
+
onFailedJob(job: ValuesOf<JobsMap>, error: AppHttpExceptionResponse): void;
|
|
35
|
+
protected _fail(res: AppHttpExceptionResponse): void;
|
|
36
|
+
protected runMiddleware<TJob extends QueuedJob>(middlewares: Array<QueueMiddleware<TJob>>, context: JobExecutionContext<TJob>, next: () => Promise<any>): Promise<any>;
|
|
37
|
+
protected createContext<TJob extends QueuedJob>(job: TJob, methodName: string, token: string): JobExecutionContext<TJob>;
|
|
38
|
+
protected runWith<JType extends QueuedJob>(job: JType, token: string): ReturnType<JobProcessExecutor<JType>>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ProcessorBase = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@my-nestjs-kit/core");
|
|
15
|
+
const bullmq_1 = require("@nestjs/bullmq");
|
|
16
|
+
const process_1 = require("./process");
|
|
17
|
+
/**
|
|
18
|
+
* Kit version of the ERP ProcessorBase: same `@Process(name)` dispatch and middleware chain, without the ERP
|
|
19
|
+
* ALS / queues-registry / proxy-queue wiring. Handlers are discovered across the prototype chain, called bound
|
|
20
|
+
* to the processor and awaited, so a throwing handler fails the BullMQ job.
|
|
21
|
+
*/
|
|
22
|
+
class ProcessorBase extends bullmq_1.WorkerHost {
|
|
23
|
+
constructor(options, middleware) {
|
|
24
|
+
super();
|
|
25
|
+
this.options = options;
|
|
26
|
+
this._logger = new common_1.Logger(this.constructor.name);
|
|
27
|
+
this._middleware = [];
|
|
28
|
+
this.handlers = new Map();
|
|
29
|
+
this._middleware = [...(options.middleware ?? []), ...(middleware ?? [])];
|
|
30
|
+
}
|
|
31
|
+
onModuleInit() {
|
|
32
|
+
this.discovery();
|
|
33
|
+
}
|
|
34
|
+
discovery() {
|
|
35
|
+
let proto = Object.getPrototypeOf(this);
|
|
36
|
+
while (proto && proto !== ProcessorBase.prototype) {
|
|
37
|
+
for (const methodName of Object.getOwnPropertyNames(proto)) {
|
|
38
|
+
if (methodName === 'constructor')
|
|
39
|
+
continue;
|
|
40
|
+
const handlerMeta = (0, process_1.getProcessMeta)(proto, methodName);
|
|
41
|
+
if (!handlerMeta || this.handlers.has(handlerMeta.name))
|
|
42
|
+
continue;
|
|
43
|
+
this.handlers.set(handlerMeta.name, { meta: handlerMeta, methodName });
|
|
44
|
+
}
|
|
45
|
+
proto = Object.getPrototypeOf(proto);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
process(job, token) {
|
|
49
|
+
return this.runWith(job, token);
|
|
50
|
+
}
|
|
51
|
+
onCompletedJob(job, result) {
|
|
52
|
+
if (!result)
|
|
53
|
+
return;
|
|
54
|
+
result.meta = {
|
|
55
|
+
...(result.meta ?? {}),
|
|
56
|
+
processor: this.constructor.name,
|
|
57
|
+
queue: this.options.queue,
|
|
58
|
+
job: job.name,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
onFailedJob(job, error) {
|
|
62
|
+
this._logger.error(this.options?.messages?.onFail ??
|
|
63
|
+
`Job failed //Queue:${this.options.queue}. Job:${job?.name}//`);
|
|
64
|
+
console.log(error);
|
|
65
|
+
}
|
|
66
|
+
_fail(res) {
|
|
67
|
+
if (!res.meta)
|
|
68
|
+
res.meta = {};
|
|
69
|
+
res.meta.processor = this.constructor.name;
|
|
70
|
+
throw new core_1.AppHttpException(res);
|
|
71
|
+
}
|
|
72
|
+
async runMiddleware(middlewares, context, next) {
|
|
73
|
+
const job = context.getJob();
|
|
74
|
+
const runNext = async (index) => {
|
|
75
|
+
if (index >= middlewares.length)
|
|
76
|
+
return next();
|
|
77
|
+
let result;
|
|
78
|
+
await middlewares[index].use(context, async () => {
|
|
79
|
+
result = await runNext(index + 1);
|
|
80
|
+
});
|
|
81
|
+
return result;
|
|
82
|
+
};
|
|
83
|
+
try {
|
|
84
|
+
return await runNext(0);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
this._logger.error(`[Middleware runner] //Queue:${this.options.queue}. Job:${job.name}// Error:`);
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
createContext(job, methodName, token) {
|
|
92
|
+
return {
|
|
93
|
+
token,
|
|
94
|
+
args: [job, token],
|
|
95
|
+
getJob: () => job,
|
|
96
|
+
getData: () => job.data,
|
|
97
|
+
getType: () => 'job',
|
|
98
|
+
getClass: () => this.constructor,
|
|
99
|
+
getThis: () => this,
|
|
100
|
+
getHandler: () => this[methodName].bind(this),
|
|
101
|
+
getQueue: () => this.worker,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
async runWith(job, token) {
|
|
105
|
+
const handlerInfo = this.handlers.get(job.name);
|
|
106
|
+
if (!handlerInfo) {
|
|
107
|
+
throw new Error(`No @Process handler for job "${job.name}" in ${this.constructor.name} (queue: ${this.options.queue})`);
|
|
108
|
+
}
|
|
109
|
+
const ctx = this.createContext(job, handlerInfo.methodName, token);
|
|
110
|
+
return this.runMiddleware(this._middleware, ctx, () => ctx.getHandler()(job, ctx));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.ProcessorBase = ProcessorBase;
|
|
114
|
+
__decorate([
|
|
115
|
+
(0, bullmq_1.OnQueueEvent)('completed'),
|
|
116
|
+
__metadata("design:type", Function),
|
|
117
|
+
__metadata("design:paramtypes", [Object, core_1.HttpSuccessRes]),
|
|
118
|
+
__metadata("design:returntype", void 0)
|
|
119
|
+
], ProcessorBase.prototype, "onCompletedJob", null);
|
|
120
|
+
__decorate([
|
|
121
|
+
(0, bullmq_1.OnQueueEvent)('failed'),
|
|
122
|
+
__metadata("design:type", Function),
|
|
123
|
+
__metadata("design:paramtypes", [Object, core_1.AppHttpExceptionResponse]),
|
|
124
|
+
__metadata("design:returntype", void 0)
|
|
125
|
+
], ProcessorBase.prototype, "onFailedJob", null);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Logger } from '@nestjs/common';
|
|
2
|
+
import { BulkJobConfig, BullJobDataBase, BullJobOptions, BullQueueType, JobsDataDictBase, MappedBulkQueueActions, MappedQueueActions, QueuedJob } from './queues.types';
|
|
3
|
+
import { DynamicQueueServiceMeta } from './decorators';
|
|
4
|
+
export type QueueServiceOptions<DataMap extends Record<string, any>> = {
|
|
5
|
+
getJobOptions?: <JName extends keyof DataMap>(jobName: JName) => BullJobOptions;
|
|
6
|
+
};
|
|
7
|
+
export declare abstract class QueueServiceBase<DataMap extends JobsDataDictBase, QName extends string = string> {
|
|
8
|
+
private typesEnum;
|
|
9
|
+
private options?;
|
|
10
|
+
actions: MappedQueueActions<DataMap, QName>;
|
|
11
|
+
bulkActions: MappedBulkQueueActions<DataMap>;
|
|
12
|
+
protected abstract readonly queue: BullQueueType<DataMap, QName>;
|
|
13
|
+
/** `proxy__<type>` queue, bound for `@QueueService({ hasProxy: true })` (see dynamic-queues `bindProxies`). */
|
|
14
|
+
protected proxyQ?: BullQueueType<DataMap, QName>;
|
|
15
|
+
protected readonly _logger: Logger;
|
|
16
|
+
private _meta;
|
|
17
|
+
protected constructor(typesEnum: Record<string, keyof DataMap>, options?: QueueServiceOptions<DataMap>);
|
|
18
|
+
get meta(): DynamicQueueServiceMeta;
|
|
19
|
+
registerProxy(proxyQ: BullQueueType<DataMap, QName>): void;
|
|
20
|
+
private initializeMethods;
|
|
21
|
+
add<JName extends keyof DataMap>(name: JName, data: DataMap[JName] & BullJobDataBase<QName, JName>, opts?: BullJobOptions): Promise<QueuedJob<DataMap[JName], DataMap, QName>>;
|
|
22
|
+
addBulk<JName extends keyof DataMap>(configs: Array<BulkJobConfig<JName, DataMap[JName] & BullJobDataBase<QName, JName>>>): Promise<Array<QueuedJob<DataMap[JName]>>>;
|
|
23
|
+
create<JName extends keyof DataMap>(name: JName, data: DataMap[JName] & BullJobDataBase<QName, JName>, opts?: BullJobOptions): {
|
|
24
|
+
name: JName;
|
|
25
|
+
data: DataMap[JName] & BullJobDataBase<QName, JName>;
|
|
26
|
+
opts?: BullJobOptions;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueueServiceBase = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const core_1 = require("@my-nestjs-kit/core");
|
|
6
|
+
const decorators_1 = require("./decorators");
|
|
7
|
+
class QueueServiceBase {
|
|
8
|
+
constructor(typesEnum, options) {
|
|
9
|
+
this.typesEnum = typesEnum;
|
|
10
|
+
this.options = options;
|
|
11
|
+
this._logger = new common_1.Logger(this.constructor.name);
|
|
12
|
+
if (!this.actions)
|
|
13
|
+
this.actions = {};
|
|
14
|
+
if (!this.bulkActions)
|
|
15
|
+
this.bulkActions = {};
|
|
16
|
+
const meta = (0, decorators_1.getQueueServiceMeta)(this);
|
|
17
|
+
if (meta) {
|
|
18
|
+
this._meta = meta;
|
|
19
|
+
}
|
|
20
|
+
this.initializeMethods();
|
|
21
|
+
}
|
|
22
|
+
get meta() {
|
|
23
|
+
return this._meta;
|
|
24
|
+
}
|
|
25
|
+
registerProxy(proxyQ) {
|
|
26
|
+
this.proxyQ = proxyQ;
|
|
27
|
+
}
|
|
28
|
+
initializeMethods() {
|
|
29
|
+
for (const jobName of (0, core_1.ObjectValues)(this.typesEnum)) {
|
|
30
|
+
this.actions[jobName] = (async (payload, options) => {
|
|
31
|
+
// jobs flagged `__proxy.enabled` go to the proxy queue when one is bound
|
|
32
|
+
const target = this.proxyQ && payload?.__proxy?.enabled ? this.proxyQ : this.queue;
|
|
33
|
+
const opts = this.options?.getJobOptions
|
|
34
|
+
? { ...this.options.getJobOptions(jobName), ...options }
|
|
35
|
+
: options;
|
|
36
|
+
try {
|
|
37
|
+
return await target.add(jobName, payload, opts);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
this._logger.error(`Add job error: ${String(jobName)}`);
|
|
41
|
+
throw e;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
this.bulkActions[jobName] = (async (payload) => {
|
|
45
|
+
try {
|
|
46
|
+
return await this.queue.addBulk(payload);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
this._logger.error(`Add bulk jobs error: ${String(jobName)}`);
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
add(name, data, opts) {
|
|
56
|
+
if (!this.typesEnum.hasOwnProperty(name)) {
|
|
57
|
+
throw new Error(`Invalid type: ${String(name)} for queue: ${this.queue.name}`);
|
|
58
|
+
}
|
|
59
|
+
return this.queue.add(name, data, opts);
|
|
60
|
+
}
|
|
61
|
+
addBulk(configs) {
|
|
62
|
+
for (const config of configs) {
|
|
63
|
+
if (!this.typesEnum.hasOwnProperty(config.name)) {
|
|
64
|
+
throw new Error(`Invalid type: ${String(config.name)} for queue: ${this.queue.name}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return this.queue.addBulk(configs);
|
|
68
|
+
}
|
|
69
|
+
create(name, data, opts) {
|
|
70
|
+
return { name, data, opts };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.QueueServiceBase = QueueServiceBase;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ACTIVE_QUEUES_REDIS_KEY = exports.QUEUES_CHANEL_NAME = exports.RATE_LIMITER_QUEUE = void 0;
|
|
4
|
+
exports.RATE_LIMITER_QUEUE = 'RATE_LIMITER_QUEUE';
|
|
5
|
+
exports.QUEUES_CHANEL_NAME = 'queues';
|
|
6
|
+
exports.ACTIVE_QUEUES_REDIS_KEY = 'activeQueues';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import * as NestBull from '@nestjs/bullmq';
|
|
3
|
+
export type RegisterQueueAsyncOptions = Omit<NestBull.RegisterQueueOptions, 'name'>;
|
|
4
|
+
/**
|
|
5
|
+
* Thin BullMQ wiring for kit-based services. `forRootAsync` sets the shared connection once (app module);
|
|
6
|
+
* feature modules call `registerQueueAsync(name, opts)` — same signature as the ERP QueuesModule.
|
|
7
|
+
*/
|
|
8
|
+
export declare class QueuesModule {
|
|
9
|
+
static forRootAsync(options: NestBull.SharedBullAsyncConfiguration): DynamicModule;
|
|
10
|
+
static registerQueueAsync(name: string, { ...options }?: RegisterQueueAsyncOptions): DynamicModule;
|
|
11
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.QueuesModule = void 0;
|
|
33
|
+
const common_1 = require("@nestjs/common");
|
|
34
|
+
const NestBull = __importStar(require("@nestjs/bullmq"));
|
|
35
|
+
/**
|
|
36
|
+
* Thin BullMQ wiring for kit-based services. `forRootAsync` sets the shared connection once (app module);
|
|
37
|
+
* feature modules call `registerQueueAsync(name, opts)` — same signature as the ERP QueuesModule.
|
|
38
|
+
*/
|
|
39
|
+
let QueuesModule = class QueuesModule {
|
|
40
|
+
static forRootAsync(options) {
|
|
41
|
+
return NestBull.BullModule.forRootAsync(options);
|
|
42
|
+
}
|
|
43
|
+
static registerQueueAsync(name, { ...options } = {}) {
|
|
44
|
+
return NestBull.BullModule.registerQueueAsync({
|
|
45
|
+
name,
|
|
46
|
+
useFactory() {
|
|
47
|
+
return {
|
|
48
|
+
...(options ?? {}),
|
|
49
|
+
name,
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
exports.QueuesModule = QueuesModule;
|
|
56
|
+
exports.QueuesModule = QueuesModule = __decorate([
|
|
57
|
+
(0, common_1.Module)({})
|
|
58
|
+
], QueuesModule);
|