@venizia/ignis-helpers 0.4.8-8 → 0.4.8

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 (199) hide show
  1. package/README.md +2 -3
  2. package/dist/esm/common/constants/app.d.ts +1 -3
  3. package/dist/esm/common/constants/app.js +1 -11
  4. package/dist/esm/common/constants/app.js.map +1 -1
  5. package/dist/esm/common/constants/http.d.ts +4 -0
  6. package/dist/esm/common/constants/http.js +5 -0
  7. package/dist/esm/common/constants/http.js.map +1 -1
  8. package/dist/esm/common/constants/mime.d.ts +0 -2
  9. package/dist/esm/common/constants/mime.js +0 -4
  10. package/dist/esm/common/constants/mime.js.map +1 -1
  11. package/dist/esm/common/redact.js.map +1 -1
  12. package/dist/esm/common/types/class.d.ts +14 -12
  13. package/dist/esm/common/types/class.js.map +1 -1
  14. package/dist/esm/common/types/const-value.d.ts +3 -3
  15. package/dist/esm/common/types/const-value.js.map +1 -1
  16. package/dist/esm/common/types/field-mapping.d.ts +6 -8
  17. package/dist/esm/common/types/field-mapping.js +0 -10
  18. package/dist/esm/common/types/field-mapping.js.map +1 -1
  19. package/dist/esm/common/types/index.d.ts +1 -0
  20. package/dist/esm/common/types/index.js +1 -0
  21. package/dist/esm/common/types/index.js.map +1 -1
  22. package/dist/esm/common/types/injection.d.ts +1 -1
  23. package/dist/esm/common/types/injection.js.map +1 -1
  24. package/dist/esm/common/types/json.d.ts +14 -0
  25. package/dist/esm/common/types/json.js +2 -0
  26. package/dist/esm/common/types/json.js.map +1 -0
  27. package/dist/esm/common/types/utility.d.ts +0 -4
  28. package/dist/esm/common/types/utility.js.map +1 -1
  29. package/dist/esm/modules/env/app-env.d.ts +17 -8
  30. package/dist/esm/modules/env/app-env.js +18 -15
  31. package/dist/esm/modules/env/app-env.js.map +1 -1
  32. package/dist/esm/modules/env/common/types.d.ts +22 -6
  33. package/dist/esm/modules/env/common/types.js.map +1 -1
  34. package/dist/esm/modules/logger/base/abstract.d.ts +6 -7
  35. package/dist/esm/modules/logger/base/abstract.js.map +1 -1
  36. package/dist/esm/modules/logger/base/base.d.ts +7 -8
  37. package/dist/esm/modules/logger/base/base.js.map +1 -1
  38. package/dist/esm/modules/logger/base/console.d.ts +6 -7
  39. package/dist/esm/modules/logger/base/console.js.map +1 -1
  40. package/dist/esm/modules/logger/common/types.d.ts +7 -7
  41. package/dist/esm/modules/logger/common/types.js.map +1 -1
  42. package/dist/esm/modules/logger/factory.js.map +1 -1
  43. package/dist/esm/modules/logger/hf/flusher.js.map +1 -1
  44. package/dist/esm/modules/logger/hf/logger.d.ts +6 -7
  45. package/dist/esm/modules/logger/hf/logger.js.map +1 -1
  46. package/dist/esm/modules/logger/pino/backing.js.map +1 -1
  47. package/dist/esm/modules/logger/pino/destination.d.ts +1 -1
  48. package/dist/esm/modules/logger/pino/destination.js.map +1 -1
  49. package/dist/esm/modules/logger/pino/logger.d.ts +1 -2
  50. package/dist/esm/modules/logger/pino/logger.js.map +1 -1
  51. package/dist/esm/modules/logger/winston/formats.js +2 -2
  52. package/dist/esm/modules/logger/winston/formats.js.map +1 -1
  53. package/dist/esm/modules/logger/winston/logger.d.ts +1 -2
  54. package/dist/esm/modules/logger/winston/logger.js.map +1 -1
  55. package/dist/esm/modules/logger/winston/transports/dgram.transport.d.ts +1 -1
  56. package/dist/esm/modules/logger/winston/transports/dgram.transport.js +1 -1
  57. package/dist/esm/modules/logger/winston/transports/dgram.transport.js.map +1 -1
  58. package/dist/esm/modules/network/http-request/common/constants.d.ts +0 -2
  59. package/dist/esm/modules/network/http-request/common/constants.js +0 -4
  60. package/dist/esm/modules/network/http-request/common/constants.js.map +1 -1
  61. package/dist/esm/modules/network/http-request/fetcher/axios-fetcher.d.ts +6 -6
  62. package/dist/esm/modules/network/http-request/fetcher/axios-fetcher.js.map +1 -1
  63. package/dist/esm/modules/network/http-request/fetcher/base-fetcher.d.ts +2 -3
  64. package/dist/esm/modules/network/http-request/fetcher/base-fetcher.js.map +1 -1
  65. package/dist/esm/modules/network/http-request/fetcher/common/types.d.ts +1 -1
  66. package/dist/esm/modules/network/http-request/fetcher/common/types.js.map +1 -1
  67. package/dist/esm/modules/network/http-request/fetcher/node-fetcher.d.ts +1 -2
  68. package/dist/esm/modules/network/http-request/fetcher/node-fetcher.js +5 -6
  69. package/dist/esm/modules/network/http-request/fetcher/node-fetcher.js.map +1 -1
  70. package/dist/esm/modules/network/tcp-socket/base-tcp-client.helper.d.ts +4 -4
  71. package/dist/esm/modules/network/tcp-socket/base-tcp-client.helper.js +1 -1
  72. package/dist/esm/modules/network/tcp-socket/base-tcp-client.helper.js.map +1 -1
  73. package/dist/esm/modules/network/tcp-socket/base-tcp-server.helper.d.ts +3 -5
  74. package/dist/esm/modules/network/tcp-socket/base-tcp-server.helper.js +0 -8
  75. package/dist/esm/modules/network/tcp-socket/base-tcp-server.helper.js.map +1 -1
  76. package/dist/esm/modules/network/utilities/query-string.d.ts +2 -2
  77. package/dist/esm/modules/network/utilities/query-string.js.map +1 -1
  78. package/dist/esm/modules/network/utilities/url-safety/common/constants.d.ts +0 -2
  79. package/dist/esm/modules/network/utilities/url-safety/common/constants.js +0 -4
  80. package/dist/esm/modules/network/utilities/url-safety/common/constants.js.map +1 -1
  81. package/dist/esm/modules/network/utilities/url-safety/ingest.js.map +1 -1
  82. package/dist/esm/modules/queue/bullmq/helper.d.ts +23 -10
  83. package/dist/esm/modules/queue/bullmq/helper.js +57 -10
  84. package/dist/esm/modules/queue/bullmq/helper.js.map +1 -1
  85. package/dist/esm/modules/queue/common/constants.d.ts +6 -2
  86. package/dist/esm/modules/queue/common/constants.js +6 -4
  87. package/dist/esm/modules/queue/common/constants.js.map +1 -1
  88. package/dist/esm/modules/queue/internal/sequential/common/types.d.ts +2 -4
  89. package/dist/esm/modules/queue/internal/sequential/common/types.js +0 -4
  90. package/dist/esm/modules/queue/internal/sequential/common/types.js.map +1 -1
  91. package/dist/esm/modules/queue/kafka/base.d.ts +1 -1
  92. package/dist/esm/modules/queue/kafka/base.js +13 -2
  93. package/dist/esm/modules/queue/kafka/base.js.map +1 -1
  94. package/dist/esm/modules/queue/kafka/common/constants.d.ts +0 -4
  95. package/dist/esm/modules/queue/kafka/common/constants.js +0 -8
  96. package/dist/esm/modules/queue/kafka/common/constants.js.map +1 -1
  97. package/dist/esm/modules/redis/base/abstract.helper.d.ts +13 -2
  98. package/dist/esm/modules/redis/base/abstract.helper.js +53 -45
  99. package/dist/esm/modules/redis/base/abstract.helper.js.map +1 -1
  100. package/dist/esm/modules/redis/cluster/cluster.helper.d.ts +1 -2
  101. package/dist/esm/modules/redis/cluster/cluster.helper.js +21 -9
  102. package/dist/esm/modules/redis/cluster/cluster.helper.js.map +1 -1
  103. package/dist/esm/modules/redis/cluster/index.d.ts +1 -0
  104. package/dist/esm/modules/redis/cluster/index.js +1 -0
  105. package/dist/esm/modules/redis/cluster/index.js.map +1 -1
  106. package/dist/esm/modules/redis/cluster/types.d.ts +20 -0
  107. package/dist/esm/modules/redis/cluster/types.js +2 -0
  108. package/dist/esm/modules/redis/cluster/types.js.map +1 -0
  109. package/dist/esm/modules/redis/common/constants.d.ts +15 -19
  110. package/dist/esm/modules/redis/common/constants.js +16 -18
  111. package/dist/esm/modules/redis/common/constants.js.map +1 -1
  112. package/dist/esm/modules/redis/common/types.d.ts +10 -60
  113. package/dist/esm/modules/redis/common/types.js.map +1 -1
  114. package/dist/esm/modules/redis/factory.d.ts +7 -25
  115. package/dist/esm/modules/redis/factory.js +9 -18
  116. package/dist/esm/modules/redis/factory.js.map +1 -1
  117. package/dist/esm/modules/redis/sentinel/constants.d.ts +14 -0
  118. package/dist/esm/modules/redis/sentinel/constants.js +11 -0
  119. package/dist/esm/modules/redis/sentinel/constants.js.map +1 -0
  120. package/dist/esm/modules/redis/sentinel/index.d.ts +2 -0
  121. package/dist/esm/modules/redis/sentinel/index.js +2 -0
  122. package/dist/esm/modules/redis/sentinel/index.js.map +1 -1
  123. package/dist/esm/modules/redis/sentinel/sentinel.helper.d.ts +1 -2
  124. package/dist/esm/modules/redis/sentinel/sentinel.helper.js +25 -15
  125. package/dist/esm/modules/redis/sentinel/sentinel.helper.js.map +1 -1
  126. package/dist/esm/modules/redis/sentinel/types.d.ts +26 -0
  127. package/dist/esm/modules/redis/sentinel/types.js +2 -0
  128. package/dist/esm/modules/redis/sentinel/types.js.map +1 -0
  129. package/dist/esm/modules/redis/single/index.d.ts +1 -0
  130. package/dist/esm/modules/redis/single/index.js +1 -0
  131. package/dist/esm/modules/redis/single/index.js.map +1 -1
  132. package/dist/esm/modules/redis/single/single.helper.d.ts +1 -2
  133. package/dist/esm/modules/redis/single/single.helper.js +5 -6
  134. package/dist/esm/modules/redis/single/single.helper.js.map +1 -1
  135. package/dist/esm/modules/redis/single/types.d.ts +15 -0
  136. package/dist/esm/modules/redis/single/types.js +2 -0
  137. package/dist/esm/modules/redis/single/types.js.map +1 -0
  138. package/dist/esm/modules/secrets/base/abstract.helper.js +6 -1
  139. package/dist/esm/modules/secrets/base/abstract.helper.js.map +1 -1
  140. package/dist/esm/modules/secrets/common/types/registration.d.ts +25 -6
  141. package/dist/esm/modules/secrets/common/types/registration.js.map +1 -1
  142. package/dist/esm/modules/secrets/dotenv/dotenv.helper.js +1 -1
  143. package/dist/esm/modules/secrets/dotenv/dotenv.helper.js.map +1 -1
  144. package/dist/esm/modules/secrets/factory.js +7 -6
  145. package/dist/esm/modules/secrets/factory.js.map +1 -1
  146. package/dist/esm/modules/secrets/hashicorp/hashicorp.helper.d.ts +12 -10
  147. package/dist/esm/modules/secrets/hashicorp/hashicorp.helper.js +50 -25
  148. package/dist/esm/modules/secrets/hashicorp/hashicorp.helper.js.map +1 -1
  149. package/dist/esm/modules/socket/socket-io/client/browser-helper.js.map +1 -1
  150. package/dist/esm/modules/socket/socket-io/common/types/client.d.ts +2 -2
  151. package/dist/esm/modules/socket/socket-io/common/types/client.js.map +1 -1
  152. package/dist/esm/modules/socket/socket-io/common/types/server.d.ts +3 -1
  153. package/dist/esm/modules/socket/socket-io/common/types/server.js.map +1 -1
  154. package/dist/esm/modules/socket/socket-io/server/helper.d.ts +2 -2
  155. package/dist/esm/modules/socket/socket-io/server/helper.js.map +1 -1
  156. package/dist/esm/modules/storage/base.d.ts +26 -57
  157. package/dist/esm/modules/storage/base.js +76 -127
  158. package/dist/esm/modules/storage/base.js.map +1 -1
  159. package/dist/esm/modules/storage/bun-s3/helper.d.ts +22 -37
  160. package/dist/esm/modules/storage/bun-s3/helper.js +131 -132
  161. package/dist/esm/modules/storage/bun-s3/helper.js.map +1 -1
  162. package/dist/esm/modules/storage/bun-s3/utility.d.ts +15 -8
  163. package/dist/esm/modules/storage/bun-s3/utility.js +66 -47
  164. package/dist/esm/modules/storage/bun-s3/utility.js.map +1 -1
  165. package/dist/esm/modules/storage/common/constants.d.ts +36 -2
  166. package/dist/esm/modules/storage/common/constants.js +34 -4
  167. package/dist/esm/modules/storage/common/constants.js.map +1 -1
  168. package/dist/esm/modules/storage/common/errors.d.ts +26 -0
  169. package/dist/esm/modules/storage/common/errors.js +23 -0
  170. package/dist/esm/modules/storage/common/errors.js.map +1 -1
  171. package/dist/esm/modules/storage/common/types.d.ts +99 -57
  172. package/dist/esm/modules/storage/common/types.js.map +1 -1
  173. package/dist/esm/modules/storage/common/utility.d.ts +8 -0
  174. package/dist/esm/modules/storage/common/utility.js +14 -2
  175. package/dist/esm/modules/storage/common/utility.js.map +1 -1
  176. package/dist/esm/modules/storage/disk/helper.d.ts +4 -8
  177. package/dist/esm/modules/storage/disk/helper.js +3 -1
  178. package/dist/esm/modules/storage/disk/helper.js.map +1 -1
  179. package/dist/esm/modules/storage/in-memory/helper.d.ts +2 -3
  180. package/dist/esm/modules/storage/in-memory/helper.js.map +1 -1
  181. package/dist/esm/modules/worker-thread/common/types.d.ts +2 -2
  182. package/dist/esm/modules/worker-thread/common/types.js.map +1 -1
  183. package/dist/esm/modules/worker-thread/thread/abstract.d.ts +2 -2
  184. package/dist/esm/modules/worker-thread/thread/abstract.js.map +1 -1
  185. package/dist/esm/modules/worker-thread/worker-pool.d.ts +1 -1
  186. package/dist/esm/modules/worker-thread/worker-pool.js.map +1 -1
  187. package/dist/esm/utilities/module.utility.d.ts +8 -5
  188. package/dist/esm/utilities/module.utility.js +5 -1
  189. package/dist/esm/utilities/module.utility.js.map +1 -1
  190. package/dist/esm/utilities/parse.utility.d.ts +34 -6
  191. package/dist/esm/utilities/parse.utility.js +82 -15
  192. package/dist/esm/utilities/parse.utility.js.map +1 -1
  193. package/dist/esm/utilities/performance.utility.d.ts +3 -3
  194. package/dist/esm/utilities/performance.utility.js.map +1 -1
  195. package/dist/esm/utilities/promise.utility.d.ts +1 -1
  196. package/dist/esm/utilities/promise.utility.js.map +1 -1
  197. package/dist/esm/utilities/request.utility.d.ts +9 -11
  198. package/dist/esm/utilities/request.utility.js.map +1 -1
  199. package/package.json +3 -3
@@ -1,8 +1,9 @@
1
1
  import { BaseHelper } from '../../base.js';
2
2
  import { getError } from '../../error/index.js';
3
+ import { RedisDefaults } from '../../redis/index.js';
3
4
  import { ErrorUtility } from '../../../utilities/error.utility.js';
4
5
  import { ModuleUtility } from '../../../utilities/module.utility.js';
5
- import { HookUtility } from '../common/index.js';
6
+ import { BullMQDefaults, BullQueueRoles, HookUtility } from '../common/index.js';
6
7
  /**
7
8
  * Wraps a BullMQ `Queue` or `Worker` (per `role`) on one Redis connection.
8
9
  * Worker concurrency is `numberOfWorker`; `lockDuration` is in milliseconds.
@@ -10,9 +11,10 @@ import { HookUtility } from '../common/index.js';
10
11
  export class BullMQHelper extends BaseHelper {
11
12
  constructor(options) {
12
13
  super({ scope: BullMQHelper.name, identifier: options.identifier });
13
- this.numberOfWorker = 1;
14
- this.lockDuration = 90 * 60 * 1000;
15
- const { queueName, redisConnection, role, numberOfWorker = 1, lockDuration = 90 * 60 * 1000, queueOptions, workerOptions, onWorkerData, onWorkerDataCompleted, onWorkerDataFail, module, } = options;
14
+ this.isClosing = false;
15
+ /** Every duplicate this helper handed to BullMQ: BullMQ never quits a client it is given, so `close()` does. */
16
+ this.duplicates = [];
17
+ const { queueName, redisConnection, role, numberOfWorker = BullMQDefaults.NUMBER_OF_WORKER, lockDuration = BullMQDefaults.LOCK_DURATION_MS, queueOptions, workerOptions, onWorkerData, onWorkerDataCompleted, onWorkerDataFail, module, } = options;
16
18
  this.bullmqModule = module;
17
19
  this.queueName = queueName;
18
20
  this.role = role;
@@ -26,6 +28,12 @@ export class BullMQHelper extends BaseHelper {
26
28
  this.onWorkerDataFail = onWorkerDataFail;
27
29
  this.configure();
28
30
  }
31
+ /** BullMQ never reconnects a client: once ioredis stops reconnecting (`maxRetry` attempts used up) it is `end` for good, no job moves, and nothing else says so. */
32
+ reportClientEnded() {
33
+ this.logger
34
+ .for(this.reportClientEnded.name)
35
+ .error('The Redis client ended, so BullMQ can no longer move jobs on queue %s | ioredis stopped reconnecting after maxRetry attempts | Back BullMQ with a helper whose maxRetry is %d (forever) | ID: %s', this.queueName, RedisDefaults.RETRY_FOREVER, this.identifier);
36
+ }
29
37
  static newInstance(opts) {
30
38
  return new BullMQHelper(opts);
31
39
  }
@@ -70,9 +78,24 @@ export class BullMQHelper extends BaseHelper {
70
78
  removeOnFail: true,
71
79
  },
72
80
  ...this.queueOptions,
73
- connection: this.redisConnection.duplicateClient(),
81
+ connection: this.createConnection(),
74
82
  };
75
83
  }
84
+ /**
85
+ * The connection BullMQ owns: a duplicate of the helper's client, so the queue or worker connects it by itself
86
+ * whatever state the shared client is in. BullMQ treats a client it is handed as shared and never quits it, so the
87
+ * helper keeps the duplicate and quits it in `close()`.
88
+ */
89
+ createConnection() {
90
+ const connection = this.redisConnection.duplicateClient();
91
+ this.duplicates.push(connection);
92
+ connection.once('end', () => {
93
+ if (!this.isClosing) {
94
+ this.reportClientEnded();
95
+ }
96
+ });
97
+ return connection;
98
+ }
76
99
  /** Full `Worker` options: `this.workerOptions`, then the connection/concurrency/lock fields the helper owns - in that precedence order. */
77
100
  workerOptionsFor(opts) {
78
101
  this.logger
@@ -80,7 +103,7 @@ export class BullMQHelper extends BaseHelper {
80
103
  .debug('Building worker options | Queue: %s | ID: %s', opts.queueName, this.identifier);
81
104
  return {
82
105
  ...this.workerOptions,
83
- connection: this.redisConnection.duplicateClient(),
106
+ connection: this.createConnection(),
84
107
  concurrency: this.numberOfWorker,
85
108
  lockDuration: this.lockDuration,
86
109
  };
@@ -111,8 +134,7 @@ export class BullMQHelper extends BaseHelper {
111
134
  queueName,
112
135
  processor: async (job) => {
113
136
  if (this.onWorkerData) {
114
- const result = await this.onWorkerData(job);
115
- return result;
137
+ return this.onWorkerData(job);
116
138
  }
117
139
  const { id, name, data } = job;
118
140
  this.logger
@@ -150,17 +172,31 @@ export class BullMQHelper extends BaseHelper {
150
172
  return;
151
173
  }
152
174
  switch (this.role) {
153
- case 'queue': {
175
+ case BullQueueRoles.QUEUE: {
154
176
  this.configureQueue();
155
177
  break;
156
178
  }
157
- case 'worker': {
179
+ case BullQueueRoles.WORKER: {
158
180
  this.configureWorker();
159
181
  break;
160
182
  }
161
183
  }
162
184
  }
185
+ /** `quit()` waits for the replies in flight; a client BullMQ already closed is left alone. */
186
+ static async release(opts) {
187
+ const { connection } = opts;
188
+ if (connection.status === 'end') {
189
+ return;
190
+ }
191
+ try {
192
+ await connection.quit();
193
+ }
194
+ catch {
195
+ connection.disconnect();
196
+ }
197
+ }
163
198
  async close() {
199
+ this.isClosing = true;
164
200
  const failures = [];
165
201
  // Both connections must be released even when the first close fails - otherwise a failing worker close silently leaks the queue's Redis connection forever.
166
202
  try {
@@ -181,6 +217,17 @@ export class BullMQHelper extends BaseHelper {
181
217
  .error('Error closing BullMQ queue: %s | ID: %s', error, this.identifier);
182
218
  failures.push(ErrorUtility.toError({ error }).message);
183
219
  }
220
+ for (const connection of this.duplicates.splice(0)) {
221
+ try {
222
+ await BullMQHelper.release({ connection });
223
+ }
224
+ catch (error) {
225
+ this.logger
226
+ .for(this.close.name)
227
+ .error('Error closing the BullMQ connection: %s | ID: %s', error, this.identifier);
228
+ failures.push(ErrorUtility.toError({ error }).message);
229
+ }
230
+ }
184
231
  if (failures.length) {
185
232
  throw getError({
186
233
  message: `[close][${this.identifier}] Failed to close BullMQ helper | Errors: ${failures.join(' | ')}`,
@@ -1 +1 @@
1
- {"version":3,"file":"helper.js","sourceRoot":"","sources":["../../../../../src/modules/queue/bullmq/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,WAAW,EAAuB,MAAM,WAAW,CAAC;AAoC7D;;;GAGG;AACH,MAAM,OAAO,YAAsD,SAAQ,UAAU;IA2BnF,YAAY,OAAoD;QAC9D,KAAK,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;QApB5D,mBAAc,GAAG,CAAC,CAAC;QACnB,iBAAY,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;QAoBtC,MAAM,EACJ,SAAS,EACT,eAAe,EACf,IAAI,EACJ,cAAc,GAAG,CAAC,EAClB,YAAY,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,EAC7B,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,MAAM,GACP,GAAG,OAAO,CAAC;QAEZ,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC;QAE3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QAEvC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,YAAY,G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pB,MAAM,QAAQ,CAAC;gBACb,OAAO,EAAE,WAAW,IAAI,CAAC,UAAU,6CAA6C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;aACvG,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,MAAM;aACR,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;aACpB,IAAI,CAAC,4CAA4C,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACzE,CAAC;CACF","sourcesContent":["import { BaseHelper } from '@/modules/base';\nimport { getError } from '@/modules/error';\nimport { type IRedisHelper } from '@/modules/redis';\nimport { ErrorUtility } from '@/utilities/error.utility';\nimport { ModuleUtility } from '@/utilities/module.utility';\nimport type { Job, Processor, Queue, QueueOptions, Worker, WorkerOptions } from 'bullmq';\nimport { HookUtility, type TBullQueueRole } from '../common';\n\n/** Options for {@link BullMQHelper}. `role` decides whether a `Queue` or a `Worker` is built. */\nexport interface IBullMQOptions<TQueueElement = any, TQueueResult = any> {\n queueName: string;\n identifier: string;\n role: TBullQueueRole;\n redisConnection: IRedisHelper;\n\n /** `bullmq` itself, for a `bun build --compile` binary that has no `node_modules` to load it from. Omitted, it is loaded when a queue or worker is built. */\n module?: typeof import('bullmq');\n\n /**\n * Worker concurrency.\n * @default 1\n */\n numberOfWorker?: number;\n /**\n * How long a worker holds a job's lock, in milliseconds.\n * @default 90 * 60 * 1000\n */\n lockDuration?: number;\n\n /** Merged over the framework defaults, under the fields the helper owns (`connection`) - see `queueOptionsFor`. */\n queueOptions?: Omit<QueueOptions, 'connection'>;\n /** Merged under the fields the helper owns (`connection`, `concurrency`, `lockDuration`) - see `workerOptionsFor`. */\n workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<any>;\n onWorkerDataCompleted?: (job: Job<TQueueElement, TQueueResult>, result: any) => Promise<void>;\n onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n}\n\n/**\n * Wraps a BullMQ `Queue` or `Worker` (per `role`) on one Redis connection.\n * Worker concurrency is `numberOfWorker`; `lockDuration` is in milliseconds.\n */\nexport class BullMQHelper<TQueueElement = any, TQueueResult = any> extends BaseHelper {\n protected queueName: string;\n protected role: TBullQueueRole;\n protected redisConnection: IRedisHelper;\n\n queue: Queue<TQueueElement, TQueueResult>;\n worker: Worker<TQueueElement, TQueueResult>;\n\n protected numberOfWorker = 1;\n protected lockDuration = 90 * 60 * 1000;\n\n protected queueOptions?: Omit<QueueOptions, 'connection'>;\n protected workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n protected onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<any>;\n protected onWorkerDataCompleted?: (\n job: Job<TQueueElement, TQueueResult>,\n result: any,\n ) => Promise<void>;\n\n protected onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n\n private readonly bullmqModule?: typeof import('bullmq');\n\n constructor(options: IBullMQOptions<TQueueElement, TQueueResult>) {\n super({ scope: BullMQHelper.name, identifier: options.identifier });\n const {\n queueName,\n redisConnection,\n role,\n numberOfWorker = 1,\n lockDuration = 90 * 60 * 1000,\n queueOptions,\n workerOptions,\n onWorkerData,\n onWorkerDataCompleted,\n onWorkerDataFail,\n module,\n } = options;\n\n this.bullmqModule = module;\n\n this.queueName = queueName;\n this.role = role;\n this.redisConnection = redisConnection;\n\n this.numberOfWorker = numberOfWorker;\n this.lockDuration = lockDuration;\n\n this.queueOptions = queueOptions;\n this.workerOptions = workerOptions;\n\n this.onWorkerData = onWorkerData;\n this.onWorkerDataCompleted = onWorkerDataCompleted;\n this.onWorkerDataFail = onWorkerDataFail;\n\n this.configure();\n }\n\n static newInstance<T = any, R = any>(opts: IBullMQOptions<T, R>) {\n return new BullMQHelper<T, R>(opts);\n }\n\n /** Structural, never `instanceof`: ioredis is an optional peer, and a second copy fails `instanceof` anyway. */\n protected isClusterClient(): boolean {\n const client = Object(this.redisConnection.getClient());\n\n if (Reflect.get(client, 'isCluster') === true) {\n return true;\n }\n\n return typeof Reflect.get(client, 'nodes') === 'function';\n }\n\n protected resolveQueueName(): string {\n if (this.isClusterClient() && !this.queueName.startsWith('{')) {\n return `{${this.queueName}}`;\n }\n\n return this.queueName;\n }\n\n configureQueue() {\n if (!this.queueName) {\n this.logger\n .for(this.configureQueue.name)\n .error('Invalid queue name | ID: %s', this.identifier);\n\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.queue = this.buildQueue({ queueName });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.queue.on('error', (error) => {\n this.logger\n .for('queue-error')\n .error(\n 'queue: %s | Queue error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n /** Full `Queue` options: framework defaults, then `this.queueOptions`, then the connection the helper owns - in that precedence order. */\n protected queueOptionsFor(opts: { queueName: string }): QueueOptions {\n this.logger\n .for(this.queueOptionsFor.name)\n .debug('Building queue options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n defaultJobOptions: {\n removeOnComplete: true,\n removeOnFail: true,\n },\n ...this.queueOptions,\n connection: this.redisConnection.duplicateClient(),\n };\n }\n\n /** Full `Worker` options: `this.workerOptions`, then the connection/concurrency/lock fields the helper owns - in that precedence order. */\n protected workerOptionsFor(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): WorkerOptions {\n this.logger\n .for(this.workerOptionsFor.name)\n .debug('Building worker options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n ...this.workerOptions,\n connection: this.redisConnection.duplicateClient(),\n concurrency: this.numberOfWorker,\n lockDuration: this.lockDuration,\n };\n }\n\n /** `bullmq` is an optional peer: loaded when a queue or worker is built, not when this module is. */\n private loadBullMQ(): typeof import('bullmq') {\n return (\n this.bullmqModule ?? ModuleUtility.loadSync<typeof import('bullmq')>({ module: 'bullmq' })\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildQueue(opts: { queueName: string }): Queue<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n\n return new bullmq.Queue<TQueueElement, TQueueResult>(\n opts.queueName,\n this.queueOptionsFor(opts),\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildWorker(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): Worker<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n\n return new bullmq.Worker<TQueueElement, TQueueResult>(\n opts.queueName,\n opts.processor,\n this.workerOptionsFor(opts),\n );\n }\n\n configureWorker() {\n if (!this.queueName) {\n this.logger\n .for(this.configureWorker.name)\n .error('Invalid worker name | ID: %s', this.identifier);\n\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.worker = this.buildWorker({\n queueName,\n processor: async (job) => {\n if (this.onWorkerData) {\n const result = await this.onWorkerData(job);\n\n return result;\n }\n\n const { id, name, data } = job;\n this.logger\n .for('onWorkerData')\n .info(\n 'queue: %s | id: %s | name: %s | data: %j | ID: %s',\n this.queueName,\n id,\n name,\n data,\n this.identifier,\n );\n\n return undefined as TQueueResult;\n },\n });\n\n this.worker.on('completed', (job, result) => {\n HookUtility.invoke({\n logger: this.logger,\n scope: 'worker-completed',\n execution: () => this.onWorkerDataCompleted?.(job, result),\n });\n });\n\n this.worker.on('failed', (job, reason) => {\n HookUtility.invoke({\n logger: this.logger,\n scope: 'worker-failed',\n execution: () => this.onWorkerDataFail?.(job, reason),\n });\n });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.worker.on('error', (error) => {\n this.logger\n .for('worker-error')\n .error(\n 'queue: %s | Worker error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n configure() {\n if (!this.role) {\n this.logger\n .for(this.configure.name)\n .error(\n 'Invalid client role to configure | Valid roles: [queue|worker] | ID: %s',\n this.identifier,\n );\n\n return;\n }\n\n switch (this.role) {\n case 'queue': {\n this.configureQueue();\n break;\n }\n case 'worker': {\n this.configureWorker();\n break;\n }\n }\n }\n\n async close() {\n const failures: string[] = [];\n\n // Both connections must be released even when the first close fails - otherwise a failing worker close silently leaks the queue's Redis connection forever.\n try {\n await this.worker?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ worker: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n\n try {\n await this.queue?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ queue: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n\n if (failures.length) {\n throw getError({\n message: `[close][${this.identifier}] Failed to close BullMQ helper | Errors: ${failures.join(' | ')}`,\n });\n }\n\n this.logger\n .for(this.close.name)\n .info('BullMQ helper closed successfully | ID: %s', this.identifier);\n }\n}\n"]}
1
+ {"version":3,"file":"helper.js","sourceRoot":"","sources":["../../../../../src/modules/queue/bullmq/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAqB,aAAa,EAAqB,MAAM,iBAAiB,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAuB,MAAM,WAAW,CAAC;AAuC7F;;;GAGG;AACH,MAAM,OAAO,YAA8D,SAAQ,UAAU;IA8B3F,YAAY,OAAoD;QAC9D,KAAK,CAAC,EAAE,KAAK,EAAE,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,C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AAK,CAAC,IAAI,CAAC;iBACpB,KAAK,CAAC,yCAAyC,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5E,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;QACzD,CAAC;QAED,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;YACnD,IAAI,CAAC;gBACH,MAAM,YAAY,CAAC,OAAO,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC;YAC7C,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,MAAM;qBACR,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;qBACpB,KAAK,CAAC,kDAAkD,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;gBACrF,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;YACzD,CAAC;QACH,CAAC;QAED,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;YACpB,MAAM,QAAQ,CAAC;gBACb,OAAO,EAAE,WAAW,IAAI,CAAC,UAAU,6CAA6C,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;aACvG,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,MAAM;aACR,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;aACpB,IAAI,CAAC,4CAA4C,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACzE,CAAC;CACF","sourcesContent":["import { BaseHelper } from '@/modules/base';\nimport { getError } from '@/modules/error';\nimport { type IRedisHelper, RedisDefaults, type TRedisClient } from '@/modules/redis';\nimport { ErrorUtility } from '@/utilities/error.utility';\nimport { ModuleUtility } from '@/utilities/module.utility';\nimport type { Job, Processor, Queue, QueueOptions, Worker, WorkerOptions } from 'bullmq';\nimport { BullMQDefaults, BullQueueRoles, HookUtility, type TBullQueueRole } from '../common';\n\n/** Options for {@link BullMQHelper}. `role` decides whether a `Queue` or a `Worker` is built. */\nexport interface IBullMQOptions<TQueueElement = unknown, TQueueResult = unknown> {\n queueName: string;\n identifier: string;\n role: TBullQueueRole;\n redisConnection: IRedisHelper;\n\n /** `bullmq` itself, for a `bun build --compile` binary that has no `node_modules` to load it from. Omitted, it is loaded when a queue or worker is built. */\n module?: typeof import('bullmq');\n\n /**\n * Worker concurrency.\n * @default BullMQDefaults.NUMBER_OF_WORKER\n */\n numberOfWorker?: number;\n /**\n * How long a worker holds a job's lock, in milliseconds.\n * @default BullMQDefaults.LOCK_DURATION_MS\n */\n lockDuration?: number;\n\n /** Merged over the framework defaults, under the fields the helper owns (`connection`) - see `queueOptionsFor`. */\n queueOptions?: Omit<QueueOptions, 'connection'>;\n /** Merged under the fields the helper owns (`connection`, `concurrency`, `lockDuration`) - see `workerOptionsFor`. */\n workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<TQueueResult>;\n onWorkerDataCompleted?: (\n job: Job<TQueueElement, TQueueResult>,\n result: TQueueResult,\n ) => Promise<void>;\n onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n}\n\n/**\n * Wraps a BullMQ `Queue` or `Worker` (per `role`) on one Redis connection.\n * Worker concurrency is `numberOfWorker`; `lockDuration` is in milliseconds.\n */\nexport class BullMQHelper<TQueueElement = unknown, TQueueResult = unknown> extends BaseHelper {\n protected queueName: string;\n protected role: TBullQueueRole;\n protected redisConnection: IRedisHelper;\n\n queue: Queue<TQueueElement, TQueueResult>;\n worker: Worker<TQueueElement, TQueueResult>;\n\n protected numberOfWorker: number;\n protected lockDuration: number;\n\n protected queueOptions?: Omit<QueueOptions, 'connection'>;\n protected workerOptions?: Omit<WorkerOptions, 'connection' | 'concurrency' | 'lockDuration'>;\n\n protected onWorkerData?: (job: Job<TQueueElement, TQueueResult>) => Promise<TQueueResult>;\n protected onWorkerDataCompleted?: (\n job: Job<TQueueElement, TQueueResult>,\n result: TQueueResult,\n ) => Promise<void>;\n\n protected onWorkerDataFail?: (\n job: Job<TQueueElement, TQueueResult> | undefined,\n error: Error,\n ) => Promise<void>;\n\n private readonly bullmqModule?: typeof import('bullmq');\n private isClosing = false;\n /** Every duplicate this helper handed to BullMQ: BullMQ never quits a client it is given, so `close()` does. */\n private readonly duplicates: TRedisClient[] = [];\n\n constructor(options: IBullMQOptions<TQueueElement, TQueueResult>) {\n super({ scope: BullMQHelper.name, identifier: options.identifier });\n const {\n queueName,\n redisConnection,\n role,\n numberOfWorker = BullMQDefaults.NUMBER_OF_WORKER,\n lockDuration = BullMQDefaults.LOCK_DURATION_MS,\n queueOptions,\n workerOptions,\n onWorkerData,\n onWorkerDataCompleted,\n onWorkerDataFail,\n module,\n } = options;\n\n this.bullmqModule = module;\n\n this.queueName = queueName;\n this.role = role;\n this.redisConnection = redisConnection;\n\n this.numberOfWorker = numberOfWorker;\n this.lockDuration = lockDuration;\n\n this.queueOptions = queueOptions;\n this.workerOptions = workerOptions;\n\n this.onWorkerData = onWorkerData;\n this.onWorkerDataCompleted = onWorkerDataCompleted;\n this.onWorkerDataFail = onWorkerDataFail;\n\n this.configure();\n }\n\n /** BullMQ never reconnects a client: once ioredis stops reconnecting (`maxRetry` attempts used up) it is `end` for good, no job moves, and nothing else says so. */\n protected reportClientEnded() {\n this.logger\n .for(this.reportClientEnded.name)\n .error(\n 'The Redis client ended, so BullMQ can no longer move jobs on queue %s | ioredis stopped reconnecting after maxRetry attempts | Back BullMQ with a helper whose maxRetry is %d (forever) | ID: %s',\n this.queueName,\n RedisDefaults.RETRY_FOREVER,\n this.identifier,\n );\n }\n\n static newInstance<T = unknown, R = unknown>(opts: IBullMQOptions<T, R>) {\n return new BullMQHelper<T, R>(opts);\n }\n\n /** Structural, never `instanceof`: ioredis is an optional peer, and a second copy fails `instanceof` anyway. */\n protected isClusterClient(): boolean {\n const client = Object(this.redisConnection.getClient());\n\n if (Reflect.get(client, 'isCluster') === true) {\n return true;\n }\n\n return typeof Reflect.get(client, 'nodes') === 'function';\n }\n\n protected resolveQueueName(): string {\n if (this.isClusterClient() && !this.queueName.startsWith('{')) {\n return `{${this.queueName}}`;\n }\n\n return this.queueName;\n }\n\n configureQueue() {\n if (!this.queueName) {\n this.logger\n .for(this.configureQueue.name)\n .error('Invalid queue name | ID: %s', this.identifier);\n\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.queue = this.buildQueue({ queueName });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.queue.on('error', (error) => {\n this.logger\n .for('queue-error')\n .error(\n 'queue: %s | Queue error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n /** Full `Queue` options: framework defaults, then `this.queueOptions`, then the connection the helper owns - in that precedence order. */\n protected queueOptionsFor(opts: { queueName: string }): QueueOptions {\n this.logger\n .for(this.queueOptionsFor.name)\n .debug('Building queue options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n defaultJobOptions: {\n removeOnComplete: true,\n removeOnFail: true,\n },\n ...this.queueOptions,\n connection: this.createConnection(),\n };\n }\n\n /**\n * The connection BullMQ owns: a duplicate of the helper's client, so the queue or worker connects it by itself\n * whatever state the shared client is in. BullMQ treats a client it is handed as shared and never quits it, so the\n * helper keeps the duplicate and quits it in `close()`.\n */\n protected createConnection(): TRedisClient {\n const connection = this.redisConnection.duplicateClient();\n this.duplicates.push(connection);\n connection.once('end', () => {\n if (!this.isClosing) {\n this.reportClientEnded();\n }\n });\n\n return connection;\n }\n\n /** Full `Worker` options: `this.workerOptions`, then the connection/concurrency/lock fields the helper owns - in that precedence order. */\n protected workerOptionsFor(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): WorkerOptions {\n this.logger\n .for(this.workerOptionsFor.name)\n .debug('Building worker options | Queue: %s | ID: %s', opts.queueName, this.identifier);\n\n return {\n ...this.workerOptions,\n connection: this.createConnection(),\n concurrency: this.numberOfWorker,\n lockDuration: this.lockDuration,\n };\n }\n\n /** `bullmq` is an optional peer: loaded when a queue or worker is built, not when this module is. */\n private loadBullMQ(): typeof import('bullmq') {\n return (\n this.bullmqModule ?? ModuleUtility.loadSync<typeof import('bullmq')>({ module: 'bullmq' })\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildQueue(opts: { queueName: string }): Queue<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n\n return new bullmq.Queue<TQueueElement, TQueueResult>(\n opts.queueName,\n this.queueOptionsFor(opts),\n );\n }\n\n /** Client factory seam - overridden in tests to run the helper without Redis. */\n protected buildWorker(opts: {\n queueName: string;\n processor: Processor<TQueueElement, TQueueResult>;\n }): Worker<TQueueElement, TQueueResult> {\n const bullmq = this.loadBullMQ();\n\n return new bullmq.Worker<TQueueElement, TQueueResult>(\n opts.queueName,\n opts.processor,\n this.workerOptionsFor(opts),\n );\n }\n\n configureWorker() {\n if (!this.queueName) {\n this.logger\n .for(this.configureWorker.name)\n .error('Invalid worker name | ID: %s', this.identifier);\n\n return;\n }\n\n const queueName = this.resolveQueueName();\n this.worker = this.buildWorker({\n queueName,\n processor: async (job) => {\n if (this.onWorkerData) {\n return this.onWorkerData(job);\n }\n\n const { id, name, data } = job;\n this.logger\n .for('onWorkerData')\n .info(\n 'queue: %s | id: %s | name: %s | data: %j | ID: %s',\n this.queueName,\n id,\n name,\n data,\n this.identifier,\n );\n\n return undefined as TQueueResult;\n },\n });\n\n this.worker.on('completed', (job, result) => {\n HookUtility.invoke({\n logger: this.logger,\n scope: 'worker-completed',\n execution: () => this.onWorkerDataCompleted?.(job, result),\n });\n });\n\n this.worker.on('failed', (job, reason) => {\n HookUtility.invoke({\n logger: this.logger,\n scope: 'worker-failed',\n execution: () => this.onWorkerDataFail?.(job, reason),\n });\n });\n\n // An 'error' event without a listener is re-thrown by EventEmitter - a Redis hiccup would take the whole process down.\n this.worker.on('error', (error) => {\n this.logger\n .for('worker-error')\n .error(\n 'queue: %s | Worker error! Error: %s | ID: %s',\n this.queueName,\n error,\n this.identifier,\n );\n });\n }\n\n configure() {\n if (!this.role) {\n this.logger\n .for(this.configure.name)\n .error(\n 'Invalid client role to configure | Valid roles: [queue|worker] | ID: %s',\n this.identifier,\n );\n\n return;\n }\n\n switch (this.role) {\n case BullQueueRoles.QUEUE: {\n this.configureQueue();\n break;\n }\n case BullQueueRoles.WORKER: {\n this.configureWorker();\n break;\n }\n }\n }\n\n /** `quit()` waits for the replies in flight; a client BullMQ already closed is left alone. */\n private static async release(opts: { connection: TRedisClient }) {\n const { connection } = opts;\n\n if (connection.status === 'end') {\n return;\n }\n\n try {\n await connection.quit();\n } catch {\n connection.disconnect();\n }\n }\n\n async close() {\n this.isClosing = true;\n const failures: string[] = [];\n\n // Both connections must be released even when the first close fails - otherwise a failing worker close silently leaks the queue's Redis connection forever.\n try {\n await this.worker?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ worker: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n\n try {\n await this.queue?.close();\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing BullMQ queue: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n\n for (const connection of this.duplicates.splice(0)) {\n try {\n await BullMQHelper.release({ connection });\n } catch (error) {\n this.logger\n .for(this.close.name)\n .error('Error closing the BullMQ connection: %s | ID: %s', error, this.identifier);\n failures.push(ErrorUtility.toError({ error }).message);\n }\n }\n\n if (failures.length) {\n throw getError({\n message: `[close][${this.identifier}] Failed to close BullMQ helper | Errors: ${failures.join(' | ')}`,\n });\n }\n\n this.logger\n .for(this.close.name)\n .info('BullMQ helper closed successfully | ID: %s', this.identifier);\n }\n}\n"]}
@@ -3,8 +3,12 @@ import type { TConstValue } from '../../../common/types/index.js';
3
3
  export declare class BullQueueRoles {
4
4
  static readonly QUEUE = "queue";
5
5
  static readonly WORKER = "worker";
6
- static readonly SCHEME_SET: Set<string>;
7
- static isValid(value: string): boolean;
6
+ }
7
+ /** What a BullMQ helper uses when its options leave a field out. */
8
+ export declare class BullMQDefaults {
9
+ static readonly NUMBER_OF_WORKER = 1;
10
+ /** How long a worker holds a job's lock: 90 minutes, in milliseconds. */
11
+ static readonly LOCK_DURATION_MS: number;
8
12
  }
9
13
  /** Valid BullMQ queue role values. */
10
14
  export type TBullQueueRole = TConstValue<typeof BullQueueRoles>;
@@ -2,9 +2,11 @@
2
2
  export class BullQueueRoles {
3
3
  static { this.QUEUE = 'queue'; }
4
4
  static { this.WORKER = 'worker'; }
5
- static { this.SCHEME_SET = new Set([this.QUEUE, this.WORKER]); }
6
- static isValid(value) {
7
- return this.SCHEME_SET.has(value);
8
- }
5
+ }
6
+ /** What a BullMQ helper uses when its options leave a field out. */
7
+ export class BullMQDefaults {
8
+ static { this.NUMBER_OF_WORKER = 1; }
9
+ /** How long a worker holds a job's lock: 90 minutes, in milliseconds. */
10
+ static { this.LOCK_DURATION_MS = 90 * 60 * 1000; }
9
11
  }
10
12
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../src/modules/queue/common/constants.ts"],"names":[],"mappings":"AAEA,+FAA+F;AAC/F,MAAM,OAAO,cAAc;aACT,UAAK,GAAG,OAAO,CAAC;aAChB,WAAM,GAAG,QAAQ,CAAC;aAElB,eAAU,GAAG,IAAI,GAAG,CAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAExE,MAAM,CAAC,OAAO,CAAC,KAAa;QAC1B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC","sourcesContent":["import type { TConstValue } from '@/common/types';\n\n/** What a BullMQ helper is: the QUEUE that jobs are added to, or the WORKER that runs them. */\nexport class BullQueueRoles {\n static readonly QUEUE = 'queue';\n static readonly WORKER = 'worker';\n\n static readonly SCHEME_SET = new Set<string>([this.QUEUE, this.WORKER]);\n\n static isValid(value: string): boolean {\n return this.SCHEME_SET.has(value);\n }\n}\n\n/** Valid BullMQ queue role values. */\nexport type TBullQueueRole = TConstValue<typeof BullQueueRoles>;\n"]}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../src/modules/queue/common/constants.ts"],"names":[],"mappings":"AAEA,+FAA+F;AAC/F,MAAM,OAAO,cAAc;aACT,UAAK,GAAG,OAAO,CAAC;aAChB,WAAM,GAAG,QAAQ,CAAC;;AAGpC,oEAAoE;AACpE,MAAM,OAAO,cAAc;aACT,qBAAgB,GAAG,CAAC,CAAC;IAErC,yEAAyE;aACzD,qBAAgB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC","sourcesContent":["import type { TConstValue } from '@/common/types';\n\n/** What a BullMQ helper is: the QUEUE that jobs are added to, or the WORKER that runs them. */\nexport class BullQueueRoles {\n static readonly QUEUE = 'queue';\n static readonly WORKER = 'worker';\n}\n\n/** What a BullMQ helper uses when its options leave a field out. */\nexport class BullMQDefaults {\n static readonly NUMBER_OF_WORKER = 1;\n\n /** How long a worker holds a job's lock: 90 minutes, in milliseconds. */\n static readonly LOCK_DURATION_MS = 90 * 60 * 1000;\n}\n\n/** Valid BullMQ queue role values. */\nexport type TBullQueueRole = TConstValue<typeof BullQueueRoles>;\n"]}
@@ -1,15 +1,13 @@
1
- import { type ValueOf, type ValueOrPromise } from '../../../../../common/types/index.js';
1
+ import { type TConstValue, type ValueOrPromise } from '../../../../../common/types/index.js';
2
2
  /** Lifecycle states of a sequential queue; the numeric prefix orders them. */
3
3
  export declare class QueueStatuses {
4
4
  static readonly WAITING = "000_WAITING";
5
5
  static readonly PROCESSING = "100_PROCESSING";
6
6
  static readonly LOCKED = "200_LOCKED";
7
7
  static readonly SETTLED = "300_SETTLED";
8
- static readonly SCHEME_SET: Set<string>;
9
- static isValid(scheme: string): boolean;
10
8
  }
11
9
  /** A {@link QueueStatuses} value. */
12
- export type TQueueStatus = ValueOf<Omit<typeof QueueStatuses, 'isValid' | 'SCHEME_SET'>>;
10
+ export type TQueueStatus = TConstValue<typeof QueueStatuses>;
13
11
  /** One queued item: `payload`, plus `isLocked` while a handler holds it. */
14
12
  export type TQueueElement<T> = {
15
13
  isLocked: boolean;
@@ -4,9 +4,5 @@ export class QueueStatuses {
4
4
  static { this.PROCESSING = '100_PROCESSING'; }
5
5
  static { this.LOCKED = '200_LOCKED'; }
6
6
  static { this.SETTLED = '300_SETTLED'; }
7
- static { this.SCHEME_SET = new Set([this.WAITING, this.PROCESSING, this.LOCKED, this.SETTLED]); }
8
- static isValid(scheme) {
9
- return this.SCHEME_SET.has(scheme);
10
- }
11
7
  }
12
8
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../../src/modules/queue/internal/sequential/common/types.ts"],"names":[],"mappings":"AAEA,8EAA8E;AAC9E,MAAM,OAAO,aAAa;aACR,YAAO,GAAG,aAAa,CAAC;aACxB,eAAU,GAAG,gBAAgB,CAAC;aAC9B,WAAM,GAAG,YAAY,CAAC;aACtB,YAAO,GAAG,aAAa,CAAC;aAExB,eAAU,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;IAEjG,MAAM,CAAC,OAAO,CAAC,MAAc;QAC3B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC","sourcesContent":["import { type ValueOf, type ValueOrPromise } from '@/common/types';\n\n/** Lifecycle states of a sequential queue; the numeric prefix orders them. */\nexport class QueueStatuses {\n static readonly WAITING = '000_WAITING';\n static readonly PROCESSING = '100_PROCESSING';\n static readonly LOCKED = '200_LOCKED';\n static readonly SETTLED = '300_SETTLED';\n\n static readonly SCHEME_SET = new Set([this.WAITING, this.PROCESSING, this.LOCKED, this.SETTLED]);\n\n static isValid(scheme: string): boolean {\n return this.SCHEME_SET.has(scheme);\n }\n}\n\n/** A {@link QueueStatuses} value. */\nexport type TQueueStatus = ValueOf<Omit<typeof QueueStatuses, 'isValid' | 'SCHEME_SET'>>;\n/** One queued item: `payload`, plus `isLocked` while a handler holds it. */\nexport type TQueueElement<T> = { isLocked: boolean; payload: T };\n\n/** Hooks of a sequential queue. */\nexport interface IQueueCallback<TElementPayload> {\n /**\n * Whether {@link SequentialQueueHelper} dispatches an element on its own.\n * @default true\n */\n autoDispatch?: boolean;\n\n onMessage?: (opts: {\n identifier: string;\n queueElement: TQueueElement<TElementPayload>;\n }) => ValueOrPromise<void>;\n onDataEnqueue?: (opts: {\n identifier: string;\n queueElement: TQueueElement<TElementPayload>;\n }) => ValueOrPromise<void>;\n onDataDequeue?: (opts: {\n identifier: string;\n queueElement: TQueueElement<TElementPayload>;\n }) => ValueOrPromise<void>;\n onStateChange?: (opts: {\n identifier: string;\n from: TQueueStatus;\n to: TQueueStatus;\n }) => ValueOrPromise<void>;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../../src/modules/queue/internal/sequential/common/types.ts"],"names":[],"mappings":"AAEA,8EAA8E;AAC9E,MAAM,OAAO,aAAa;aACR,YAAO,GAAG,aAAa,CAAC;aACxB,eAAU,GAAG,gBAAgB,CAAC;aAC9B,WAAM,GAAG,YAAY,CAAC;aACtB,YAAO,GAAG,aAAa,CAAC","sourcesContent":["import { type TConstValue, type ValueOrPromise } from '@/common/types';\n\n/** Lifecycle states of a sequential queue; the numeric prefix orders them. */\nexport class QueueStatuses {\n static readonly WAITING = '000_WAITING';\n static readonly PROCESSING = '100_PROCESSING';\n static readonly LOCKED = '200_LOCKED';\n static readonly SETTLED = '300_SETTLED';\n}\n\n/** A {@link QueueStatuses} value. */\nexport type TQueueStatus = TConstValue<typeof QueueStatuses>;\n/** One queued item: `payload`, plus `isLocked` while a handler holds it. */\nexport type TQueueElement<T> = { isLocked: boolean; payload: T };\n\n/** Hooks of a sequential queue. */\nexport interface IQueueCallback<TElementPayload> {\n /**\n * Whether {@link SequentialQueueHelper} dispatches an element on its own.\n * @default true\n */\n autoDispatch?: boolean;\n\n onMessage?: (opts: {\n identifier: string;\n queueElement: TQueueElement<TElementPayload>;\n }) => ValueOrPromise<void>;\n onDataEnqueue?: (opts: {\n identifier: string;\n queueElement: TQueueElement<TElementPayload>;\n }) => ValueOrPromise<void>;\n onDataDequeue?: (opts: {\n identifier: string;\n queueElement: TQueueElement<TElementPayload>;\n }) => ValueOrPromise<void>;\n onStateChange?: (opts: {\n identifier: string;\n from: TQueueStatus;\n to: TQueueStatus;\n }) => ValueOrPromise<void>;\n}\n"]}
@@ -41,7 +41,7 @@ export declare abstract class BaseKafkaHelper<TClient extends Base<BaseOptions>>
41
41
  protected swapClient(newClient: TClient): void;
42
42
  protected resetHealthState(): void;
43
43
  protected closeClient(): Promise<void>;
44
- /** Force-close through the callback overload: only Producer and Consumer expose `close(force, cb)`, and the `Base` type this class is generic over declares no such overload - hence the cast. */
44
+ /** Force-close through the callback overload: only Producer and Consumer expose `close(force, cb)`, and the `Base` type this class is generic over declares no such overload - hence the guard. */
45
45
  protected closeClientWithCallback(): Promise<void>;
46
46
  protected gracefulCloseClient(): Promise<void>;
47
47
  }
@@ -2,6 +2,12 @@ import { getError } from '../../error/index.js';
2
2
  import { BaseHelper } from '../../base.js';
3
3
  import { HookUtility } from '../common/index.js';
4
4
  import { KafkaClientEvents, KafkaDefaults, KafkaHealthStatuses, } from './common/constants.js';
5
+ const hasCallbackClose = (client) => {
6
+ return (typeof client === 'object' &&
7
+ client !== null &&
8
+ 'close' in client &&
9
+ typeof client.close === 'function');
10
+ };
5
11
  /**
6
12
  * Shared health tracking and broker event wiring for all Kafka helpers (producer, consumer, admin); `TClient` is the platformatic client type, passed by subclasses via `super({ client })`.
7
13
  *
@@ -91,11 +97,16 @@ export class BaseKafkaHelper extends BaseHelper {
91
97
  closeClient() {
92
98
  return this.client.close();
93
99
  }
94
- /** Force-close through the callback overload: only Producer and Consumer expose `close(force, cb)`, and the `Base` type this class is generic over declares no such overload - hence the cast. */
100
+ /** Force-close through the callback overload: only Producer and Consumer expose `close(force, cb)`, and the `Base` type this class is generic over declares no such overload - hence the guard. */
95
101
  closeClientWithCallback() {
96
102
  const client = this.client;
103
+ if (!hasCallbackClose(client)) {
104
+ return Promise.reject(getError({ message: '[closeClientWithCallback] The client has no close(force, callback)' }));
105
+ }
106
+ const closable = client;
97
107
  return new Promise((resolve, reject) => {
98
- client.close(true, (error) => {
108
+ // Through `ICallbackCloseClient`, not `client`: on the narrowed type the overload of `Base.close` wins and takes no `force`.
109
+ closable.close(true, (error) => {
99
110
  if (error) {
100
111
  reject(error);
101
112
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../../src/modules/queue/kafka/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,mBAAmB,GAEpB,MAAM,oBAAoB,CAAC;AAiB5B;;;;GAIG;AACH,MAAM,OAAgB,eAAmD,SAAQ,UAAU;IAYzF,YAAY,IAAgC;QAC1C,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;QATlD,iBAAY,GAAuB,mBAAmB,CAAC,OAAO,CAAC;QA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,kMAAkM;IACxL,uBAAuB;QAC/B,MAAM,MAAM,GAAG,IAAI,CAAC,MAAiB,CAAC;QAEtC,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC3C,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,KAAoB,EAAE,EAAE;gBAC1C,IAAI,KAAK,EAAE,CAAC;oBACV,MAAM,CAAC,KAAK,CAAC,CAAC;oBAEd,OAAO;gBACT,CAAC;gBAED,OAAO,EAAE,CAAC;YACZ,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAES,KAAK,CAAC,mBAAmB;QACjC,IAAI,aAAa,GAA0B,IAAI,CAAC;QAEhD,IAAI,CAAC;YACH,2KAA2K;YAC3K,MAAM,OAAO,CAAC,IAAI,CAAC;gBACjB,IAAI,CAAC,WAAW,EAAE;gBAClB,IAAI,OAAO,CAAO,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE;oBAC9B,aAAa,GAAG,UAAU,CACxB,GAAG,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,OAAO,EAAE,0CAA0C,EAAE,CAAC,CAAC,EAC/E,IAAI,CAAC,eAAe,CACrB,CAAC;gBACJ,CAAC,CAAC;aACH,CAAC,CAAC;QACL,CAAC;gBAAS,CAAC;YACT,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;gBAC3B,YAAY,CAAC,aAAa,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import type { AnyType } from '@/common';\nimport { getError } from '@/modules/error';\nimport { BaseHelper } from '@/modules/base';\nimport type { Base, BaseOptions, ConnectionPoolEventPayload } from '@platformatic/kafka';\nimport { HookUtility } from '../common';\nimport {\n KafkaClientEvents,\n KafkaDefaults,\n KafkaHealthStatuses,\n type TKafkaHealthStatus,\n} from './common/constants';\nimport type { TKafkaBrokerEventCallback } from './common/types';\n\n/** Options shared by the Kafka helpers. */\nexport interface IKafkaBaseOptions<TClient extends Base<BaseOptions>> {\n scope: string;\n identifier: string;\n client: TClient;\n /**\n * Milliseconds the helper waits for in-flight work on shutdown.\n * @default KafkaDefaults.SHUTDOWN_TIMEOUT\n */\n shutdownTimeout?: number;\n onBrokerConnect?: TKafkaBrokerEventCallback;\n onBrokerDisconnect?: TKafkaBrokerEventCallback;\n}\n\n/**\n * Shared health tracking and broker event wiring for all Kafka helpers (producer, consumer, admin); `TClient` is the platformatic client type, passed by subclasses via `super({ client })`.\n *\n * Every member below is `protected`, not `private`, so a producer, consumer, or admin helper can extend this class instead of forking it. That visibility is not a stable contract between minor versions.\n */\nexport abstract class BaseKafkaHelper<TClient extends Base<BaseOptions>> extends BaseHelper {\n protected client: TClient;\n protected readonly shutdownTimeout: number;\n\n protected healthStatus: TKafkaHealthStatus = KafkaHealthStatuses.UNKNOWN;\n\n /** Per-broker connection state. Key = \"host:port\" */\n protected readonly connectedBrokers = new Set<string>();\n\n protected readonly onBrokerConnect?: TKafkaBrokerEventCallback;\n protected readonly onBrokerDisconnect?: TKafkaBrokerEventCallback;\n\n constructor(opts: IKafkaBaseOptions<TClient>) {\n super({ scope: opts.scope, identifier: opts.identifier });\n this.client = opts.client;\n this.shutdownTimeout = opts.shutdownTimeout ?? KafkaDefaults.SHUTDOWN_TIMEOUT;\n this.onBrokerConnect = opts.onBrokerConnect;\n this.onBrokerDisconnect = opts.onBrokerDisconnect;\n }\n\n isHealthy(): boolean {\n return this.connectedBrokers.size > 0;\n }\n\n isReady(): boolean {\n return this.healthStatus === KafkaHealthStatuses.CONNECTED;\n }\n\n getHealthStatus(): TKafkaHealthStatus {\n return this.healthStatus;\n }\n\n getConnectedBrokerCount(): number {\n return this.connectedBrokers.size;\n }\n\n protected configureBrokerConnect() {\n this.client.on(KafkaClientEvents.BROKER_CONNECT, (payload: ConnectionPoolEventPayload) => {\n const brokerKey = `${payload.broker.host}:${payload.broker.port}`;\n this.connectedBrokers.add(brokerKey);\n this.healthStatus = KafkaHealthStatuses.CONNECTED;\n\n this.logger.info(\n '[configureBrokerConnect] Broker CONNECTED | Host: %s | Port: %d | Connected: %d',\n payload.broker.host,\n payload.broker.port,\n this.connectedBrokers.size,\n );\n\n HookUtility.invoke({\n logger: this.logger,\n scope: 'configureBrokerConnect',\n execution: () => this.onBrokerConnect?.({ broker: payload.broker }),\n });\n });\n }\n\n protected configureBrokerDisconnect() {\n this.client.on(KafkaClientEvents.BROKER_DISCONNECT, (payload: ConnectionPoolEventPayload) => {\n const brokerKey = `${payload.broker.host}:${payload.broker.port}`;\n this.connectedBrokers.delete(brokerKey);\n\n if (this.connectedBrokers.size === 0) {\n this.healthStatus = KafkaHealthStatuses.DISCONNECTED;\n }\n\n this.logger.warn(\n '[configureBrokerDisconnect] Broker DISCONNECTED | Host: %s | Port: %d | Remaining: %d',\n payload.broker.host,\n payload.broker.port,\n this.connectedBrokers.size,\n );\n\n HookUtility.invoke({\n logger: this.logger,\n scope: 'configureBrokerDisconnect',\n execution: () => this.onBrokerDisconnect?.({ broker: payload.broker }),\n });\n });\n }\n\n protected configureBrokerFailed() {\n this.client.on(KafkaClientEvents.BROKER_FAILED, (payload: ConnectionPoolEventPayload) => {\n const brokerKey = `${payload.broker.host}:${payload.broker.port}`;\n this.connectedBrokers.delete(brokerKey);\n\n if (this.connectedBrokers.size === 0) {\n this.healthStatus = KafkaHealthStatuses.DISCONNECTED;\n }\n\n this.logger.error(\n '[configureBrokerFailed] Broker connection FAILED | Host: %s | Port: %d | Remaining: %d',\n payload.broker.host,\n payload.broker.port,\n this.connectedBrokers.size,\n );\n });\n }\n\n protected configureBrokerEvents(): void {\n this.configureBrokerConnect();\n this.configureBrokerDisconnect();\n this.configureBrokerFailed();\n }\n\n protected unconfigureBrokerEvents(): void {\n this.client.removeAllListeners(KafkaClientEvents.BROKER_CONNECT);\n this.client.removeAllListeners(KafkaClientEvents.BROKER_DISCONNECT);\n this.client.removeAllListeners(KafkaClientEvents.BROKER_FAILED);\n }\n\n protected swapClient(newClient: TClient): void {\n this.unconfigureBrokerEvents();\n this.client = newClient;\n this.resetHealthState();\n this.configureBrokerEvents();\n }\n\n protected resetHealthState(): void {\n this.connectedBrokers.clear();\n this.healthStatus = KafkaHealthStatuses.DISCONNECTED;\n }\n\n protected closeClient(): Promise<void> {\n return this.client.close();\n }\n\n /** Force-close through the callback overload: only Producer and Consumer expose `close(force, cb)`, and the `Base` type this class is generic over declares no such overload - hence the cast. */\n protected closeClientWithCallback(): Promise<void> {\n const client = this.client as AnyType;\n\n return new Promise<void>((resolve, reject) => {\n client.close(true, (error?: Error | null) => {\n if (error) {\n reject(error);\n\n return;\n }\n\n resolve();\n });\n });\n }\n\n protected async gracefulCloseClient(): Promise<void> {\n let timeoutHandle: NodeJS.Timeout | null = null;\n\n try {\n // The timer MUST be cleared once close() wins: an armed shutdownTimeout timer keeps the event loop alive long after a successful shutdown, so the process refuses to exit.\n await Promise.race([\n this.closeClient(),\n new Promise<void>((_, reject) => {\n timeoutHandle = setTimeout(\n () => reject(getError({ message: '[gracefulCloseClient] Shutdown timed out' })),\n this.shutdownTimeout,\n );\n }),\n ]);\n } finally {\n if (timeoutHandle !== null) {\n clearTimeout(timeoutHandle);\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../../src/modules/queue/kafka/base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,mBAAmB,GAEpB,MAAM,oBAAoB,CAAC;AAQ5B,MAAM,gBAAgB,GAAG,CAAC,MAAe,EAAkC,EAAE;IAC3E,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,OAAO,IAAI,MAAM;QACjB,OAAO,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,EAAE,CAAC,CAAC,EAC/E,IAAI,CAAC,eAAe,CACrB,CAAC;gBACJ,CAAC,CAAC;aACH,CAAC,CAAC;QACL,CAAC;gBAAS,CAAC;YACT,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;gBAC3B,YAAY,CAAC,aAAa,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import { getError } from '@/modules/error';\nimport { BaseHelper } from '@/modules/base';\nimport type { Base, BaseOptions, ConnectionPoolEventPayload } from '@platformatic/kafka';\nimport { HookUtility } from '../common';\nimport {\n KafkaClientEvents,\n KafkaDefaults,\n KafkaHealthStatuses,\n type TKafkaHealthStatus,\n} from './common/constants';\nimport type { TKafkaBrokerEventCallback } from './common/types';\n\n/** A client that closes through `close(force, callback)`, which only Producer and Consumer expose. */\ninterface ICallbackCloseClient {\n close(force: boolean, callback: (error?: Error | null) => void): void;\n}\n\nconst hasCallbackClose = (client: unknown): client is ICallbackCloseClient => {\n return (\n typeof client === 'object' &&\n client !== null &&\n 'close' in client &&\n typeof client.close === 'function'\n );\n};\n\n/** Options shared by the Kafka helpers. */\nexport interface IKafkaBaseOptions<TClient extends Base<BaseOptions>> {\n scope: string;\n identifier: string;\n client: TClient;\n /**\n * Milliseconds the helper waits for in-flight work on shutdown.\n * @default KafkaDefaults.SHUTDOWN_TIMEOUT\n */\n shutdownTimeout?: number;\n onBrokerConnect?: TKafkaBrokerEventCallback;\n onBrokerDisconnect?: TKafkaBrokerEventCallback;\n}\n\n/**\n * Shared health tracking and broker event wiring for all Kafka helpers (producer, consumer, admin); `TClient` is the platformatic client type, passed by subclasses via `super({ client })`.\n *\n * Every member below is `protected`, not `private`, so a producer, consumer, or admin helper can extend this class instead of forking it. That visibility is not a stable contract between minor versions.\n */\nexport abstract class BaseKafkaHelper<TClient extends Base<BaseOptions>> extends BaseHelper {\n protected client: TClient;\n protected readonly shutdownTimeout: number;\n\n protected healthStatus: TKafkaHealthStatus = KafkaHealthStatuses.UNKNOWN;\n\n /** Per-broker connection state. Key = \"host:port\" */\n protected readonly connectedBrokers = new Set<string>();\n\n protected readonly onBrokerConnect?: TKafkaBrokerEventCallback;\n protected readonly onBrokerDisconnect?: TKafkaBrokerEventCallback;\n\n constructor(opts: IKafkaBaseOptions<TClient>) {\n super({ scope: opts.scope, identifier: opts.identifier });\n this.client = opts.client;\n this.shutdownTimeout = opts.shutdownTimeout ?? KafkaDefaults.SHUTDOWN_TIMEOUT;\n this.onBrokerConnect = opts.onBrokerConnect;\n this.onBrokerDisconnect = opts.onBrokerDisconnect;\n }\n\n isHealthy(): boolean {\n return this.connectedBrokers.size > 0;\n }\n\n isReady(): boolean {\n return this.healthStatus === KafkaHealthStatuses.CONNECTED;\n }\n\n getHealthStatus(): TKafkaHealthStatus {\n return this.healthStatus;\n }\n\n getConnectedBrokerCount(): number {\n return this.connectedBrokers.size;\n }\n\n protected configureBrokerConnect() {\n this.client.on(KafkaClientEvents.BROKER_CONNECT, (payload: ConnectionPoolEventPayload) => {\n const brokerKey = `${payload.broker.host}:${payload.broker.port}`;\n this.connectedBrokers.add(brokerKey);\n this.healthStatus = KafkaHealthStatuses.CONNECTED;\n\n this.logger.info(\n '[configureBrokerConnect] Broker CONNECTED | Host: %s | Port: %d | Connected: %d',\n payload.broker.host,\n payload.broker.port,\n this.connectedBrokers.size,\n );\n\n HookUtility.invoke({\n logger: this.logger,\n scope: 'configureBrokerConnect',\n execution: () => this.onBrokerConnect?.({ broker: payload.broker }),\n });\n });\n }\n\n protected configureBrokerDisconnect() {\n this.client.on(KafkaClientEvents.BROKER_DISCONNECT, (payload: ConnectionPoolEventPayload) => {\n const brokerKey = `${payload.broker.host}:${payload.broker.port}`;\n this.connectedBrokers.delete(brokerKey);\n\n if (this.connectedBrokers.size === 0) {\n this.healthStatus = KafkaHealthStatuses.DISCONNECTED;\n }\n\n this.logger.warn(\n '[configureBrokerDisconnect] Broker DISCONNECTED | Host: %s | Port: %d | Remaining: %d',\n payload.broker.host,\n payload.broker.port,\n this.connectedBrokers.size,\n );\n\n HookUtility.invoke({\n logger: this.logger,\n scope: 'configureBrokerDisconnect',\n execution: () => this.onBrokerDisconnect?.({ broker: payload.broker }),\n });\n });\n }\n\n protected configureBrokerFailed() {\n this.client.on(KafkaClientEvents.BROKER_FAILED, (payload: ConnectionPoolEventPayload) => {\n const brokerKey = `${payload.broker.host}:${payload.broker.port}`;\n this.connectedBrokers.delete(brokerKey);\n\n if (this.connectedBrokers.size === 0) {\n this.healthStatus = KafkaHealthStatuses.DISCONNECTED;\n }\n\n this.logger.error(\n '[configureBrokerFailed] Broker connection FAILED | Host: %s | Port: %d | Remaining: %d',\n payload.broker.host,\n payload.broker.port,\n this.connectedBrokers.size,\n );\n });\n }\n\n protected configureBrokerEvents(): void {\n this.configureBrokerConnect();\n this.configureBrokerDisconnect();\n this.configureBrokerFailed();\n }\n\n protected unconfigureBrokerEvents(): void {\n this.client.removeAllListeners(KafkaClientEvents.BROKER_CONNECT);\n this.client.removeAllListeners(KafkaClientEvents.BROKER_DISCONNECT);\n this.client.removeAllListeners(KafkaClientEvents.BROKER_FAILED);\n }\n\n protected swapClient(newClient: TClient): void {\n this.unconfigureBrokerEvents();\n this.client = newClient;\n this.resetHealthState();\n this.configureBrokerEvents();\n }\n\n protected resetHealthState(): void {\n this.connectedBrokers.clear();\n this.healthStatus = KafkaHealthStatuses.DISCONNECTED;\n }\n\n protected closeClient(): Promise<void> {\n return this.client.close();\n }\n\n /** Force-close through the callback overload: only Producer and Consumer expose `close(force, cb)`, and the `Base` type this class is generic over declares no such overload - hence the guard. */\n protected closeClientWithCallback(): Promise<void> {\n const client = this.client;\n\n if (!hasCallbackClose(client)) {\n return Promise.reject(\n getError({ message: '[closeClientWithCallback] The client has no close(force, callback)' }),\n );\n }\n\n const closable: ICallbackCloseClient = client;\n\n return new Promise<void>((resolve, reject) => {\n // Through `ICallbackCloseClient`, not `client`: on the narrowed type the overload of `Base.close` wins and takes no `force`.\n closable.close(true, (error?: Error | null) => {\n if (error) {\n reject(error);\n\n return;\n }\n\n resolve();\n });\n });\n }\n\n protected async gracefulCloseClient(): Promise<void> {\n let timeoutHandle: NodeJS.Timeout | null = null;\n\n try {\n // The timer MUST be cleared once close() wins: an armed shutdownTimeout timer keeps the event loop alive long after a successful shutdown, so the process refuses to exit.\n await Promise.race([\n this.closeClient(),\n new Promise<void>((_, reject) => {\n timeoutHandle = setTimeout(\n () => reject(getError({ message: '[gracefulCloseClient] Shutdown timed out' })),\n this.shutdownTimeout,\n );\n }),\n ]);\n } finally {\n if (timeoutHandle !== null) {\n clearTimeout(timeoutHandle);\n }\n }\n }\n}\n"]}
@@ -23,8 +23,6 @@ export declare class KafkaHealthStatuses {
23
23
  static readonly CONNECTED = "connected";
24
24
  static readonly DISCONNECTED = "disconnected";
25
25
  static readonly UNKNOWN = "unknown";
26
- static readonly SCHEME_SET: Set<string>;
27
- static isValid(status: string): boolean;
28
26
  }
29
27
  /** A {@link KafkaHealthStatuses} value. */
30
28
  export type TKafkaHealthStatus = TConstValue<typeof KafkaHealthStatuses>;
@@ -68,8 +66,6 @@ export declare class KafkaDefaults {
68
66
  export declare class KafkaBrokersDownReasons {
69
67
  static readonly DISCONNECTED = "disconnected";
70
68
  static readonly FAILED = "failed";
71
- static readonly SCHEME_SET: Set<string>;
72
- static isValid(value: string): boolean;
73
69
  }
74
70
  /** Valid Kafka brokers-down reason values. */
75
71
  export type TKafkaBrokersDownReason = TConstValue<typeof KafkaBrokersDownReasons>;
@@ -22,10 +22,6 @@ export class KafkaHealthStatuses {
22
22
  static { this.CONNECTED = 'connected'; }
23
23
  static { this.DISCONNECTED = 'disconnected'; }
24
24
  static { this.UNKNOWN = 'unknown'; }
25
- static { this.SCHEME_SET = new Set([this.CONNECTED, this.DISCONNECTED, this.UNKNOWN]); }
26
- static isValid(status) {
27
- return this.SCHEME_SET.has(status);
28
- }
29
25
  }
30
26
  /** Names of the `@platformatic/kafka` client, consumer-group and stream events the helpers listen to. */
31
27
  export class KafkaClientEvents {
@@ -70,9 +66,5 @@ export class KafkaDefaults {
70
66
  export class KafkaBrokersDownReasons {
71
67
  static { this.DISCONNECTED = 'disconnected'; }
72
68
  static { this.FAILED = 'failed'; }
73
- static { this.SCHEME_SET = new Set([this.DISCONNECTED, this.FAILED]); }
74
- static isValid(value) {
75
- return this.SCHEME_SET.has(value);
76
- }
77
69
  }
78
70
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../../src/modules/queue/kafka/common/constants.ts"],"names":[],"mappings":"AAEA,wDAAwD;AACxD,MAAM,OAAO,kBAAkB;aACb,YAAO,GAAG,SAAS,CAAC;aACpB,aAAQ,GAAG,UAAU,CAAC;aAEtB,eAAU,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEpE,MAAM,CAAC,OAAO,CAAC,IAAY;QACzB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;;AAMH,kHAAkH;AAClH,MAAM,OAAO,SAAS;aACJ,SAAI,GAAG,CAAC,CAAC,GAAC,sCAAsC;aAChD,WAAM,GAAG,CAAC,CAAC,GAAC,6BAA6B;aACzC,QAAG,GAAG,CAAC,CAAC,CAAC,GAAC,0BAA0B;aAEpC,eAAU,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAEzE,MAAM,CAAC,OAAO,CAAC,GAAW;QACxB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAClC,CAAC;;AAMH,+EAA+E;AAC/E,MAAM,OAAO,mBAAmB;aACd,cAAS,GAAG,WAAW,CAAC;aACxB,iBAAY,GAAG,cAAc,CAAC;aAC9B,YAAO,GAAG,SA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0BAAqB,GAAG,QAAQ,CAAC;aAEjC,oBAAe,GAAG,KAAK,CAAC;aACxB,2BAAsB,GAAG,CAAC,CAAC;aAC3B,qBAAgB,GAAG,MAAM,CAAC;aAC1B,yBAAoB,GAAG,MAAM,CAAC;;AAGhD,8GAA8G;AAC9G,MAAM,OAAO,uBAAuB;aAClB,iBAAY,GAAG,cAAc,CAAC;aAC9B,WAAM,GAAG,QAAQ,CAAC;aAElB,eAAU,GAAG,IAAI,GAAG,CAAS,CAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAE/E,MAAM,CAAC,OAAO,CAAC,KAAa;QAC1B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC","sourcesContent":["import { type TConstValue } from '@/common';\n\n/** Consumer group protocol: `classic` or `consumer`. */\nexport class KafkaGroupProtocol {\n static readonly CLASSIC = 'classic';\n static readonly CONSUMER = 'consumer';\n\n static readonly SCHEME_SET = new Set([this.CLASSIC, this.CONSUMER]);\n\n static isValid(mode: string): boolean {\n return this.SCHEME_SET.has(mode);\n }\n}\n\n/** A {@link KafkaGroupProtocol} value. */\nexport type TKafkaGroupProtocol = TConstValue<typeof KafkaGroupProtocol>;\n\n/** Producer acknowledgment level, as the Kafka `acks` number: `0` none, `1` leader, `-1` all in-sync replicas. */\nexport class KafkaAcks {\n static readonly NONE = 0; // No acknowledgment — fire-and-forget\n static readonly LEADER = 1; // Leader acknowledgment only\n static readonly ALL = -1; // Full ISR acknowledgment\n\n static readonly SCHEME_SET = new Set([this.NONE, this.LEADER, this.ALL]);\n\n static isValid(ack: number): boolean {\n return this.SCHEME_SET.has(ack);\n }\n}\n\n/** A {@link KafkaAcks} value. */\nexport type TKafkaAcks = TConstValue<typeof KafkaAcks>;\n\n/** Connection health of a Kafka helper, as reported by `getHealthStatus()`. */\nexport class KafkaHealthStatuses {\n static readonly CONNECTED = 'connected';\n static readonly DISCONNECTED = 'disconnected';\n static readonly UNKNOWN = 'unknown';\n\n static readonly SCHEME_SET = new Set([this.CONNECTED, this.DISCONNECTED, this.UNKNOWN]);\n\n static isValid(status: string): boolean {\n return this.SCHEME_SET.has(status);\n }\n}\n\n/** A {@link KafkaHealthStatuses} value. */\nexport type TKafkaHealthStatus = TConstValue<typeof KafkaHealthStatuses>;\n\n/** Names of the `@platformatic/kafka` client, consumer-group and stream events the helpers listen to. */\nexport class KafkaClientEvents {\n static readonly BROKER_CONNECT = 'client:broker:connect';\n static readonly BROKER_DISCONNECT = 'client:broker:disconnect';\n static readonly BROKER_FAILED = 'client:broker:failed';\n\n static readonly CONSUMER_GROUP_JOIN = 'consumer:group:join';\n static readonly CONSUMER_GROUP_LEAVE = 'consumer:group:leave';\n static readonly CONSUMER_GROUP_REBALANCE = 'consumer:group:rebalance';\n static readonly CONSUMER_HEARTBEAT_ERROR = 'consumer:heartbeat:error';\n static readonly CONSUMER_LAG = 'consumer:lag';\n static readonly CONSUMER_LAG_ERROR = 'consumer:lag:error';\n\n static readonly STREAM_DATA = 'data';\n static readonly STREAM_ERROR = 'error';\n}\n\n/** The option values the Kafka helpers use for a field the caller leaves unset. Timeouts, delays and intervals are in milliseconds; `MAX_BYTES` is in bytes. */\nexport class KafkaDefaults {\n // Shared\n static readonly RETRIES = 3;\n static readonly RETRY_DELAY = 1_000;\n\n // Producer\n static readonly STRICT = true;\n static readonly AUTOCREATE_TOPICS = false;\n\n // Consumer\n static readonly AUTOCOMMIT = false;\n static readonly SESSION_TIMEOUT = 60_000;\n static readonly HEARTBEAT_INTERVAL = 10_000;\n static readonly HIGH_WATER_MARK = 1024;\n static readonly MIN_BYTES = 1;\n static readonly MAX_BYTES = 1_048_576 * 10; // 10 MB\n static readonly MAX_WAIT_TIME = 5_000;\n static readonly METADATA_MAX_AGE = 300_000;\n static readonly GROUP_PROTOCOL = KafkaGroupProtocol.CLASSIC;\n\n static readonly CONSUME_MODE = 'committed';\n static readonly CONSUME_FALLBACK_MODE = 'latest';\n\n static readonly RECONNECT_DELAY = 2_000;\n static readonly MAX_RECONNECT_ATTEMPTS = 5;\n static readonly SHUTDOWN_TIMEOUT = 30_000;\n static readonly LAG_MONITOR_INTERVAL = 30_000;\n}\n\n/** Why every broker of a consumer is gone: DISCONNECTED (the connections closed) or FAILED (they errored). */\nexport class KafkaBrokersDownReasons {\n static readonly DISCONNECTED = 'disconnected';\n static readonly FAILED = 'failed';\n\n static readonly SCHEME_SET = new Set<string>([this.DISCONNECTED, this.FAILED]);\n\n static isValid(value: string): boolean {\n return this.SCHEME_SET.has(value);\n }\n}\n\n/** Valid Kafka brokers-down reason values. */\nexport type TKafkaBrokersDownReason = TConstValue<typeof KafkaBrokersDownReasons>;\n"]}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../../../../src/modules/queue/kafka/common/constants.ts"],"names":[],"mappings":"AAEA,wDAAwD;AACxD,MAAM,OAAO,kBAAkB;aACb,YAAO,GAAG,SAAS,CAAC;aACpB,aAAQ,GAAG,UAAU,CAAC;aAEtB,eAAU,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEpE,MAAM,CAAC,OAAO,CAAC,IAAY;QACzB,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;;AAMH,kHAAkH;AAClH,MAAM,OAAO,SAAS;aACJ,SAAI,GAAG,CAAC,CAAC,GAAC,sCAAsC;aAChD,WAAM,GAAG,CAAC,CAAC,GAAC,6BAA6B;aACzC,QAAG,GAAG,CAAC,CAAC,CAAC,GAAC,0BAA0B;aAEpC,eAAU,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAEzE,MAAM,CAAC,OAAO,CAAC,GAAW;QACxB,OAAO,IAAI,CAAC,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xB,2BAAsB,GAAG,CAAC,CAAC;aAC3B,qBAAgB,GAAG,MAAM,CAAC;aAC1B,yBAAoB,GAAG,MAAM,CAAC;;AAGhD,8GAA8G;AAC9G,MAAM,OAAO,uBAAuB;aAClB,iBAAY,GAAG,cAAc,CAAC;aAC9B,WAAM,GAAG,QAAQ,CAAC","sourcesContent":["import { type TConstValue } from '@/common';\n\n/** Consumer group protocol: `classic` or `consumer`. */\nexport class KafkaGroupProtocol {\n static readonly CLASSIC = 'classic';\n static readonly CONSUMER = 'consumer';\n\n static readonly SCHEME_SET = new Set([this.CLASSIC, this.CONSUMER]);\n\n static isValid(mode: string): boolean {\n return this.SCHEME_SET.has(mode);\n }\n}\n\n/** A {@link KafkaGroupProtocol} value. */\nexport type TKafkaGroupProtocol = TConstValue<typeof KafkaGroupProtocol>;\n\n/** Producer acknowledgment level, as the Kafka `acks` number: `0` none, `1` leader, `-1` all in-sync replicas. */\nexport class KafkaAcks {\n static readonly NONE = 0; // No acknowledgment — fire-and-forget\n static readonly LEADER = 1; // Leader acknowledgment only\n static readonly ALL = -1; // Full ISR acknowledgment\n\n static readonly SCHEME_SET = new Set([this.NONE, this.LEADER, this.ALL]);\n\n static isValid(ack: number): boolean {\n return this.SCHEME_SET.has(ack);\n }\n}\n\n/** A {@link KafkaAcks} value. */\nexport type TKafkaAcks = TConstValue<typeof KafkaAcks>;\n\n/** Connection health of a Kafka helper, as reported by `getHealthStatus()`. */\nexport class KafkaHealthStatuses {\n static readonly CONNECTED = 'connected';\n static readonly DISCONNECTED = 'disconnected';\n static readonly UNKNOWN = 'unknown';\n}\n\n/** A {@link KafkaHealthStatuses} value. */\nexport type TKafkaHealthStatus = TConstValue<typeof KafkaHealthStatuses>;\n\n/** Names of the `@platformatic/kafka` client, consumer-group and stream events the helpers listen to. */\nexport class KafkaClientEvents {\n static readonly BROKER_CONNECT = 'client:broker:connect';\n static readonly BROKER_DISCONNECT = 'client:broker:disconnect';\n static readonly BROKER_FAILED = 'client:broker:failed';\n\n static readonly CONSUMER_GROUP_JOIN = 'consumer:group:join';\n static readonly CONSUMER_GROUP_LEAVE = 'consumer:group:leave';\n static readonly CONSUMER_GROUP_REBALANCE = 'consumer:group:rebalance';\n static readonly CONSUMER_HEARTBEAT_ERROR = 'consumer:heartbeat:error';\n static readonly CONSUMER_LAG = 'consumer:lag';\n static readonly CONSUMER_LAG_ERROR = 'consumer:lag:error';\n\n static readonly STREAM_DATA = 'data';\n static readonly STREAM_ERROR = 'error';\n}\n\n/** The option values the Kafka helpers use for a field the caller leaves unset. Timeouts, delays and intervals are in milliseconds; `MAX_BYTES` is in bytes. */\nexport class KafkaDefaults {\n // Shared\n static readonly RETRIES = 3;\n static readonly RETRY_DELAY = 1_000;\n\n // Producer\n static readonly STRICT = true;\n static readonly AUTOCREATE_TOPICS = false;\n\n // Consumer\n static readonly AUTOCOMMIT = false;\n static readonly SESSION_TIMEOUT = 60_000;\n static readonly HEARTBEAT_INTERVAL = 10_000;\n static readonly HIGH_WATER_MARK = 1024;\n static readonly MIN_BYTES = 1;\n static readonly MAX_BYTES = 1_048_576 * 10; // 10 MB\n static readonly MAX_WAIT_TIME = 5_000;\n static readonly METADATA_MAX_AGE = 300_000;\n static readonly GROUP_PROTOCOL = KafkaGroupProtocol.CLASSIC;\n\n static readonly CONSUME_MODE = 'committed';\n static readonly CONSUME_FALLBACK_MODE = 'latest';\n\n static readonly RECONNECT_DELAY = 2_000;\n static readonly MAX_RECONNECT_ATTEMPTS = 5;\n static readonly SHUTDOWN_TIMEOUT = 30_000;\n static readonly LAG_MONITOR_INTERVAL = 30_000;\n}\n\n/** Why every broker of a consumer is gone: DISCONNECTED (the connections closed) or FAILED (they errored). */\nexport class KafkaBrokersDownReasons {\n static readonly DISCONNECTED = 'disconnected';\n static readonly FAILED = 'failed';\n}\n\n/** Valid Kafka brokers-down reason values. */\nexport type TKafkaBrokersDownReason = TConstValue<typeof KafkaBrokersDownReasons>;\n"]}
@@ -16,15 +16,26 @@ export declare class AbstractRedisHelper<ClientType extends TRedisClient = TRedi
16
16
  identifier: string;
17
17
  client: ClientType;
18
18
  } & IRedisHelperCallbacks);
19
+ /** `maxRetry` is `0`, `RedisDefaults.RETRY_FOREVER` or a positive integer; anything else is a typo that would silently change how the client reconnects. */
20
+ protected static assertMaxRetry(opts: {
21
+ scope: string;
22
+ name: string;
23
+ maxRetry: number;
24
+ }): void;
25
+ /** The wait before each reconnect, then `undefined` once `maxRetry` attempts failed: ioredis stops and the helper logs it once. */
19
26
  protected static buildRetryStrategy(opts: {
27
+ scope: string;
28
+ name: string;
20
29
  maxRetry: number;
21
- }): (attemptCounter: number) => number | undefined;
30
+ }): (attempt: number) => number | undefined;
22
31
  protected static buildDefaultOpts(opts: {
32
+ scope: string;
33
+ name: string;
23
34
  maxRetry: number;
24
35
  }): {
25
36
  showFriendlyErrorStack: boolean;
26
37
  maxRetriesPerRequest: null;
27
- retryStrategy: (attemptCounter: number) => number | undefined;
38
+ retryStrategy: (attempt: number) => number | undefined;
28
39
  };
29
40
  getClient(): ClientType;
30
41
  duplicateClient(): TRedisClient;