@anchan828/nest-cloud-run-queue-worker 2.1.19 → 2.2.1

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.
@@ -1,4 +1,5 @@
1
1
  import { QueueWorkerName } from "@anchan828/nest-cloud-run-queue-common";
2
+ import { QueueWorkerOptions, QueueWorkerProcessOptions } from "./interfaces";
2
3
  /**
3
4
  * Define worker
4
5
  *
@@ -9,6 +10,7 @@ import { QueueWorkerName } from "@anchan828/nest-cloud-run-queue-common";
9
10
  */
10
11
  export declare function QueueWorker(name: QueueWorkerName, priority?: number): ClassDecorator;
11
12
  export declare function QueueWorker(names: QueueWorkerName[], priority?: number): ClassDecorator;
13
+ export declare function QueueWorker(options: QueueWorkerOptions): ClassDecorator;
12
14
  /**
13
15
  * Define worker processor
14
16
  *
@@ -17,3 +19,4 @@ export declare function QueueWorker(names: QueueWorkerName[], priority?: number)
17
19
  * @returns {MethodDecorator}
18
20
  */
19
21
  export declare function QueueWorkerProcess(priority?: number): MethodDecorator;
22
+ export declare function QueueWorkerProcess(options?: QueueWorkerProcessOptions): MethodDecorator;
@@ -4,19 +4,28 @@ exports.QueueWorker = QueueWorker;
4
4
  exports.QueueWorkerProcess = QueueWorkerProcess;
5
5
  const common_1 = require("@nestjs/common");
6
6
  const constants_1 = require("./constants");
7
- function QueueWorker(names, priority = 0) {
7
+ function QueueWorker(nameOrOptions, priority = 0) {
8
+ if (Array.isArray(nameOrOptions)) {
9
+ return (0, common_1.SetMetadata)(constants_1.QUEUE_WORKER_DECORATOR, {
10
+ names: nameOrOptions,
11
+ priority,
12
+ });
13
+ }
14
+ if (typeof nameOrOptions === "string") {
15
+ return (0, common_1.SetMetadata)(constants_1.QUEUE_WORKER_DECORATOR, {
16
+ names: [nameOrOptions],
17
+ priority,
18
+ });
19
+ }
8
20
  return (0, common_1.SetMetadata)(constants_1.QUEUE_WORKER_DECORATOR, {
9
- names: Array.isArray(names) ? names : [names],
10
- priority,
21
+ enabled: nameOrOptions.enabled,
22
+ names: Array.isArray(nameOrOptions.name) ? nameOrOptions.name : [nameOrOptions.name],
23
+ priority: nameOrOptions.priority || 0,
11
24
  });
12
25
  }
13
- /**
14
- * Define worker processor
15
- *
16
- * @export
17
- * @param {number} [priority=0] Highest priority is 0, and lower the larger integer you use.
18
- * @returns {MethodDecorator}
19
- */
20
- function QueueWorkerProcess(priority = 0) {
21
- return (0, common_1.SetMetadata)(constants_1.QUEUE_WORKER_PROCESS_DECORATOR, { priority });
26
+ function QueueWorkerProcess(priorityOrOptions) {
27
+ const options = typeof priorityOrOptions === "number"
28
+ ? { priority: priorityOrOptions }
29
+ : Object.assign({ priority: 0 }, priorityOrOptions);
30
+ return (0, common_1.SetMetadata)(constants_1.QUEUE_WORKER_PROCESS_DECORATOR, options);
22
31
  }
@@ -33,6 +33,9 @@ let QueueWorkerExplorerService = class QueueWorkerExplorerService {
33
33
  .filter((instanceWrapper) => instanceWrapper.instance?.constructor)) {
34
34
  const args = Reflect.getMetadata(constants_1.QUEUE_WORKER_DECORATOR, classInstanceWrapper.instance.constructor);
35
35
  if (args && Array.isArray(args.names)) {
36
+ if (args.enabled === false) {
37
+ continue;
38
+ }
36
39
  for (const name of args.names) {
37
40
  metadata.push({
38
41
  instance: classInstanceWrapper.instance,
@@ -49,9 +52,12 @@ let QueueWorkerExplorerService = class QueueWorkerExplorerService {
49
52
  const metadata = [];
50
53
  const instance = worker.instance;
51
54
  const prototype = Object.getPrototypeOf(instance);
52
- for (const methodName of this.metadataScanner.getAllFilteredMethodNames(prototype)) {
55
+ for (const methodName of this.metadataScanner.getAllMethodNames(prototype)) {
53
56
  const args = Reflect.getMetadata(constants_1.QUEUE_WORKER_PROCESS_DECORATOR, prototype[methodName]);
54
57
  if (args) {
58
+ if (args.enabled === false) {
59
+ continue;
60
+ }
55
61
  metadata.push({
56
62
  priority: args.priority || 0,
57
63
  processor: prototype[methodName].bind(instance),
@@ -64,5 +70,6 @@ let QueueWorkerExplorerService = class QueueWorkerExplorerService {
64
70
  exports.QueueWorkerExplorerService = QueueWorkerExplorerService;
65
71
  exports.QueueWorkerExplorerService = QueueWorkerExplorerService = __decorate([
66
72
  (0, common_1.Injectable)(),
67
- __metadata("design:paramtypes", [core_1.DiscoveryService, metadata_scanner_1.MetadataScanner])
73
+ __metadata("design:paramtypes", [core_1.DiscoveryService,
74
+ metadata_scanner_1.MetadataScanner])
68
75
  ], QueueWorkerExplorerService);
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, QueueWorkerReceivedMessage, QueueWorkerDecodedMessage, } from "./interfaces";
3
+ export { QueueWorkerControllerInterface, QueueWorkerControllerMetadata, QueueWorkerDecodedMessage, QueueWorkerExtraConfig, QueueWorkerModuleAsyncOptions, QueueWorkerModuleOptions, QueueWorkerModuleOptionsFactory, QueueWorkerOptions, QueueWorkerProcessOptions, QueueWorkerProcessor, QueueWorkerProcessorStatus, QueueWorkerRawMessage, QueueWorkerReceivedMessage, } from "./interfaces";
4
4
  export { QueueWorkerModule } from "./worker.module";
5
5
  export { QueueWorkerService } from "./worker.service";
@@ -1,4 +1,4 @@
1
- import { QueueWorkerName, ModuleAsyncOptions, ModuleOptions, ModuleOptionsFactory, Message } from "@anchan828/nest-cloud-run-queue-common";
1
+ import { Message, ModuleAsyncOptions, ModuleOptions, ModuleOptionsFactory, QueueWorkerName } from "@anchan828/nest-cloud-run-queue-common";
2
2
  import { RequestMappingMetadata } from "@nestjs/common";
3
3
  import { Injectable } from "@nestjs/common/interfaces";
4
4
  export interface QueueWorkerModuleOptions extends ModuleOptions {
@@ -61,6 +61,10 @@ export interface QueueWorkerDecoratorArgs {
61
61
  * @memberof QueueWorkerDecoratorArgs
62
62
  */
63
63
  priority: number;
64
+ /**
65
+ * If you want to disable the worker, set it to false.
66
+ */
67
+ enabled?: boolean;
64
68
  }
65
69
  export interface QueueWorkerProcessDecoratorArgs {
66
70
  /**
@@ -70,6 +74,10 @@ export interface QueueWorkerProcessDecoratorArgs {
70
74
  * @memberof QueueWorkerProcessDecoratorArgs
71
75
  */
72
76
  priority: number;
77
+ /**
78
+ * If you want to disable the process, set it to false.
79
+ */
80
+ enabled?: boolean;
73
81
  }
74
82
  export type QueueWorkerRawMessage<T = any> = {
75
83
  readonly data?: string | null | Message<T>;
@@ -95,3 +103,27 @@ export interface QueueWorkerControllerMetadata extends RequestMappingMetadata {
95
103
  export interface QueueWorkerControllerInterface {
96
104
  execute(body: QueueWorkerReceivedMessage, headers: Record<string, string>): Promise<void>;
97
105
  }
106
+ export interface QueueWorkerOptions {
107
+ /**
108
+ * Worker name. If you want to define multiple names, use an array.
109
+ */
110
+ name: QueueWorkerName | QueueWorkerName[];
111
+ /**
112
+ * Highest priority is 0, and lower the larger integer you use.
113
+ */
114
+ priority?: number;
115
+ /**
116
+ * If you want to disable the worker, set it to false.
117
+ */
118
+ enabled?: boolean;
119
+ }
120
+ export interface QueueWorkerProcessOptions {
121
+ /**
122
+ * Highest priority is 0, and lower the larger integer you use.
123
+ */
124
+ priority?: number;
125
+ /**
126
+ * If you want to disable the process, set it to false.
127
+ */
128
+ enabled?: boolean;
129
+ }
package/dist/util.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Message } from "@anchan828/nest-cloud-run-queue-common";
2
+ import { QueueWorkerDecodedMessage, QueueWorkerRawMessage } from "./interfaces";
2
3
  /**
3
4
  * parse json string to javascript object.
4
5
  * JSON.parse has receiver for Date.parse.
@@ -24,3 +25,4 @@ export declare function sortByPriority<T extends {
24
25
  * @return {*} {boolean}
25
26
  */
26
27
  export declare function isBase64(value?: string | null | Message): value is string;
28
+ export declare function decodeMessage<T = any>(message: QueueWorkerRawMessage | Message): QueueWorkerDecodedMessage<T>;
package/dist/util.js CHANGED
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseJSON = void 0;
4
4
  exports.sortByPriority = sortByPriority;
5
5
  exports.isBase64 = isBase64;
6
+ exports.decodeMessage = decodeMessage;
7
+ const common_1 = require("@nestjs/common");
8
+ const constants_1 = require("./constants");
6
9
  const dateRegExp = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
7
10
  /**
8
11
  * parse json string to javascript object.
@@ -57,3 +60,45 @@ function isBase64(value) {
57
60
  const firstPaddingChar = value.indexOf("=");
58
61
  return (firstPaddingChar === -1 || firstPaddingChar === len - 1 || (firstPaddingChar === len - 2 && value[len - 1] === "="));
59
62
  }
63
+ function decodeMessage(message) {
64
+ let data;
65
+ if (isBase64(message.data)) {
66
+ // pubsub
67
+ data = decodeData(message.data);
68
+ }
69
+ else {
70
+ // tasks / http
71
+ data = message;
72
+ }
73
+ if (!data.name) {
74
+ throw new common_1.BadRequestException(constants_1.ERROR_QUEUE_WORKER_NAME_NOT_FOUND);
75
+ }
76
+ return {
77
+ data,
78
+ headers: "headers" in message ? message.headers : undefined,
79
+ raw: message,
80
+ };
81
+ }
82
+ function decodeData(data) {
83
+ if (!data) {
84
+ throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
85
+ }
86
+ if (Buffer.isBuffer(data)) {
87
+ data = data.toString();
88
+ }
89
+ if (data instanceof Uint8Array) {
90
+ data = new TextDecoder("utf8").decode(data);
91
+ }
92
+ if (isBase64(data)) {
93
+ data = Buffer.from(data, "base64").toString();
94
+ }
95
+ try {
96
+ if (typeof data === "string") {
97
+ return (0, exports.parseJSON)(data);
98
+ }
99
+ return data;
100
+ }
101
+ catch {
102
+ throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
103
+ }
104
+ }
@@ -23,6 +23,7 @@ function getWorkerController(metadata) {
23
23
  this.service = service;
24
24
  }
25
25
  async execute(body, headers) {
26
+ this.service.execute({});
26
27
  await this.service.execute({ ...body.message, headers });
27
28
  }
28
29
  };
@@ -1,17 +1,21 @@
1
1
  import { Logger } from "@nestjs/common";
2
- import { QueueWorkerDecodedMessage, QueueWorkerModuleOptions } from "./interfaces";
2
+ import { Message } from "@anchan828/nest-cloud-run-queue-common";
3
3
  import { QueueWorkerExplorerService } from "./explorer.service";
4
- import { QueueWorkerRawMessage } from "./interfaces";
4
+ import { QueueWorkerDecodedMessage, QueueWorkerModuleOptions, QueueWorkerRawMessage } from "./interfaces";
5
5
  export declare class QueueWorkerService {
6
6
  #private;
7
7
  private readonly options;
8
8
  private readonly logger;
9
9
  private readonly explorerService;
10
10
  constructor(options: QueueWorkerModuleOptions, logger: Logger, explorerService: QueueWorkerExplorerService);
11
- execute(rawMessage: QueueWorkerRawMessage | QueueWorkerDecodedMessage): Promise<void>;
12
- decodeMessage<T = any>(message: QueueWorkerRawMessage): QueueWorkerDecodedMessage<T>;
11
+ execute<T = any>(meessage: Message<T>): Promise<void>;
12
+ execute<T = any>(meessage: QueueWorkerDecodedMessage<T>): Promise<void>;
13
+ execute<T = any>(meessage: QueueWorkerRawMessage<T>): Promise<void>;
14
+ /**
15
+ * @deprecated Use `decodeMessage` function instead.
16
+ */
17
+ decodeMessage<T = any>(message: QueueWorkerRawMessage | Message): QueueWorkerDecodedMessage<T>;
13
18
  private runWorkers;
14
19
  private isDecodedMessage;
15
- private decodeData;
16
20
  private execProcessor;
17
21
  }
@@ -38,28 +38,14 @@ let QueueWorkerService = class QueueWorkerService {
38
38
  this.explorerService = explorerService;
39
39
  _QueueWorkerService__allWorkers.set(this, void 0);
40
40
  }
41
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
42
- async execute(rawMessage) {
43
- await this.runWorkers(this.isDecodedMessage(rawMessage) ? rawMessage : this.decodeMessage(rawMessage));
41
+ async execute(meessage) {
42
+ await this.runWorkers(this.isDecodedMessage(meessage) ? meessage : (0, util_1.decodeMessage)(meessage));
44
43
  }
44
+ /**
45
+ * @deprecated Use `decodeMessage` function instead.
46
+ */
45
47
  decodeMessage(message) {
46
- let data;
47
- if ((0, util_1.isBase64)(message.data)) {
48
- // pubsub
49
- data = this.decodeData(message.data);
50
- }
51
- else {
52
- // tasks / http
53
- data = message;
54
- }
55
- if (!data.name) {
56
- throw new common_1.BadRequestException(constants_1.ERROR_QUEUE_WORKER_NAME_NOT_FOUND);
57
- }
58
- return {
59
- data,
60
- headers: message.headers,
61
- raw: message,
62
- };
48
+ return (0, util_1.decodeMessage)(message);
63
49
  }
64
50
  async runWorkers(decodedMessage) {
65
51
  const maxRetryAttempts = this.options.maxRetryAttempts ?? 1;
@@ -86,30 +72,7 @@ let QueueWorkerService = class QueueWorkerService {
86
72
  await this.options.extraConfig?.postProcessor?.(decodedMessage.data.name, decodedMessage.data.data, decodedMessage.raw);
87
73
  }
88
74
  isDecodedMessage(message) {
89
- return !!message.raw;
90
- }
91
- decodeData(data) {
92
- if (!data) {
93
- throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
94
- }
95
- if (Buffer.isBuffer(data)) {
96
- data = data.toString();
97
- }
98
- if (data instanceof Uint8Array) {
99
- data = new TextDecoder("utf8").decode(data);
100
- }
101
- if ((0, util_1.isBase64)(data)) {
102
- data = Buffer.from(data, "base64").toString();
103
- }
104
- try {
105
- if (typeof data === "string") {
106
- return (0, util_1.parseJSON)(data);
107
- }
108
- return data;
109
- }
110
- catch {
111
- throw new common_1.BadRequestException(constants_1.ERROR_INVALID_MESSAGE_FORMAT);
112
- }
75
+ return "raw" in message;
113
76
  }
114
77
  async execProcessor(processor, maxRetryAttempts, data, raw) {
115
78
  for (let i = 0; i < maxRetryAttempts; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchan828/nest-cloud-run-queue-worker",
3
- "version": "2.1.19",
3
+ "version": "2.2.1",
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,10 +33,10 @@
33
33
  "watch": "tsc -w"
34
34
  },
35
35
  "dependencies": {
36
- "@anchan828/nest-cloud-run-queue-common": "^2.1.19"
36
+ "@anchan828/nest-cloud-run-queue-common": "^2.2.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@nestjs/common": "10.3.9",
39
+ "@nestjs/common": "10.3.10",
40
40
  "rxjs": "7.8.1"
41
41
  },
42
42
  "peerDependencies": {
@@ -46,5 +46,5 @@
46
46
  "access": "public"
47
47
  },
48
48
  "packageManager": "npm@10.8.1",
49
- "gitHead": "153d36da9db498fa40bc2cffd1a5a5b63451ecb6"
49
+ "gitHead": "882db718c2c146c2ec24b95462eccbad2cad2125"
50
50
  }