franchising 0.5.1 → 0.7.0

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.
Files changed (97) hide show
  1. package/dist/cli/template-assets/api/src/main.ts +13 -11
  2. package/dist/cli/template-assets/api/src/modules/health/controller.ts +25 -4
  3. package/dist/cli/template-assets/api/src/shared/http/errors.ts +17 -0
  4. package/dist/cli/template-assets/cloudflare/cache.service.ts +1 -0
  5. package/dist/cli/template-assets/cloudflare/queue.service.ts +1 -0
  6. package/dist/cli/template-assets/cloudflare/worker.ts +4 -0
  7. package/dist/cli/template-assets/github/PULL_REQUEST_TEMPLATE.md +37 -0
  8. package/dist/cli/template-assets/observability/adapters.ts +37 -0
  9. package/dist/cli/template-assets/observability/bootstrap.ts +48 -0
  10. package/dist/cli/template-assets/observability/env.ts +21 -0
  11. package/dist/cli/template-assets/observability/index.ts +10 -0
  12. package/dist/cli/template-assets/observability/lifecycle.ts +97 -0
  13. package/dist/cli/template-assets/observability/local-index.ts +9 -0
  14. package/dist/cli/template-assets/observability/register.ts +12 -0
  15. package/dist/cli/template-assets/packages/jobs/src/index.ts +4 -40
  16. package/dist/cli/template-assets/packages/jobs/src/shared/queue/job-id.ts +9 -0
  17. package/dist/cli/template-assets/packages/jobs/src/shared/queue/options.ts +7 -0
  18. package/dist/cli/template-assets/packages/jobs/src/shared/queue/prefix.ts +18 -0
  19. package/dist/cli/template-assets/packages/queue/src/index.ts +4 -0
  20. package/dist/cli/template-assets/packages/queue/src/shared/errors/failure-reason.ts +7 -0
  21. package/dist/cli/template-assets/packages/{queue-runtime/src/index.ts → queue/src/shared/redis/connection.ts} +2 -15
  22. package/dist/cli/template-assets/packages/queue/src/types/redis/connection.ts +6 -0
  23. package/dist/cli/template-assets/packages/webhooks/src/index.ts +11 -0
  24. package/dist/cli/template-assets/packages/webhooks/src/shared/crypto/secret.ts +81 -0
  25. package/dist/cli/template-assets/packages/webhooks/src/shared/signing/headers.ts +43 -0
  26. package/dist/cli/template-assets/packages/webhooks/src/shared/url/deliverable.ts +58 -0
  27. package/dist/cli/template-assets/packages/webhooks/src/types/webhooks/job.ts +18 -0
  28. package/dist/cli/template-assets/packages/webhooks/test/webhooks.test.ts.tpl +69 -0
  29. package/dist/cli/template-assets/root/dot-dockerignore +88 -8
  30. package/dist/cli/template-assets/root/dot-oxlintrc.json +22 -0
  31. package/dist/cli/template-assets/root/lefthook.yml +32 -8
  32. package/dist/cli/template-assets/web/src/app/(app)/layout.tsx +2 -3
  33. package/dist/cli/template-assets/web/src/app/(auth)/login/page.tsx +1 -1
  34. package/dist/cli/template-assets/web/src/app/page.tsx +1 -0
  35. package/dist/cli/template-assets/web/src/{lib/api-client.ts → shared/api/client.ts} +11 -8
  36. package/dist/cli/template-assets/web/src/{lib/server-auth.ts → shared/auth/server.ts} +1 -2
  37. package/dist/cli/template-assets/web/src/types/api.ts +0 -15
  38. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/controller.ts +81 -0
  39. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/create-endpoint.dto.ts +25 -0
  40. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/dto/endpoint.dto.ts +28 -0
  41. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/module.ts +27 -0
  42. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/repository.ts +147 -0
  43. package/dist/cli/template-assets/webhooks/api/src/modules/webhooks/service.ts +119 -0
  44. package/dist/cli/template-assets/webhooks/api/src/types/webhooks/endpoint.ts +26 -0
  45. package/dist/cli/template-assets/webhooks/worker/src/env.ts +18 -0
  46. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/dispatcher.ts +119 -0
  47. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/module.ts +16 -0
  48. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/processor.ts +176 -0
  49. package/dist/cli/template-assets/webhooks/worker/src/modules/queue/repository.ts +187 -0
  50. package/dist/cli/template-assets/webhooks/worker/src/shared/http/deliver.ts +41 -0
  51. package/dist/cli/template-assets/webhooks/worker/src/types/webhooks/dispatch.ts +26 -0
  52. package/dist/cli/template-assets/worker/src/app.module.ts +14 -0
  53. package/dist/cli/template-assets/worker/src/main.ts +24 -0
  54. package/dist/cli/template-assets/worker/src/modules/queue/processor.ts +26 -3
  55. package/dist/cli/template-assets/worker/src/modules/queue/retry.ts +22 -7
  56. package/dist/cli/template-assets/worker/test/retry.test.ts.tpl +13 -1
  57. package/dist/cli/template-assets/worker-messaging/messaging-api-service.ts +6 -6
  58. package/dist/cli/template-assets/worker-messaging/src/env.ts +1 -1
  59. package/dist/cli/template-assets/worker-messaging/src/modules/messaging/service.ts +2 -4
  60. package/dist/cli/template-assets/worker-messaging/src/modules/queue/module.ts +1 -1
  61. package/dist/cli/template-assets/worker-messaging/src/modules/queue/processor.ts +32 -10
  62. package/dist/cli.js +631 -314
  63. package/dist/{index-BDYdEWl2.d.ts → index-B_byXBRW.d.cts} +10 -2
  64. package/dist/{index-BDYdEWl2.d.cts → index-B_byXBRW.d.ts} +10 -2
  65. package/dist/{index-u3ArUXO0.d.ts → index-CNlb6WNr.d.ts} +2 -2
  66. package/dist/{index--EJQTnHy.d.cts → index-I_Nx5SsQ.d.cts} +2 -2
  67. package/dist/index.cjs +1 -1
  68. package/dist/index.d.cts +1 -1
  69. package/dist/index.d.ts +1 -1
  70. package/dist/index.js +1 -1
  71. package/dist/messaging/index.cjs +1 -1
  72. package/dist/messaging/index.d.cts +1 -1
  73. package/dist/messaging/index.d.ts +1 -1
  74. package/dist/messaging/index.js +1 -1
  75. package/dist/sso/client.d.cts +7 -2
  76. package/dist/sso/client.d.ts +7 -2
  77. package/dist/sso/index.cjs +3 -1
  78. package/dist/sso/index.cjs.map +1 -1
  79. package/dist/sso/index.d.cts +1 -1
  80. package/dist/sso/index.d.ts +1 -1
  81. package/dist/sso/index.js +3 -1
  82. package/dist/sso/index.js.map +1 -1
  83. package/dist/sso/nestjs.d.cts +2 -2
  84. package/dist/sso/nestjs.d.ts +2 -2
  85. package/dist/sso/resource.d.cts +2 -2
  86. package/dist/sso/resource.d.ts +2 -2
  87. package/dist/version-Cm1EJ-YK.js +1 -0
  88. package/dist/{version-CrBclf1K.cjs → version-iERj9NJ6.cjs} +1 -1
  89. package/package.json +1 -1
  90. package/dist/cli/template-assets/api/src/errors/codes.ts +0 -17
  91. package/dist/cli/template-assets/api/src/errors/index.ts +0 -16
  92. package/dist/cli/template-assets/api/src/filters/exception.ts +0 -53
  93. package/dist/cli/template-assets/api/src/shared/exceptions.ts +0 -40
  94. package/dist/cli/template-assets/api/src/shared/response.ts +0 -31
  95. package/dist/cli/template-assets/api/src/telemetry.ts +0 -59
  96. package/dist/version-BhZpPjUh.js +0 -1
  97. /package/dist/cli/template-assets/web/src/{lib/auth-client.ts → shared/auth/client.ts} +0 -0
@@ -0,0 +1,187 @@
1
+ import { Injectable, type OnModuleDestroy } from '@nestjs/common';
2
+ // #if drizzle
3
+ import { and, arrayContains, asc, eq, inArray, isNull } from 'drizzle-orm';
4
+ import {
5
+ DatabaseService,
6
+ webhookDeliveries,
7
+ webhookEndpoints,
8
+ webhookOutbox,
9
+ } from '__ROCK_SCOPE__/database';
10
+ // #else
11
+ import { DatabaseService } from '__ROCK_SCOPE__/database';
12
+ // #endif
13
+ import type {
14
+ DeliveryAttempt,
15
+ DeliveryEndpoint,
16
+ DispatchedEvent,
17
+ } from '../../types/webhooks/dispatch';
18
+
19
+ // #if prisma
20
+ type OutboxRow = {
21
+ id: string;
22
+ ownerId: string;
23
+ eventType: string;
24
+ payload: unknown;
25
+ createdAt: Date;
26
+ };
27
+
28
+ // #endif
29
+ /**
30
+ * Outbox, endpoints e registro de entregas.
31
+ *
32
+ * @remarks
33
+ * `dispatchBatch` reserva o lote com `FOR UPDATE SKIP LOCKED`: duas replicas
34
+ * do worker pegam lotes diferentes, sem fila extra e sem lock global. O
35
+ * enfileiramento roda dentro da mesma transacao; se o Redis falhar, nada e
36
+ * marcado como despachado e o lote volta para a proxima rodada. O id de job
37
+ * estavel impede que esse retorno duplique entrega.
38
+ */
39
+ @Injectable()
40
+ export class WebhooksRepository implements OnModuleDestroy {
41
+ constructor(private readonly database: DatabaseService) {}
42
+
43
+ // #if drizzle
44
+ dispatchBatch(
45
+ limit: number,
46
+ enqueue: (events: DispatchedEvent[]) => Promise<void>,
47
+ ): Promise<number> {
48
+ return this.database.client.transaction(async (tx) => {
49
+ const events = await tx
50
+ .select()
51
+ .from(webhookOutbox)
52
+ .where(isNull(webhookOutbox.dispatchedAt))
53
+ .orderBy(asc(webhookOutbox.createdAt))
54
+ .limit(limit)
55
+ .for('update', { skipLocked: true });
56
+
57
+ if (events.length === 0) return 0;
58
+
59
+ const dispatched: DispatchedEvent[] = [];
60
+
61
+ for (const event of events) {
62
+ const endpoints = await tx
63
+ .select({ id: webhookEndpoints.id })
64
+ .from(webhookEndpoints)
65
+ .where(
66
+ and(
67
+ eq(webhookEndpoints.ownerId, event.ownerId),
68
+ eq(webhookEndpoints.enabled, true),
69
+ arrayContains(webhookEndpoints.eventTypes, [
70
+ event.eventType,
71
+ ]),
72
+ ),
73
+ );
74
+
75
+ dispatched.push({
76
+ id: event.id,
77
+ eventType: event.eventType,
78
+ payload: event.payload,
79
+ createdAt: event.createdAt,
80
+ endpointIds: endpoints.map((endpoint) => endpoint.id),
81
+ });
82
+ }
83
+
84
+ await enqueue(dispatched);
85
+
86
+ await tx
87
+ .update(webhookOutbox)
88
+ .set({ dispatchedAt: new Date() })
89
+ .where(
90
+ inArray(
91
+ webhookOutbox.id,
92
+ events.map((event) => event.id),
93
+ ),
94
+ );
95
+
96
+ return events.length;
97
+ });
98
+ }
99
+
100
+ async findEndpoint(id: string): Promise<DeliveryEndpoint | undefined> {
101
+ const [endpoint] = await this.database.client
102
+ .select({
103
+ url: webhookEndpoints.url,
104
+ secret: webhookEndpoints.secret,
105
+ enabled: webhookEndpoints.enabled,
106
+ })
107
+ .from(webhookEndpoints)
108
+ .where(eq(webhookEndpoints.id, id));
109
+
110
+ return endpoint;
111
+ }
112
+
113
+ async recordDelivery(attempt: DeliveryAttempt): Promise<void> {
114
+ await this.database.client.insert(webhookDeliveries).values(attempt);
115
+ }
116
+ // #else
117
+ dispatchBatch(
118
+ limit: number,
119
+ enqueue: (events: DispatchedEvent[]) => Promise<void>,
120
+ ): Promise<number> {
121
+ return this.database.client.$transaction(
122
+ async (tx) => {
123
+ const events = await tx.$queryRaw<OutboxRow[]>`
124
+ SELECT id, owner_id AS "ownerId", event_type AS "eventType",
125
+ payload, created_at AS "createdAt"
126
+ FROM webhook_outbox
127
+ WHERE dispatched_at IS NULL
128
+ ORDER BY created_at
129
+ LIMIT ${limit}
130
+ FOR UPDATE SKIP LOCKED
131
+ `;
132
+
133
+ if (events.length === 0) return 0;
134
+
135
+ const dispatched: DispatchedEvent[] = [];
136
+
137
+ for (const event of events) {
138
+ const endpoints = await tx.webhookEndpoint.findMany({
139
+ where: {
140
+ ownerId: event.ownerId,
141
+ enabled: true,
142
+ eventTypes: { has: event.eventType },
143
+ },
144
+ select: { id: true },
145
+ });
146
+
147
+ dispatched.push({
148
+ id: event.id,
149
+ eventType: event.eventType,
150
+ payload: event.payload,
151
+ createdAt: event.createdAt,
152
+ endpointIds: endpoints.map((endpoint) => endpoint.id),
153
+ });
154
+ }
155
+
156
+ await enqueue(dispatched);
157
+
158
+ await tx.webhookOutbox.updateMany({
159
+ where: { id: { in: events.map((event) => event.id) } },
160
+ data: { dispatchedAt: new Date() },
161
+ });
162
+
163
+ return events.length;
164
+ },
165
+ /* O enfileiramento no Redis roda dentro da transacao. */
166
+ { timeout: 30_000 },
167
+ );
168
+ }
169
+
170
+ async findEndpoint(id: string): Promise<DeliveryEndpoint | undefined> {
171
+ const endpoint = await this.database.client.webhookEndpoint.findUnique({
172
+ where: { id },
173
+ select: { url: true, secret: true, enabled: true },
174
+ });
175
+
176
+ return endpoint ?? void 0;
177
+ }
178
+
179
+ async recordDelivery(attempt: DeliveryAttempt): Promise<void> {
180
+ await this.database.client.webhookDelivery.create({ data: attempt });
181
+ }
182
+ // #endif
183
+
184
+ async onModuleDestroy(): Promise<void> {
185
+ await this.database.disconnect();
186
+ }
187
+ }
@@ -0,0 +1,41 @@
1
+ import type { WebhookHeaders } from '__ROCK_SCOPE__/webhooks';
2
+
3
+ /** Resposta fora de 2xx; `status` decide a retentativa em `isRetryable`. */
4
+ export class WebhookDeliveryError extends Error {
5
+ override readonly name = 'WebhookDeliveryError';
6
+
7
+ constructor(readonly status: number) {
8
+ super(`WEBHOOK_RESPONSE_${status}`);
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Faz o POST e devolve o status.
14
+ *
15
+ * @remarks
16
+ * `redirect: 'manual'`: seguir redirecionamento deixaria o receptor mandar o
17
+ * worker para a rede interna, contornando a checagem da URL; um 3xx conta
18
+ * como falha. O corpo da resposta e descartado, porque so o status importa
19
+ * e um receptor lento nao deve segurar a conexao.
20
+ */
21
+ export async function deliverWebhook(input: {
22
+ url: string;
23
+ body: string;
24
+ headers: WebhookHeaders;
25
+ timeoutMs: number;
26
+ fetch?: ((url: string, init: RequestInit) => Promise<Response>) | undefined;
27
+ }): Promise<number> {
28
+ const send = input.fetch ?? fetch;
29
+
30
+ const response = await send(input.url, {
31
+ method: 'POST',
32
+ headers: input.headers,
33
+ body: input.body,
34
+ redirect: 'manual',
35
+ signal: AbortSignal.timeout(input.timeoutMs),
36
+ });
37
+
38
+ await response.body?.cancel();
39
+
40
+ return response.status;
41
+ }
@@ -0,0 +1,26 @@
1
+ /** Evento reservado do outbox, com os endpoints inscritos nele. */
2
+ export type DispatchedEvent = {
3
+ id: string;
4
+ eventType: string;
5
+ payload: unknown;
6
+ createdAt: Date;
7
+ endpointIds: string[];
8
+ };
9
+
10
+ /** O que a entrega precisa do endpoint, lido de novo a cada tentativa. */
11
+ export type DeliveryEndpoint = {
12
+ url: string;
13
+ secret: string;
14
+ enabled: boolean;
15
+ };
16
+
17
+ /** Uma tentativa, gravada em `webhook_deliveries`. */
18
+ export type DeliveryAttempt = {
19
+ endpointId: string;
20
+ messageId: string;
21
+ eventType: string;
22
+ attempt: number;
23
+ status: 'succeeded' | 'failed';
24
+ responseStatus: number | null;
25
+ error: string | null;
26
+ };
@@ -1,5 +1,19 @@
1
1
  import { Module } from '@nestjs/common';
2
+ // #if telemetry
3
+ import { TelemetryModule } from 'franchising/telemetry';
4
+ import { adapters } from '__ROCK_SCOPE__/observability';
5
+ // #endif
2
6
  import { __ROCK_WORKER_CLASS__Module } from './modules/queue/module';
3
7
 
8
+ // #if telemetry
9
+ @Module({
10
+ imports: [
11
+ /* Mesmos adapters da API: o worker manda para o mesmo destino. */
12
+ TelemetryModule.forRoot({ adapters }),
13
+ __ROCK_WORKER_CLASS__Module,
14
+ ],
15
+ })
16
+ // #else
4
17
  @Module({ imports: [__ROCK_WORKER_CLASS__Module] })
18
+ // #endif
5
19
  export class AppModule {}
@@ -1,6 +1,16 @@
1
1
  import 'reflect-metadata';
2
+ // #if telemetry
3
+ /*
4
+ * Antes de qualquer `@nestjs/*`: a auto-instrumentacao precisa envolver o
5
+ * Nest, o Redis e os clients HTTP antes de eles existirem.
6
+ */
7
+ import '__ROCK_SCOPE__/observability/register';
8
+ import { NestFactory } from '@nestjs/core';
9
+ import { bootstrapFailed, observeProcess } from '__ROCK_SCOPE__/observability';
10
+ // #else
2
11
  import { Logger } from '@nestjs/common';
3
12
  import { NestFactory } from '@nestjs/core';
13
+ // #endif
4
14
  import { AppModule } from './app.module';
5
15
 
6
16
  /*
@@ -9,10 +19,22 @@ import { AppModule } from './app.module';
9
19
  * conexoes antes do processo morrer, para um job em andamento nao ficar preso.
10
20
  */
11
21
  async function bootstrap(): Promise<void> {
22
+ // #if telemetry
23
+ const app = await NestFactory.createApplicationContext(AppModule, {
24
+ bufferLogs: true,
25
+ });
26
+
27
+ observeProcess(app);
28
+ // #else
12
29
  const app = await NestFactory.createApplicationContext(AppModule);
30
+ // #endif
31
+
13
32
  app.enableShutdownHooks();
14
33
  }
15
34
 
35
+ // #if telemetry
36
+ void bootstrap().catch(bootstrapFailed('o worker __ROCK_WORKER_NAME__'));
37
+ // #else
16
38
  void bootstrap().catch((error: unknown) => {
17
39
  Logger.error(
18
40
  error instanceof Error
@@ -21,5 +43,7 @@ void bootstrap().catch((error: unknown) => {
21
43
  undefined,
22
44
  'Bootstrap',
23
45
  );
46
+
24
47
  process.exitCode = 1;
25
48
  });
49
+ // #endif
@@ -1,15 +1,21 @@
1
1
  import {
2
2
  Injectable,
3
+ // #if !telemetry
3
4
  Logger,
5
+ // #endif
4
6
  type OnModuleDestroy,
5
7
  type OnModuleInit,
6
8
  } from '@nestjs/common';
7
9
  import { UnrecoverableError, Worker } from 'bullmq';
10
+ // #if telemetry
11
+ import { Telemetry } from 'franchising/telemetry';
12
+ import { observeWorker, runObservedJob } from '__ROCK_SCOPE__/observability';
13
+ // #endif
8
14
  import {
9
15
  createWorkerRedisConnection,
10
16
  failureReason,
11
17
  type QueueRedisConnection,
12
- } from '__ROCK_SCOPE__/queue-runtime';
18
+ } from '__ROCK_SCOPE__/queue';
13
19
  import { __ROCK_WORKER_QUEUE_CONST__, queuePrefix } from '__ROCK_SCOPE__/jobs';
14
20
  import { env } from '../../env';
15
21
  import {
@@ -33,19 +39,33 @@ import { isRetryable } from './retry';
33
39
  export class __ROCK_WORKER_CLASS__Processor
34
40
  implements OnModuleInit, OnModuleDestroy
35
41
  {
42
+ // #if !telemetry
36
43
  private readonly logger = new Logger(__ROCK_WORKER_CLASS__Processor.name);
44
+ // #endif
37
45
  private worker?: Worker;
38
46
  private connection?: QueueRedisConnection;
39
47
 
48
+ // #if telemetry
40
49
  constructor(
41
50
  private readonly repository: __ROCK_WORKER_CLASS__Repository,
51
+ private readonly telemetry: Telemetry,
42
52
  ) {}
53
+ // #else
54
+ constructor(
55
+ private readonly repository: __ROCK_WORKER_CLASS__Repository,
56
+ ) {}
57
+ // #endif
43
58
 
44
59
  onModuleInit(): void {
45
60
  this.connection = createWorkerRedisConnection(env.QUEUE_REDIS_URL);
61
+
46
62
  this.worker = new Worker(
47
63
  __ROCK_WORKER_QUEUE_CONST__,
64
+ // #if telemetry
65
+ (job) => runObservedJob(this.telemetry, job, () => this.process(job)),
66
+ // #else
48
67
  (job) => this.process(job),
68
+ // #endif
49
69
  {
50
70
  connection: this.connection.client,
51
71
  prefix: queuePrefix(
@@ -60,6 +80,9 @@ export class __ROCK_WORKER_CLASS__Processor
60
80
  },
61
81
  );
62
82
 
83
+ // #if telemetry
84
+ observeWorker(this.worker, this.telemetry);
85
+ // #else
63
86
  this.worker.on('failed', (job, error) => {
64
87
  this.logger.error(
65
88
  JSON.stringify({
@@ -79,6 +102,7 @@ export class __ROCK_WORKER_CLASS__Processor
79
102
  }),
80
103
  );
81
104
  });
105
+ // #endif
82
106
  }
83
107
 
84
108
  async onModuleDestroy(): Promise<void> {
@@ -95,9 +119,8 @@ export class __ROCK_WORKER_CLASS__Processor
95
119
  * `UnrecoverableError` tira o job da fila na hora. Sem isso um
96
120
  * payload invalido ficaria repetindo ate esgotar as tentativas.
97
121
  */
98
- if (!isRetryable(error)) {
122
+ if (!isRetryable(error))
99
123
  throw new UnrecoverableError(failureReason(error));
100
- }
101
124
 
102
125
  throw error;
103
126
  }
@@ -1,17 +1,32 @@
1
+ /** Status HTTP de um erro: `HttpError`/`HttpException` ou `{ status }`. */
2
+ function statusOf(error: unknown): number | undefined {
3
+ const candidate = error as
4
+ | { status?: unknown; getStatus?: () => number }
5
+ | undefined;
6
+
7
+ if (typeof candidate?.getStatus === 'function') return candidate.getStatus();
8
+
9
+ return typeof candidate?.status === 'number' ? candidate.status : void 0;
10
+ }
11
+
1
12
  /**
2
13
  * Decide se vale tentar o job de novo.
3
14
  *
4
15
  * @remarks
5
- * Erro de rede ou indisponibilidade tende a passar sozinho, entao vale
6
- * retentar. Erro de payload invalido nao muda com o tempo: retentar so queima
7
- * as tentativas e atrasa a ida para a fila de falhas.
16
+ * Erro de rede, 5xx, 408 e 429 tendem a passar sozinhos, entao vale retentar.
17
+ * Payload invalido (`TypeError`, o `ValidationFailed` do `franchising/http`) e
18
+ * os outros 4xx nao mudam com o tempo: retentar so queima as tentativas e
19
+ * atrasa a ida para a fila de falhas.
8
20
  */
9
21
  export function isRetryable(error: unknown): boolean {
10
- if (error instanceof TypeError) {
22
+ if (error instanceof TypeError) return false;
23
+
24
+ if ((error as { code?: unknown } | undefined)?.code === 'ValidationFailed')
11
25
  return false;
12
- }
13
26
 
14
- const code = (error as { code?: string } | undefined)?.code;
27
+ const status = statusOf(error);
28
+
29
+ if (status === void 0) return true;
15
30
 
16
- return code !== 'VALIDATION_FAILED';
31
+ return status >= 500 || status === 408 || status === 429;
17
32
  }
@@ -7,6 +7,18 @@ describe('isRetryable', () => {
7
7
  });
8
8
 
9
9
  it('nao retenta payload invalido', () => {
10
- expect(isRetryable(Object.assign(new Error('bad'), { code: 'VALIDATION_FAILED' }))).toBe(false);
10
+ expect(
11
+ isRetryable(Object.assign(new Error('bad'), { code: 'ValidationFailed' })),
12
+ ).toBe(false);
13
+
14
+ expect(isRetryable(new TypeError('INVALID_JOB'))).toBe(false);
15
+ });
16
+
17
+ it('retenta 5xx, 408 e 429, e nao os outros 4xx', () => {
18
+ for (const status of [500, 503, 408, 429])
19
+ expect(isRetryable({ status })).toBe(true);
20
+
21
+ for (const status of [400, 401, 404, 410])
22
+ expect(isRetryable({ status })).toBe(false);
11
23
  });
12
24
  });
@@ -2,29 +2,29 @@ import { Injectable, type OnModuleDestroy } from '@nestjs/common';
2
2
  import { Queue } from 'bullmq';
3
3
  import {
4
4
  DEFAULT_JOB_OPTIONS,
5
- MESSAGING_QUEUE_NAME,
5
+ EMAILS_QUEUE_NAME,
6
6
  queuePrefix,
7
7
  type SendEmailJob,
8
8
  } from '__ROCK_SCOPE__/jobs';
9
- import { createProducerRedisConnection } from '__ROCK_SCOPE__/queue-runtime';
9
+ import { createProducerRedisConnection } from '__ROCK_SCOPE__/queue';
10
10
  import { env } from '../../env';
11
11
 
12
12
  /**
13
- * Publica e-mails na fila do worker `messaging`.
13
+ * Publica e-mails na fila do worker `emails`.
14
14
  *
15
15
  * @remarks
16
16
  * A API nao fala com o provedor: enfileira e responde. O envio, as
17
17
  * retentativas e a credencial da plataforma Messaging ficam em
18
- * `apps/workers/messaging`, entao uma lentidao no provedor nao segura a
18
+ * `apps/workers/emails`, entao uma lentidao no provedor nao segura a
19
19
  * requisicao de quem pediu o e-mail.
20
20
  */
21
21
  @Injectable()
22
22
  export class MessagingService implements OnModuleDestroy {
23
23
  private readonly connection = createProducerRedisConnection(env.REDIS_URL);
24
24
 
25
- private readonly queue = new Queue<SendEmailJob>(MESSAGING_QUEUE_NAME, {
25
+ private readonly queue = new Queue<SendEmailJob>(EMAILS_QUEUE_NAME, {
26
26
  connection: this.connection.client,
27
- prefix: queuePrefix(env.QUEUE_PREFIX, MESSAGING_QUEUE_NAME),
27
+ prefix: queuePrefix(env.QUEUE_PREFIX, EMAILS_QUEUE_NAME),
28
28
  defaultJobOptions: DEFAULT_JOB_OPTIONS,
29
29
  });
30
30
 
@@ -8,7 +8,7 @@ export const env = cleanEnv(process.env, {
8
8
  }),
9
9
  QUEUE_REDIS_URL: str({ default: 'redis://localhost:6379' }),
10
10
  QUEUE_PREFIX: str({ default: '__ROCK_PROJECT_SLUG__' }),
11
- MESSAGING_CONCURRENCY: num({ default: 5 }),
11
+ __ROCK_WORKER_CONCURRENCY_ENV__: num({ default: 5 }),
12
12
  MESSAGING_API_KEY: str({ default: '' }),
13
13
  MESSAGING_FROM: str({ default: 'nao-responda@rockfeller.com.br' }),
14
14
  });
@@ -29,9 +29,8 @@ export class MessagingService {
29
29
  * desenvolvimento o conteudo vai para o console, para testar links
30
30
  * de login e redefinicao sem conta na plataforma.
31
31
  */
32
- if (env.NODE_ENV === 'production') {
32
+ if (env.NODE_ENV === 'production')
33
33
  throw new Error('MESSAGING_API_KEY_REQUIRED');
34
- }
35
34
 
36
35
  this.logger.warn(
37
36
  JSON.stringify({
@@ -52,8 +51,7 @@ export class MessagingService {
52
51
  html: input.html,
53
52
  });
54
53
 
55
- if (error) {
54
+ if (error)
56
55
  throw new Error(`EMAIL_SEND_FAILED: ${error.code}`);
57
- }
58
56
  }
59
57
  }
@@ -5,4 +5,4 @@ import { MessagingProcessor } from './processor';
5
5
  @Module({
6
6
  providers: [MessagingService, MessagingProcessor],
7
7
  })
8
- export class MessagingModule {}
8
+ export class __ROCK_WORKER_CLASS__Module {}
@@ -1,17 +1,23 @@
1
1
  import {
2
2
  Injectable,
3
+ // #if !telemetry
3
4
  Logger,
5
+ // #endif
4
6
  type OnModuleDestroy,
5
7
  type OnModuleInit,
6
8
  } from '@nestjs/common';
7
9
  import { UnrecoverableError, Worker } from 'bullmq';
10
+ // #if telemetry
11
+ import { Telemetry } from 'franchising/telemetry';
12
+ import { observeWorker, runObservedJob } from '__ROCK_SCOPE__/observability';
13
+ // #endif
8
14
  import {
9
15
  createWorkerRedisConnection,
10
16
  failureReason,
11
17
  type QueueRedisConnection,
12
- } from '__ROCK_SCOPE__/queue-runtime';
18
+ } from '__ROCK_SCOPE__/queue';
13
19
  import {
14
- MESSAGING_QUEUE_NAME,
20
+ __ROCK_WORKER_QUEUE_CONST__,
15
21
  queuePrefix,
16
22
  type SendEmailJob,
17
23
  } from '__ROCK_SCOPE__/jobs';
@@ -33,15 +39,14 @@ function parseSendEmailJob(data: unknown): SendEmailJob {
33
39
  typeof job?.to !== 'string' ||
34
40
  typeof job.subject !== 'string' ||
35
41
  typeof job.html !== 'string'
36
- ) {
42
+ )
37
43
  throw new TypeError('INVALID_EMAIL_JOB');
38
- }
39
44
 
40
45
  return { to: job.to, subject: job.subject, html: job.html };
41
46
  }
42
47
 
43
48
  /**
44
- * Consumidor da fila `messaging`.
49
+ * Consumidor da fila `__ROCK_WORKER_QUEUE__`.
45
50
  *
46
51
  * @remarks
47
52
  * A API so publica o e-mail; o envio, as retentativas e a credencial da
@@ -51,21 +56,35 @@ function parseSendEmailJob(data: unknown): SendEmailJob {
51
56
  */
52
57
  @Injectable()
53
58
  export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
59
+ // #if !telemetry
54
60
  private readonly logger = new Logger(MessagingProcessor.name);
61
+ // #endif
55
62
  private worker?: Worker;
56
63
  private connection?: QueueRedisConnection;
57
64
 
65
+ // #if telemetry
66
+ constructor(
67
+ private readonly messaging: MessagingService,
68
+ private readonly telemetry: Telemetry,
69
+ ) {}
70
+ // #else
58
71
  constructor(private readonly messaging: MessagingService) {}
72
+ // #endif
59
73
 
60
74
  onModuleInit(): void {
61
75
  this.connection = createWorkerRedisConnection(env.QUEUE_REDIS_URL);
76
+
62
77
  this.worker = new Worker(
63
- MESSAGING_QUEUE_NAME,
78
+ __ROCK_WORKER_QUEUE_CONST__,
79
+ // #if telemetry
80
+ (job) => runObservedJob(this.telemetry, job, () => this.process(job)),
81
+ // #else
64
82
  (job) => this.process(job),
83
+ // #endif
65
84
  {
66
85
  connection: this.connection.client,
67
- prefix: queuePrefix(env.QUEUE_PREFIX, MESSAGING_QUEUE_NAME),
68
- concurrency: env.MESSAGING_CONCURRENCY,
86
+ prefix: queuePrefix(env.QUEUE_PREFIX, __ROCK_WORKER_QUEUE_CONST__),
87
+ concurrency: env.__ROCK_WORKER_CONCURRENCY_ENV__,
69
88
  limiter: { max: RATE_LIMIT_PER_SECOND, duration: 1000 },
70
89
  lockDuration: LOCK_DURATION_MS,
71
90
  stalledInterval: STALLED_INTERVAL_MS,
@@ -73,6 +92,9 @@ export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
73
92
  },
74
93
  );
75
94
 
95
+ // #if telemetry
96
+ observeWorker(this.worker, this.telemetry);
97
+ // #else
76
98
  this.worker.on('failed', (job, error) => {
77
99
  this.logger.error(
78
100
  JSON.stringify({
@@ -92,6 +114,7 @@ export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
92
114
  }),
93
115
  );
94
116
  });
117
+ // #endif
95
118
  }
96
119
 
97
120
  async onModuleDestroy(): Promise<void> {
@@ -103,9 +126,8 @@ export class MessagingProcessor implements OnModuleInit, OnModuleDestroy {
103
126
  try {
104
127
  await this.messaging.sendEmail(parseSendEmailJob(job.data));
105
128
  } catch (error) {
106
- if (!isRetryable(error)) {
129
+ if (!isRetryable(error))
107
130
  throw new UnrecoverableError(failureReason(error));
108
- }
109
131
 
110
132
  throw error;
111
133
  }