@seidor-cloud-produtos/orbit-backend-lib 1.101.41 → 1.101.43

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,5 +1,7 @@
1
1
  export default abstract class Handler {
2
2
  protected simultaneity: number;
3
+ protected timeoutPerMessageSeconds: number | undefined;
3
4
  getSimultaneity(): number;
5
+ getTimeoutPerMessageSeconds(): number | undefined;
4
6
  abstract handle(message: any): Promise<void>;
5
7
  }
@@ -2,8 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class Handler {
4
4
  simultaneity = 1;
5
+ timeoutPerMessageSeconds = undefined;
5
6
  getSimultaneity() {
6
7
  return this.simultaneity;
7
8
  }
9
+ getTimeoutPerMessageSeconds() {
10
+ return this.timeoutPerMessageSeconds;
11
+ }
8
12
  }
9
13
  exports.default = Handler;
@@ -0,0 +1,28 @@
1
+ /// <reference types="node" />
2
+ import Handler from '../../../../clean-arch/application/queue/handler';
3
+ import AmqpQueue from '../../queue/rabbitmq/amqp-lib';
4
+ import { RabbitMQScaledJobOptions } from './types';
5
+ import { AmqpQueueGetResult } from '../../queue/rabbitmq/types';
6
+ export declare class RabbitMQScaledJobRunner<T = any> {
7
+ private readonly amqpQueue;
8
+ private readonly queueName;
9
+ /**
10
+ * @deprecated The vHost already exists on amqpQueue, in v2 this field doesnt exists.
11
+ */
12
+ private readonly vHost;
13
+ private readonly handler;
14
+ private options;
15
+ constructor(amqpQueue: AmqpQueue, queueName: string,
16
+ /**
17
+ * @deprecated The vHost already exists on amqpQueue, in v2 this field doesnt exists.
18
+ */
19
+ vHost: string, handler: Handler, options: RabbitMQScaledJobOptions);
20
+ private setOptions;
21
+ private handleError;
22
+ protected errorStrategy(getResult: AmqpQueueGetResult): Promise<void>;
23
+ protected parse(raw: {
24
+ content: Buffer;
25
+ }): any;
26
+ private finishProcess;
27
+ run(): Promise<void>;
28
+ }
@@ -2,19 +2,29 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RabbitMQScaledJobRunner = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const logger_1 = tslib_1.__importDefault(require("../../application/logger"));
5
+ const logger_1 = tslib_1.__importDefault(require("../../../application/logger"));
6
6
  class RabbitMQScaledJobRunner {
7
7
  amqpQueue;
8
8
  queueName;
9
9
  vHost;
10
10
  handler;
11
11
  options;
12
- constructor(amqpQueue, queueName, vHost, handler, options) {
12
+ constructor(amqpQueue, queueName,
13
+ /**
14
+ * @deprecated The vHost already exists on amqpQueue, in v2 this field doesnt exists.
15
+ */
16
+ vHost, handler, options) {
13
17
  this.amqpQueue = amqpQueue;
14
18
  this.queueName = queueName;
15
19
  this.vHost = vHost;
16
20
  this.handler = handler;
17
- this.options = options;
21
+ this.setOptions(options);
22
+ }
23
+ setOptions(options) {
24
+ this.options = {
25
+ errorStrategy: options?.errorStrategy || 'nack-dlq',
26
+ manualAck: options?.manualAck || true,
27
+ };
18
28
  }
19
29
  async handleError(error, getResult) {
20
30
  if (!getResult) {
@@ -34,13 +44,17 @@ class RabbitMQScaledJobRunner {
34
44
  logger_1.default?.error('[RabbitMQScaledJobRunner] Falha ao aplicar estratégia de erro');
35
45
  }
36
46
  }
47
+ parse(raw) {
48
+ return JSON.parse(raw.content.toString());
49
+ }
37
50
  async finishProcess() {
38
51
  logger_1.default.debug()?.info('[RabbitMQScaledJobRunner] Finished process');
39
52
  }
40
53
  async run() {
41
54
  let getResult = null;
42
55
  let consumedMessages = 0;
43
- const messagesToGet = this.options.numberOfMessagesToGet ?? 1;
56
+ const timeoutPerMessageSeconds = this.handler.getTimeoutPerMessageSeconds();
57
+ const messagesToGet = this.options.numberOfMessagesToGet || this.handler.getSimultaneity() || 1;
44
58
  try {
45
59
  await this.amqpQueue.connect(this.vHost);
46
60
  while (consumedMessages < messagesToGet) {
@@ -53,14 +67,15 @@ class RabbitMQScaledJobRunner {
53
67
  return;
54
68
  }
55
69
  const { channel, rawMessage } = getResult;
56
- const msg = this.options.parse(rawMessage);
70
+ const msg = this.parse(rawMessage);
57
71
  logger_1.default
58
72
  .debug()
59
73
  ?.info(`[RabbitMQScaledJobRunner] Mensagem recebida ${JSON.stringify(msg, null, 2)}`);
60
- await Promise.race([
61
- this.handler.handle(msg),
62
- rejectTimeout(this.options.timeoutPerMessageMs),
63
- ]);
74
+ const promises = [this.handler.handle(msg)];
75
+ if (timeoutPerMessageSeconds) {
76
+ promises.push(rejectTimeout(timeoutPerMessageSeconds * 1000));
77
+ }
78
+ await Promise.race(promises);
64
79
  if (this.options.manualAck) {
65
80
  await this.amqpQueue.ack(channel, rawMessage);
66
81
  }
@@ -1,10 +1,10 @@
1
+ import { ScaledJobOptions } from '../types';
1
2
  export type ErrorStrategy = 'nack-dlq' | 'nack-requeue';
2
- export interface ScaledJobOptions {
3
- timeoutPerMessageMs: number;
4
- }
5
3
  export interface RabbitMQScaledJobOptions extends ScaledJobOptions {
6
- parse: (raw: any) => any;
4
+ /**
5
+ * @deprecated Use the simultaneity attribute on Handler.
6
+ */
7
+ numberOfMessagesToGet?: number;
7
8
  manualAck: boolean;
8
9
  errorStrategy: ErrorStrategy;
9
- numberOfMessagesToGet?: number;
10
10
  }
@@ -2,9 +2,10 @@ import Handler from '../../../../clean-arch/application/queue/handler';
2
2
  import { SQSScaledJobOptions } from './types';
3
3
  export declare class SQSScaledJobRunner {
4
4
  private readonly handler;
5
+ private readonly queueUrl;
5
6
  private readonly options;
6
7
  private sqs;
7
- constructor(handler: Handler, options: SQSScaledJobOptions);
8
+ constructor(handler: Handler, queueUrl: string, options: SQSScaledJobOptions);
8
9
  private static parseMessage;
9
10
  run(): Promise<void>;
10
11
  }
@@ -2,14 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SQSScaledJobRunner = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const logger_1 = tslib_1.__importDefault(require("../../../application/logger"));
6
5
  const client_sqs_1 = require("@aws-sdk/client-sqs");
6
+ const logger_1 = tslib_1.__importDefault(require("lib/clean-arch/application/logger"));
7
7
  class SQSScaledJobRunner {
8
8
  handler;
9
+ queueUrl;
9
10
  options;
10
11
  sqs;
11
- constructor(handler, options) {
12
+ constructor(handler, queueUrl, options) {
12
13
  this.handler = handler;
14
+ this.queueUrl = queueUrl;
13
15
  this.options = options;
14
16
  this.sqs = new client_sqs_1.SQSClient({ region: this.options.sqsRegion });
15
17
  }
@@ -25,10 +27,11 @@ class SQSScaledJobRunner {
25
27
  async run() {
26
28
  let consumedMessages = 0;
27
29
  const simultaneity = this.handler.getSimultaneity();
30
+ const timeoutPerMessageSeconds = this.handler.getTimeoutPerMessageSeconds();
28
31
  while (consumedMessages < simultaneity) {
29
32
  try {
30
33
  const resp = await this.sqs.send(new client_sqs_1.ReceiveMessageCommand({
31
- QueueUrl: this.options.queueUrl,
34
+ QueueUrl: this.queueUrl,
32
35
  MaxNumberOfMessages: 1,
33
36
  WaitTimeSeconds: this.options.waitTimeSeconds,
34
37
  }));
@@ -39,30 +42,25 @@ class SQSScaledJobRunner {
39
42
  }
40
43
  const [rawMessage] = messages;
41
44
  const message = SQSScaledJobRunner.parseMessage(rawMessage.Body);
42
- logger_1.default
43
- .debug()
44
- ?.info(`[SQSScaledJobRunner] Mensagem recebida ${JSON.stringify(message)}`);
45
- await Promise.race([
46
- this.handler.handle(message),
47
- rejectTimeout(this.options.timeoutPerMessageMs),
48
- ]);
49
- logger_1.default
50
- .debug()
51
- ?.info('[SQSScaledJobRunner] Mensagem processada com sucesso');
45
+ logger_1.default.info(`[SQSScaledJobRunner] Mensagem recebida ${JSON.stringify(message)}`);
46
+ const promises = [this.handler.handle(message)];
47
+ if (timeoutPerMessageSeconds) {
48
+ promises.push(rejectTimeout(timeoutPerMessageSeconds * 1000));
49
+ }
50
+ await Promise.race(promises);
51
+ logger_1.default.info('[SQSScaledJobRunner] Mensagem processada com sucesso');
52
52
  await this.sqs.send(new client_sqs_1.DeleteMessageCommand({
53
- QueueUrl: this.options.queueUrl,
53
+ QueueUrl: this.queueUrl,
54
54
  ReceiptHandle: rawMessage.ReceiptHandle,
55
55
  }));
56
56
  consumedMessages++;
57
- logger_1.default.debug()?.info('[SQSScaledJobRunner] Mensagem deletada');
57
+ logger_1.default.info('[SQSScaledJobRunner] Mensagem deletada');
58
58
  }
59
59
  catch (error) {
60
60
  logger_1.default.error(`[SQSScaledJobRunner] Erro: ${JSON.stringify(error)}`);
61
61
  }
62
62
  finally {
63
- logger_1.default
64
- .debug()
65
- ?.info(`[SQSScaledJobRunner] Mensagens consumidas: ${consumedMessages}`);
63
+ logger_1.default.info(`[SQSScaledJobRunner] Mensagens consumidas: ${consumedMessages}`);
66
64
  }
67
65
  }
68
66
  }
@@ -2,5 +2,4 @@ import { ScaledJobOptions } from '../types';
2
2
  export interface SQSScaledJobOptions extends ScaledJobOptions {
3
3
  waitTimeSeconds: number;
4
4
  sqsRegion: string;
5
- queueUrl: string;
6
5
  }
@@ -0,0 +1,2 @@
1
+ export interface ScaledJobOptions {
2
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1 @@
1
- declare function rejectTimeout(timeoutMs: number): Promise<never>;
1
+ declare function rejectTimeout(timeoutMs: number): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "1.101.41",
3
+ "version": "1.101.43",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,16 +0,0 @@
1
- import Handler from '../../../clean-arch/application/queue/handler';
2
- import AmqpQueue from '../queue/rabbitmq/amqp-lib';
3
- import { AmqpQueueGetResult } from '../queue/rabbitmq/types';
4
- import { RabbitMQScaledJobOptions } from './types';
5
- export declare class RabbitMQScaledJobRunner<T = any> {
6
- private readonly amqpQueue;
7
- private readonly queueName;
8
- private readonly vHost;
9
- private readonly handler;
10
- private readonly options;
11
- constructor(amqpQueue: AmqpQueue, queueName: string, vHost: string, handler: Handler, options: RabbitMQScaledJobOptions);
12
- private handleError;
13
- protected errorStrategy(getResult: AmqpQueueGetResult): Promise<void>;
14
- private finishProcess;
15
- run(): Promise<void>;
16
- }