@seidor-cloud-produtos/orbit-backend-lib 2.0.29 → 2.0.31

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.
@@ -5,6 +5,7 @@ const tslib_1 = require("tslib");
5
5
  const logger_in_memory_1 = tslib_1.__importDefault(require("../../infra/logger/logger-in-memory"));
6
6
  const env_1 = require("../../infra/environment/env");
7
7
  const types_1 = require("../../infra/environment/types");
8
+ const logger_1 = require("../logger");
8
9
  class Cache {
9
10
  elegibleClients;
10
11
  DEFAULT_EXPIRATION_SECONDS = env_1.env?.DEFAULT_CACHE_EXPIRATION || 360;
@@ -49,7 +50,13 @@ class Cache {
49
50
  }
50
51
  currentClient?.close();
51
52
  }
52
- await logger_in_memory_1.default.log(`Changed cache client to ${this.client.constructor.name}`, { level: types_1.LOG_LEVEL.warn });
53
+ await logger_in_memory_1.default.log(`Changed cache client to ${this.client.constructor.name}`, {
54
+ level: types_1.LOG_LEVEL.warn,
55
+ actor: 'cache',
56
+ actorType: logger_1.ActorEnum.SYSTEM,
57
+ category: logger_1.CategoryEnum.TECHNICAL,
58
+ type: 'cache-changed-client',
59
+ });
53
60
  return this;
54
61
  }
55
62
  async start(options) {
@@ -160,10 +167,13 @@ class Cache {
160
167
  }
161
168
  }
162
169
  async handleException(e) {
163
- try {
164
- await logger_in_memory_1.default.log(JSON.stringify(e), { level: types_1.LOG_LEVEL.error });
165
- }
166
- catch { }
170
+ await logger_in_memory_1.default.log(JSON.stringify(e), {
171
+ level: types_1.LOG_LEVEL.error,
172
+ actor: 'cache',
173
+ actorType: logger_1.ActorEnum.SYSTEM,
174
+ category: logger_1.CategoryEnum.TECHNICAL,
175
+ type: 'cache-handled-error',
176
+ });
167
177
  throw e;
168
178
  }
169
179
  async getOrFetchAndCache(keyCache, expiration = this.DEFAULT_EXPIRATION_SECONDS, promise, options) {
@@ -31,9 +31,13 @@ export default abstract class LoggerGateway {
31
31
  protected cache: Cache;
32
32
  constructor(cache: Cache);
33
33
  private static cacheKey;
34
- abstract register(data: any, props?: Partial<LogProperties>, options?: LogOptions): Promise<void>;
34
+ abstract register(data: any, props?: LogProperties, options?: LogOptions): Promise<void>;
35
35
  private static buildAcceptLogLevelValues;
36
- log(data: any, props?: Partial<LogProperties>, options?: LogOptions): Promise<void>;
37
- protected buildProps(props?: Partial<LogProperties>): Partial<LogProperties>;
38
- setLevel(level: LOG_LEVEL): Promise<void>;
36
+ log(data: any, props?: LogProperties, options?: LogOptions): Promise<void>;
37
+ protected buildProps(props?: LogProperties): LogProperties;
38
+ protected buildDefaultProps(): LogProperties;
39
+ setLevel(level: LOG_LEVEL, filter: {
40
+ logType: string;
41
+ logActor: string;
42
+ }, ttl?: number): Promise<void>;
39
43
  }
@@ -33,9 +33,10 @@ class LoggerGateway {
33
33
  }
34
34
  async log(data, props, options) {
35
35
  const buildedProps = this.buildProps();
36
- const cachedValue = await this.cache.get(LoggerGateway.cacheKey);
36
+ const cachedValue = await this.cache.get(`${LoggerGateway.cacheKey}:${props?.type}|*-${props?.actor}|*`);
37
37
  const acceptValues = LoggerGateway.buildAcceptLogLevelValues(cachedValue);
38
- if (acceptValues.includes(buildedProps.level || types_1.LOG_LEVEL.info)) {
38
+ if (props?.category === CategoryEnum.BUSINESS ||
39
+ acceptValues.includes(buildedProps.level || types_1.LOG_LEVEL.info)) {
39
40
  return await this.register(data, buildedProps, options);
40
41
  }
41
42
  }
@@ -43,14 +44,21 @@ class LoggerGateway {
43
44
  if (props) {
44
45
  return props;
45
46
  }
47
+ return this.buildDefaultProps();
48
+ }
49
+ buildDefaultProps() {
46
50
  return {
47
51
  level: types_1.LOG_LEVEL.info,
48
52
  dateTime: new Date(),
53
+ actor: 'default',
54
+ type: 'default',
55
+ actorType: ActorEnum.SYSTEM,
56
+ category: CategoryEnum.TECHNICAL,
49
57
  };
50
58
  }
51
- async setLevel(level) {
52
- const oneWeekInSeconds = 604800;
53
- return await this.cache.set(LoggerGateway.cacheKey, level, oneWeekInSeconds);
59
+ async setLevel(level, filter, ttl) {
60
+ const fourDaysInSeconds = 345600;
61
+ return await this.cache.set(`${LoggerGateway.cacheKey}:${filter.logType}-${filter.logActor}`, level, ttl || fourDaysInSeconds);
54
62
  }
55
63
  }
56
64
  exports.default = LoggerGateway;
@@ -1,7 +1,7 @@
1
1
  export default abstract class Handler {
2
2
  protected simultaneity: number;
3
- protected timeoutPerMessage: number | undefined;
3
+ protected timeoutPerMessageSeconds: number | undefined;
4
4
  getSimultaneity(): number;
5
- getTimeoutPerMessage(): number | undefined;
5
+ getTimeoutPerMessageSeconds(): number | undefined;
6
6
  abstract handle(message: any): Promise<void>;
7
7
  }
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class Handler {
4
4
  simultaneity = 1;
5
- timeoutPerMessage = undefined;
5
+ timeoutPerMessageSeconds = undefined;
6
6
  getSimultaneity() {
7
7
  return this.simultaneity;
8
8
  }
9
- getTimeoutPerMessage() {
10
- return this.timeoutPerMessage;
9
+ getTimeoutPerMessageSeconds() {
10
+ return this.timeoutPerMessageSeconds;
11
11
  }
12
12
  }
13
13
  exports.default = Handler;
@@ -21,5 +21,4 @@ export declare class LoggerOrbit extends LoggerGateway {
21
21
  protected buildProps(props?: Partial<LogProperties>): LogProperties & {
22
22
  expirationHours: number;
23
23
  };
24
- protected buildDefaultProps(): LogProperties;
25
24
  }
@@ -2,10 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LoggerOrbit = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const logger_1 = tslib_1.__importStar(require("../../application/logger"));
5
+ const logger_1 = tslib_1.__importDefault(require("../../application/logger"));
6
6
  const logger_in_memory_1 = tslib_1.__importDefault(require("./logger-in-memory"));
7
7
  const log_error_1 = tslib_1.__importDefault(require("../../domain/errors/log-error"));
8
- const types_1 = require("../environment/types");
9
8
  const sevenDaysInHours = 168;
10
9
  class LogEvent {
11
10
  input;
@@ -26,8 +25,8 @@ class LoggerOrbit extends logger_1.default {
26
25
  return await super.log(data, props, options);
27
26
  }
28
27
  async register(data, props, options) {
29
- const payload = this.buildPayload(data, props);
30
28
  try {
29
+ const payload = this.buildPayload(data, props);
31
30
  await this.queue.publish('logservice.common.direct', new LogEvent(payload));
32
31
  }
33
32
  catch (error) {
@@ -66,15 +65,5 @@ class LoggerOrbit extends logger_1.default {
66
65
  }
67
66
  return buildedProps;
68
67
  }
69
- buildDefaultProps() {
70
- return {
71
- actor: 'default',
72
- type: 'default',
73
- actorType: logger_1.ActorEnum.SYSTEM,
74
- category: logger_1.CategoryEnum.TECHNICAL,
75
- level: types_1.LOG_LEVEL.info,
76
- expirationHours: sevenDaysInHours,
77
- };
78
- }
79
68
  }
80
69
  exports.LoggerOrbit = LoggerOrbit;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RabbitMQScaledJobRunner = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const logger_in_memory_1 = tslib_1.__importDefault(require("../../logger/logger-in-memory"));
6
+ const timeout_1 = require("../../../shared/timeout");
6
7
  class RabbitMQScaledJobRunner {
7
8
  amqpQueue;
8
9
  queueName;
@@ -47,8 +48,8 @@ class RabbitMQScaledJobRunner {
47
48
  async run() {
48
49
  let getResult = null;
49
50
  let consumedMessages = 0;
51
+ const timeoutPerMessageSeconds = this.handler.getTimeoutPerMessageSeconds();
50
52
  const messagesToGet = this.handler.getSimultaneity();
51
- const timeoutPerMessage = this.handler.getTimeoutPerMessage();
52
53
  try {
53
54
  while (consumedMessages < messagesToGet) {
54
55
  try {
@@ -63,8 +64,8 @@ class RabbitMQScaledJobRunner {
63
64
  const msg = this.parse(rawMessage);
64
65
  logger_in_memory_1.default.log(`[RabbitMQScaledJobRunner] Mensagem recebida ${JSON.stringify(msg, null, 2)}`);
65
66
  const promises = [this.handler.handle(msg)];
66
- if (timeoutPerMessage) {
67
- promises.push(rejectTimeout(timeoutPerMessage));
67
+ if (timeoutPerMessageSeconds) {
68
+ promises.push((0, timeout_1.rejectTimeout)(timeoutPerMessageSeconds * 1000));
68
69
  }
69
70
  await Promise.race(promises);
70
71
  if (this.options.manualAck) {
@@ -5,6 +5,8 @@ const tslib_1 = require("tslib");
5
5
  const logger_in_memory_1 = tslib_1.__importDefault(require("../../logger/logger-in-memory"));
6
6
  const client_sqs_1 = require("@aws-sdk/client-sqs");
7
7
  const types_1 = require("../../environment/types");
8
+ const logger_1 = require("../../../../clean-arch/application/logger");
9
+ const timeout_1 = require("../../../../clean-arch/shared/timeout");
8
10
  class SQSScaledJobRunner {
9
11
  handler;
10
12
  queueUrl;
@@ -28,7 +30,7 @@ class SQSScaledJobRunner {
28
30
  async run() {
29
31
  let consumedMessages = 0;
30
32
  const simultaneity = this.handler.getSimultaneity();
31
- const timeoutPerMessage = this.handler.getTimeoutPerMessage();
33
+ const timeoutPerMessageSeconds = this.handler.getTimeoutPerMessageSeconds();
32
34
  while (consumedMessages < simultaneity) {
33
35
  try {
34
36
  const resp = await this.sqs.send(new client_sqs_1.ReceiveMessageCommand({
@@ -38,15 +40,21 @@ class SQSScaledJobRunner {
38
40
  }));
39
41
  const messages = resp.Messages ?? [];
40
42
  if (!messages.length) {
41
- logger_in_memory_1.default.log('[SQSScaledJobRunner] Nenhuma mensagem disponível.', { level: types_1.LOG_LEVEL.warn });
43
+ logger_in_memory_1.default.log('[SQSScaledJobRunner] Nenhuma mensagem disponível.', {
44
+ level: types_1.LOG_LEVEL.error,
45
+ actor: 'scaledjob-runner',
46
+ actorType: logger_1.ActorEnum.SYSTEM,
47
+ category: logger_1.CategoryEnum.TECHNICAL,
48
+ type: 'runner-without-messages',
49
+ });
42
50
  break;
43
51
  }
44
52
  const [rawMessage] = messages;
45
53
  const message = SQSScaledJobRunner.parseMessage(rawMessage.Body);
46
54
  logger_in_memory_1.default.log(`[SQSScaledJobRunner] Mensagem recebida ${JSON.stringify(message)}`);
47
55
  const promises = [this.handler.handle(message)];
48
- if (timeoutPerMessage) {
49
- promises.push(rejectTimeout(timeoutPerMessage));
56
+ if (timeoutPerMessageSeconds) {
57
+ promises.push((0, timeout_1.rejectTimeout)(timeoutPerMessageSeconds * 1000));
50
58
  }
51
59
  await Promise.race(promises);
52
60
  logger_in_memory_1.default.log('[SQSScaledJobRunner] Mensagem processada com sucesso');
@@ -58,7 +66,13 @@ class SQSScaledJobRunner {
58
66
  logger_in_memory_1.default.log('[SQSScaledJobRunner] Mensagem deletada');
59
67
  }
60
68
  catch (error) {
61
- logger_in_memory_1.default.log(`[SQSScaledJobRunner] Erro: ${JSON.stringify(error)}`, { level: types_1.LOG_LEVEL.error });
69
+ logger_in_memory_1.default.log(`[SQSScaledJobRunner] Erro: ${JSON.stringify(error)}`, {
70
+ level: types_1.LOG_LEVEL.error,
71
+ actor: 'scaledjob-runner',
72
+ actorType: logger_1.ActorEnum.SYSTEM,
73
+ category: logger_1.CategoryEnum.TECHNICAL,
74
+ type: 'scaledjob-runner-handled-error',
75
+ });
62
76
  }
63
77
  finally {
64
78
  logger_in_memory_1.default.log(`[SQSScaledJobRunner] Mensagens consumidas: ${consumedMessages}`);
@@ -1 +1 @@
1
- declare function rejectTimeout(timeoutMs: number): Promise<void>;
1
+ export declare function rejectTimeout(timeoutMs: number): Promise<void>;
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rejectTimeout = void 0;
2
4
  function rejectTimeout(timeoutMs) {
3
5
  return new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeoutMs));
4
6
  }
7
+ exports.rejectTimeout = rejectTimeout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seidor-cloud-produtos/orbit-backend-lib",
3
- "version": "2.0.29",
3
+ "version": "2.0.31",
4
4
  "description": "Internal lib for backend components",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",