@seidor-cloud-produtos/orbit-backend-lib 1.101.40 → 1.101.41
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/clean-arch/infra/scaledjob/runner.d.ts +0 -1
- package/dist/clean-arch/infra/scaledjob/runner.js +7 -5
- package/dist/clean-arch/infra/scaledjob/sqs/runner.d.ts +10 -0
- package/dist/clean-arch/infra/scaledjob/sqs/runner.js +70 -0
- package/dist/clean-arch/infra/scaledjob/sqs/types.d.ts +6 -0
- package/dist/clean-arch/infra/scaledjob/sqs/types.js +2 -0
- package/dist/clean-arch/infra/scaledjob/types.d.ts +6 -4
- package/dist/clean-arch/shared/timeout.d.ts +1 -0
- package/dist/clean-arch/shared/timeout.js +4 -0
- package/package.json +2 -1
|
@@ -11,7 +11,6 @@ export declare class RabbitMQScaledJobRunner<T = any> {
|
|
|
11
11
|
constructor(amqpQueue: AmqpQueue, queueName: string, vHost: string, handler: Handler, options: RabbitMQScaledJobOptions);
|
|
12
12
|
private handleError;
|
|
13
13
|
protected errorStrategy(getResult: AmqpQueueGetResult): Promise<void>;
|
|
14
|
-
private rejectTimeout;
|
|
15
14
|
private finishProcess;
|
|
16
15
|
run(): Promise<void>;
|
|
17
16
|
}
|
|
@@ -34,9 +34,6 @@ class RabbitMQScaledJobRunner {
|
|
|
34
34
|
logger_1.default?.error('[RabbitMQScaledJobRunner] Falha ao aplicar estratégia de erro');
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
rejectTimeout() {
|
|
38
|
-
return new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), this.options.timeoutMs));
|
|
39
|
-
}
|
|
40
37
|
async finishProcess() {
|
|
41
38
|
logger_1.default.debug()?.info('[RabbitMQScaledJobRunner] Finished process');
|
|
42
39
|
}
|
|
@@ -60,7 +57,10 @@ class RabbitMQScaledJobRunner {
|
|
|
60
57
|
logger_1.default
|
|
61
58
|
.debug()
|
|
62
59
|
?.info(`[RabbitMQScaledJobRunner] Mensagem recebida ${JSON.stringify(msg, null, 2)}`);
|
|
63
|
-
await Promise.race([
|
|
60
|
+
await Promise.race([
|
|
61
|
+
this.handler.handle(msg),
|
|
62
|
+
rejectTimeout(this.options.timeoutPerMessageMs),
|
|
63
|
+
]);
|
|
64
64
|
if (this.options.manualAck) {
|
|
65
65
|
await this.amqpQueue.ack(channel, rawMessage);
|
|
66
66
|
}
|
|
@@ -75,7 +75,9 @@ class RabbitMQScaledJobRunner {
|
|
|
75
75
|
if (getResult) {
|
|
76
76
|
getResult = null;
|
|
77
77
|
consumedMessages += 1;
|
|
78
|
-
logger_1.default
|
|
78
|
+
logger_1.default
|
|
79
|
+
.debug()
|
|
80
|
+
?.info(`[RabbitMQScaledJobRunner] Mensagens consumidas: ${consumedMessages}`);
|
|
79
81
|
}
|
|
80
82
|
}
|
|
81
83
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Handler from '../../../../clean-arch/application/queue/handler';
|
|
2
|
+
import { SQSScaledJobOptions } from './types';
|
|
3
|
+
export declare class SQSScaledJobRunner {
|
|
4
|
+
private readonly handler;
|
|
5
|
+
private readonly options;
|
|
6
|
+
private sqs;
|
|
7
|
+
constructor(handler: Handler, options: SQSScaledJobOptions);
|
|
8
|
+
private static parseMessage;
|
|
9
|
+
run(): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SQSScaledJobRunner = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const logger_1 = tslib_1.__importDefault(require("../../../application/logger"));
|
|
6
|
+
const client_sqs_1 = require("@aws-sdk/client-sqs");
|
|
7
|
+
class SQSScaledJobRunner {
|
|
8
|
+
handler;
|
|
9
|
+
options;
|
|
10
|
+
sqs;
|
|
11
|
+
constructor(handler, options) {
|
|
12
|
+
this.handler = handler;
|
|
13
|
+
this.options = options;
|
|
14
|
+
this.sqs = new client_sqs_1.SQSClient({ region: this.options.sqsRegion });
|
|
15
|
+
}
|
|
16
|
+
static parseMessage(rawMessage) {
|
|
17
|
+
if (rawMessage === null || rawMessage === undefined) {
|
|
18
|
+
throw new Error('[SQSScaledJobRunner] Mensagem inválida.');
|
|
19
|
+
}
|
|
20
|
+
if (typeof rawMessage !== 'string') {
|
|
21
|
+
throw new Error('[SQSScaledJobRunner] Mensagem deve ser uma string.');
|
|
22
|
+
}
|
|
23
|
+
return JSON.parse(rawMessage);
|
|
24
|
+
}
|
|
25
|
+
async run() {
|
|
26
|
+
let consumedMessages = 0;
|
|
27
|
+
const simultaneity = this.handler.getSimultaneity();
|
|
28
|
+
while (consumedMessages < simultaneity) {
|
|
29
|
+
try {
|
|
30
|
+
const resp = await this.sqs.send(new client_sqs_1.ReceiveMessageCommand({
|
|
31
|
+
QueueUrl: this.options.queueUrl,
|
|
32
|
+
MaxNumberOfMessages: 1,
|
|
33
|
+
WaitTimeSeconds: this.options.waitTimeSeconds,
|
|
34
|
+
}));
|
|
35
|
+
const messages = resp.Messages ?? [];
|
|
36
|
+
if (!messages.length) {
|
|
37
|
+
logger_1.default.warning('[SQSScaledJobRunner] Nenhuma mensagem disponível.');
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
const [rawMessage] = messages;
|
|
41
|
+
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');
|
|
52
|
+
await this.sqs.send(new client_sqs_1.DeleteMessageCommand({
|
|
53
|
+
QueueUrl: this.options.queueUrl,
|
|
54
|
+
ReceiptHandle: rawMessage.ReceiptHandle,
|
|
55
|
+
}));
|
|
56
|
+
consumedMessages++;
|
|
57
|
+
logger_1.default.debug()?.info('[SQSScaledJobRunner] Mensagem deletada');
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
logger_1.default.error(`[SQSScaledJobRunner] Erro: ${JSON.stringify(error)}`);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
logger_1.default
|
|
64
|
+
.debug()
|
|
65
|
+
?.info(`[SQSScaledJobRunner] Mensagens consumidas: ${consumedMessages}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.SQSScaledJobRunner = SQSScaledJobRunner;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export type ErrorStrategy =
|
|
2
|
-
export
|
|
1
|
+
export type ErrorStrategy = 'nack-dlq' | 'nack-requeue';
|
|
2
|
+
export interface ScaledJobOptions {
|
|
3
|
+
timeoutPerMessageMs: number;
|
|
4
|
+
}
|
|
5
|
+
export interface RabbitMQScaledJobOptions extends ScaledJobOptions {
|
|
3
6
|
parse: (raw: any) => any;
|
|
4
7
|
manualAck: boolean;
|
|
5
8
|
errorStrategy: ErrorStrategy;
|
|
6
|
-
timeoutMs: number;
|
|
7
9
|
numberOfMessagesToGet?: number;
|
|
8
|
-
}
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare function rejectTimeout(timeoutMs: number): Promise<never>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seidor-cloud-produtos/orbit-backend-lib",
|
|
3
|
-
"version": "1.101.
|
|
3
|
+
"version": "1.101.41",
|
|
4
4
|
"description": "Internal lib for backend components",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"vitest": "^0.32.0"
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
|
+
"@aws-sdk/client-sqs": "3.940.0",
|
|
81
82
|
"@fastify/cors": "8.3.0",
|
|
82
83
|
"@nestjs/common": "8.4.7",
|
|
83
84
|
"amqplib": "0.10.9",
|