@anchan828/nest-cloud-run-queue-worker 1.0.9 → 1.0.12
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 +1 -1
- package/dist/interfaces.d.ts +14 -8
- package/dist/worker.module.js +12 -4
- package/dist/worker.service.d.ts +6 -3
- package/dist/worker.service.js +53 -44
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { ALL_WORKERS_QUEUE_WORKER_NAME as ALL_QUEUE_WORKERS, UNHANDLED_QUEUE_WORKER_NAME as UNHANDLED_QUEUE_WORKER, } from "./constants";
|
|
2
2
|
export { QueueWorker, QueueWorkerProcess } from "./decorators";
|
|
3
|
-
export { QueueWorkerModuleAsyncOptions, QueueWorkerModuleOptions, QueueWorkerModuleOptionsFactory, QueueWorkerProcessor, QueueWorkerProcessorStatus, QueueWorkerExtraConfig, QueueWorkerRawMessage, QueueWorkerControllerInterface, QueueWorkerControllerMetadata, } from "./interfaces";
|
|
3
|
+
export { QueueWorkerModuleAsyncOptions, QueueWorkerModuleOptions, QueueWorkerModuleOptionsFactory, QueueWorkerProcessor, QueueWorkerProcessorStatus, QueueWorkerExtraConfig, QueueWorkerRawMessage, QueueWorkerControllerInterface, QueueWorkerControllerMetadata, QueueWorkerReceivedMessage, QueueWorkerDecodedMessage, } from "./interfaces";
|
|
4
4
|
export { QueueWorkerModule } from "./worker.module";
|
|
5
5
|
export { QueueWorkerService } from "./worker.service";
|
package/dist/interfaces.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { QueueWorkerName, ModuleAsyncOptions, ModuleOptions, ModuleOptionsFactory } from "@anchan828/nest-cloud-run-queue-common";
|
|
2
|
+
import { QueueWorkerName, ModuleAsyncOptions, ModuleOptions, ModuleOptionsFactory, Message } from "@anchan828/nest-cloud-run-queue-common";
|
|
3
3
|
import { RequestMappingMetadata } from "@nestjs/common";
|
|
4
4
|
import { Injectable } from "@nestjs/common/interfaces";
|
|
5
5
|
export interface QueueWorkerModuleOptions extends ModuleOptions {
|
|
@@ -27,14 +27,15 @@ export interface QueueWorkerModuleOptions extends ModuleOptions {
|
|
|
27
27
|
/**
|
|
28
28
|
* Define a Route for the controller.
|
|
29
29
|
* Default: POST /
|
|
30
|
-
*
|
|
30
|
+
* If you provide your own Controller, set it to null.
|
|
31
|
+
* @type {(QueueWorkerControllerMetadata | null)}
|
|
31
32
|
* @memberof QueueWorkerModuleOptions
|
|
32
33
|
*/
|
|
33
|
-
workerController?: QueueWorkerControllerMetadata;
|
|
34
|
+
workerController?: QueueWorkerControllerMetadata | null;
|
|
34
35
|
}
|
|
35
36
|
export declare type QueueWorkerModuleAsyncOptions = ModuleAsyncOptions<Omit<QueueWorkerModuleOptions, "workerController">> & Pick<QueueWorkerModuleOptions, "workerController">;
|
|
36
37
|
export declare type QueueWorkerModuleOptionsFactory = ModuleOptionsFactory<Omit<QueueWorkerModuleOptions, "workerController">> & Pick<QueueWorkerModuleOptions, "workerController">;
|
|
37
|
-
export declare type QueueWorkerProcessor = <T>(message: T,
|
|
38
|
+
export declare type QueueWorkerProcessor = <T>(message: T, raw: QueueWorkerRawMessage) => Promise<void> | void;
|
|
38
39
|
export interface QueueWorkerMetadata {
|
|
39
40
|
instance: Injectable;
|
|
40
41
|
processors: QueueWorkerProcessorMetadata[];
|
|
@@ -71,11 +72,16 @@ export interface QueueWorkerProcessDecoratorArgs {
|
|
|
71
72
|
*/
|
|
72
73
|
priority: number;
|
|
73
74
|
}
|
|
74
|
-
export declare type QueueWorkerRawMessage
|
|
75
|
+
export declare type QueueWorkerRawMessage = {
|
|
75
76
|
readonly data?: string | Uint8Array | Buffer | null;
|
|
76
77
|
readonly headers?: Record<string, string>;
|
|
77
|
-
} &
|
|
78
|
-
export declare type
|
|
78
|
+
} & Record<string, any>;
|
|
79
|
+
export declare type QueueWorkerDecodedMessage<T = any> = {
|
|
80
|
+
readonly data: Message<T>;
|
|
81
|
+
readonly headers?: Record<string, string>;
|
|
82
|
+
readonly raw: QueueWorkerRawMessage;
|
|
83
|
+
};
|
|
84
|
+
export declare type QueueWorkerReceivedMessage = {
|
|
79
85
|
readonly message: QueueWorkerRawMessage;
|
|
80
86
|
};
|
|
81
87
|
export interface QueueWorkerControllerMetadata extends RequestMappingMetadata {
|
|
@@ -88,5 +94,5 @@ export interface QueueWorkerControllerMetadata extends RequestMappingMetadata {
|
|
|
88
94
|
statusCode?: number;
|
|
89
95
|
}
|
|
90
96
|
export interface QueueWorkerControllerInterface {
|
|
91
|
-
execute(body:
|
|
97
|
+
execute(body: QueueWorkerReceivedMessage, headers: Record<string, string>): Promise<void>;
|
|
92
98
|
}
|
package/dist/worker.module.js
CHANGED
|
@@ -18,20 +18,28 @@ const worker_controller_1 = require("./worker.controller");
|
|
|
18
18
|
const worker_service_1 = require("./worker.service");
|
|
19
19
|
let QueueWorkerModule = QueueWorkerModule_1 = class QueueWorkerModule {
|
|
20
20
|
static register(options = {}) {
|
|
21
|
-
const
|
|
21
|
+
const controllers = [];
|
|
22
22
|
const providers = [(0, nest_cloud_run_queue_common_1.createOptionProvider)(constants_1.QUEUE_WORKER_MODULE_OPTIONS, options)];
|
|
23
|
+
if (options.workerController !== null) {
|
|
24
|
+
const WorkerController = (0, worker_controller_1.getWorkerController)(options.workerController);
|
|
25
|
+
controllers.push(WorkerController);
|
|
26
|
+
}
|
|
23
27
|
return {
|
|
24
|
-
controllers
|
|
28
|
+
controllers,
|
|
25
29
|
global: true,
|
|
26
30
|
module: QueueWorkerModule_1,
|
|
27
31
|
providers,
|
|
28
32
|
};
|
|
29
33
|
}
|
|
30
34
|
static registerAsync(options) {
|
|
31
|
-
const
|
|
35
|
+
const controllers = [];
|
|
32
36
|
const providers = [...(0, nest_cloud_run_queue_common_1.createAsyncProviders)(constants_1.QUEUE_WORKER_MODULE_OPTIONS, options)];
|
|
37
|
+
if (options.workerController !== null) {
|
|
38
|
+
const WorkerController = (0, worker_controller_1.getWorkerController)(options.workerController);
|
|
39
|
+
controllers.push(WorkerController);
|
|
40
|
+
}
|
|
33
41
|
return {
|
|
34
|
-
controllers
|
|
42
|
+
controllers,
|
|
35
43
|
global: true,
|
|
36
44
|
imports: [...(options.imports || [])],
|
|
37
45
|
module: QueueWorkerModule_1,
|
package/dist/worker.service.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Logger } from "@nestjs/common";
|
|
2
|
-
import { QueueWorkerModuleOptions } from "./interfaces";
|
|
2
|
+
import { QueueWorkerDecodedMessage, QueueWorkerModuleOptions } from "./interfaces";
|
|
3
3
|
import { QueueWorkerExplorerService } from "./explorer.service";
|
|
4
4
|
import { QueueWorkerRawMessage } from "./interfaces";
|
|
5
5
|
export declare class QueueWorkerService {
|
|
@@ -8,7 +8,10 @@ export declare class QueueWorkerService {
|
|
|
8
8
|
private readonly logger;
|
|
9
9
|
private readonly explorerService;
|
|
10
10
|
constructor(options: QueueWorkerModuleOptions, logger: Logger, explorerService: QueueWorkerExplorerService);
|
|
11
|
-
execute(
|
|
12
|
-
|
|
11
|
+
execute(rawMessage: QueueWorkerRawMessage | QueueWorkerDecodedMessage): Promise<void>;
|
|
12
|
+
decodeMessage<T = any>(message: QueueWorkerRawMessage): QueueWorkerDecodedMessage<T>;
|
|
13
|
+
private runWorkers;
|
|
14
|
+
private isDecodedMessage;
|
|
13
15
|
private decodeData;
|
|
16
|
+
private execProcessor;
|
|
14
17
|
}
|
package/dist/worker.service.js
CHANGED
|
@@ -31,7 +31,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
31
31
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
32
32
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
33
33
|
};
|
|
34
|
-
var
|
|
34
|
+
var _QueueWorkerService_instances, _QueueWorkerService__allWorkers, _QueueWorkerService_allWorkers_get, _QueueWorkerService_spetialWorkers_get;
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.QueueWorkerService = void 0;
|
|
37
37
|
const common_1 = require("@nestjs/common");
|
|
@@ -45,70 +45,59 @@ let QueueWorkerService = class QueueWorkerService {
|
|
|
45
45
|
this.options = options;
|
|
46
46
|
this.logger = logger;
|
|
47
47
|
this.explorerService = explorerService;
|
|
48
|
-
|
|
48
|
+
_QueueWorkerService_instances.add(this);
|
|
49
|
+
_QueueWorkerService__allWorkers.set(this, void 0);
|
|
49
50
|
}
|
|
50
51
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
51
|
-
execute(
|
|
52
|
+
execute(rawMessage) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
yield this.runWorkers(this.isDecodedMessage(rawMessage) ? rawMessage : this.decodeMessage(rawMessage));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
decodeMessage(message) {
|
|
58
|
+
const data = this.decodeData(message.data);
|
|
59
|
+
if (!data.name) {
|
|
60
|
+
throw new common_1.BadRequestException(constants_1.ERROR_QUEUE_WORKER_NAME_NOT_FOUND);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
data,
|
|
64
|
+
headers: message.headers,
|
|
65
|
+
raw: message,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
runWorkers(decodedMessage) {
|
|
52
69
|
var _a, _b, _c, _d, _e, _f;
|
|
53
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
if (!__classPrivateFieldGet(this, _QueueWorkerService_allWorkers, "f")) {
|
|
55
|
-
__classPrivateFieldSet(this, _QueueWorkerService_allWorkers, this.explorerService.explore(), "f");
|
|
56
|
-
}
|
|
57
71
|
const maxRetryAttempts = (_a = this.options.maxRetryAttempts) !== null && _a !== void 0 ? _a : 1;
|
|
58
72
|
const workers = [];
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
data = this.decodeData(message.data);
|
|
63
|
-
if (!data.name) {
|
|
64
|
-
throw new Error(constants_1.ERROR_QUEUE_WORKER_NAME_NOT_FOUND);
|
|
65
|
-
}
|
|
66
|
-
workers.push(...__classPrivateFieldGet(this, _QueueWorkerService_allWorkers, "f").filter((worker) => data.name === worker.name));
|
|
67
|
-
spetialWorkers.push(...__classPrivateFieldGet(this, _QueueWorkerService_allWorkers, "f").filter((worker) => [constants_1.ALL_WORKERS_QUEUE_WORKER_NAME, constants_1.UNHANDLED_QUEUE_WORKER_NAME].includes(worker.name)));
|
|
68
|
-
if (workers.length === 0) {
|
|
69
|
-
throw new Error((0, constants_1.ERROR_WORKER_NOT_FOUND)(data.name));
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
this.logger.error(error.message);
|
|
74
|
-
if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.throwModuleError) && spetialWorkers.length === 0) {
|
|
75
|
-
throw new common_1.BadRequestException(error.message);
|
|
76
|
-
}
|
|
73
|
+
workers.push(...__classPrivateFieldGet(this, _QueueWorkerService_instances, "a", _QueueWorkerService_allWorkers_get).filter((worker) => decodedMessage.data.name === worker.name));
|
|
74
|
+
if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.throwModuleError) && workers.length === 0 && __classPrivateFieldGet(this, _QueueWorkerService_instances, "a", _QueueWorkerService_spetialWorkers_get).length === 0) {
|
|
75
|
+
throw new common_1.BadRequestException((0, constants_1.ERROR_WORKER_NOT_FOUND)(decodedMessage.data.name));
|
|
77
76
|
}
|
|
78
77
|
const processors = (0, util_1.sortByPriority)(workers)
|
|
79
78
|
.map((w) => (0, util_1.sortByPriority)(w.processors))
|
|
80
79
|
.flat();
|
|
81
|
-
const spetialProcessors = (0, util_1.sortByPriority)(
|
|
80
|
+
const spetialProcessors = (0, util_1.sortByPriority)(__classPrivateFieldGet(this, _QueueWorkerService_instances, "a", _QueueWorkerService_spetialWorkers_get))
|
|
82
81
|
.map((w) => (0, util_1.sortByPriority)(w.processors))
|
|
83
82
|
.flat();
|
|
84
|
-
const processorStatus = yield ((_d = (_c = this.options.extraConfig) === null || _c === void 0 ? void 0 : _c.preProcessor) === null || _d === void 0 ? void 0 : _d.call(_c, data.name, data.data,
|
|
83
|
+
const processorStatus = yield ((_d = (_c = this.options.extraConfig) === null || _c === void 0 ? void 0 : _c.preProcessor) === null || _d === void 0 ? void 0 : _d.call(_c, decodedMessage.data.name, decodedMessage.data.data, decodedMessage.raw));
|
|
85
84
|
if (processorStatus !== interfaces_1.QueueWorkerProcessorStatus.SKIP) {
|
|
86
85
|
for (const processor of processors) {
|
|
87
|
-
yield this.execProcessor(processor.processor, maxRetryAttempts, data.data,
|
|
86
|
+
yield this.execProcessor(processor.processor, maxRetryAttempts, decodedMessage.data.data, decodedMessage.raw);
|
|
88
87
|
}
|
|
89
88
|
for (const processor of spetialProcessors) {
|
|
90
|
-
yield this.execProcessor(processor.processor, maxRetryAttempts, data,
|
|
89
|
+
yield this.execProcessor(processor.processor, maxRetryAttempts, decodedMessage.data, decodedMessage.raw);
|
|
91
90
|
}
|
|
92
91
|
}
|
|
93
|
-
yield ((_f = (_e = this.options.extraConfig) === null || _e === void 0 ? void 0 : _e.postProcessor) === null || _f === void 0 ? void 0 : _f.call(_e, data.name, data.data,
|
|
92
|
+
yield ((_f = (_e = this.options.extraConfig) === null || _e === void 0 ? void 0 : _e.postProcessor) === null || _f === void 0 ? void 0 : _f.call(_e, decodedMessage.data.name, decodedMessage.data.data, decodedMessage.raw));
|
|
94
93
|
});
|
|
95
94
|
}
|
|
96
|
-
|
|
97
|
-
return
|
|
98
|
-
for (let i = 0; i < maxRetryAttempts; i++) {
|
|
99
|
-
try {
|
|
100
|
-
yield processor(data, rawMessage);
|
|
101
|
-
i = maxRetryAttempts;
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
this.logger.error(error.message);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
95
|
+
isDecodedMessage(message) {
|
|
96
|
+
return !!message.raw;
|
|
108
97
|
}
|
|
109
98
|
decodeData(data) {
|
|
110
99
|
if (!data) {
|
|
111
|
-
throw new
|
|
100
|
+
throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
|
|
112
101
|
}
|
|
113
102
|
if (Buffer.isBuffer(data)) {
|
|
114
103
|
data = data.toString();
|
|
@@ -126,11 +115,31 @@ let QueueWorkerService = class QueueWorkerService {
|
|
|
126
115
|
return data;
|
|
127
116
|
}
|
|
128
117
|
catch (_a) {
|
|
129
|
-
throw new
|
|
118
|
+
throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
|
|
130
119
|
}
|
|
131
120
|
}
|
|
121
|
+
execProcessor(processor, maxRetryAttempts, data, raw) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
for (let i = 0; i < maxRetryAttempts; i++) {
|
|
124
|
+
try {
|
|
125
|
+
yield processor(data, raw);
|
|
126
|
+
i = maxRetryAttempts;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
this.logger.error(error.message);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
_QueueWorkerService__allWorkers = new WeakMap(), _QueueWorkerService_instances = new WeakSet(), _QueueWorkerService_allWorkers_get = function _QueueWorkerService_allWorkers_get() {
|
|
136
|
+
if (!__classPrivateFieldGet(this, _QueueWorkerService__allWorkers, "f")) {
|
|
137
|
+
__classPrivateFieldSet(this, _QueueWorkerService__allWorkers, this.explorerService.explore(), "f");
|
|
138
|
+
}
|
|
139
|
+
return __classPrivateFieldGet(this, _QueueWorkerService__allWorkers, "f");
|
|
140
|
+
}, _QueueWorkerService_spetialWorkers_get = function _QueueWorkerService_spetialWorkers_get() {
|
|
141
|
+
return (__classPrivateFieldGet(this, _QueueWorkerService__allWorkers, "f") || []).filter((worker) => [constants_1.ALL_WORKERS_QUEUE_WORKER_NAME, constants_1.UNHANDLED_QUEUE_WORKER_NAME].includes(worker.name));
|
|
132
142
|
};
|
|
133
|
-
_QueueWorkerService_allWorkers = new WeakMap();
|
|
134
143
|
QueueWorkerService = __decorate([
|
|
135
144
|
(0, common_1.Injectable)(),
|
|
136
145
|
__param(0, (0, common_1.Inject)(constants_1.QUEUE_WORKER_MODULE_OPTIONS)),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anchan828/nest-cloud-run-queue-worker",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"homepage": "https://github.com/anchan828/nest-cloud-run-queue/tree/master/packages/worker#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"watch": "tsc -w"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@anchan828/nest-cloud-run-queue-common": "^1.0.
|
|
36
|
+
"@anchan828/nest-cloud-run-queue-common": "^1.0.12",
|
|
37
37
|
"class-validator": "0.13.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"publishConfig": {
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "4bc729dc2d2db026d22f5d36fe18eceeab6e04c7"
|
|
50
50
|
}
|