@tmlmobilidade/writers 20260326.1644.50 → 20260326.2139.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/generic.d.ts CHANGED
@@ -25,6 +25,16 @@ interface BatchWriterParams<T> {
25
25
  * @required
26
26
  */
27
27
  insertFn: (data: T[]) => Promise<void>;
28
+ /**
29
+ * Maximum number of retries for transient insert errors.
30
+ * @default 3
31
+ */
32
+ max_retries?: number;
33
+ /**
34
+ * Base delay in milliseconds for exponential backoff.
35
+ * @default 1000
36
+ */
37
+ retry_base_delay_ms?: number;
28
38
  /**
29
39
  * The title of this BatchWriter instance,
30
40
  * used to identify the source of the logs.
@@ -47,12 +57,21 @@ export declare class BatchWriter<T> {
47
57
  * @param callback Optional callback to execute after the flush is complete, receiving the flushed data as a parameter
48
58
  */
49
59
  flush(callback?: (data?: T[]) => Promise<void>): Promise<void>;
60
+ /**
61
+ * Helper method to perform insert operations with retry logic for transient errors.
62
+ * This method will attempt to insert the data using the provided insert function,
63
+ * and if an error occurs, it will retry the operation with exponential backoff
64
+ * until the maximum number of retries is reached.
65
+ * @param data The data to insert.
66
+ * @returns A promise that resolves when the insert operation is successful, or rejects if all retries fail.
67
+ */
68
+ private insertWithRetry;
50
69
  /**
51
70
  * Write data to the batch.
52
- * @param data The data to write
53
- * @param options Options for the write operation (reserved for future use)
54
- * @param writeCallback Callback function to call after the write operation is complete
55
- * @param flushCallback Callback function to call after the flush operation is complete
71
+ * @param data The data to write.
72
+ * @param options Options for the write operation (reserved for future use).
73
+ * @param writeCallback Callback function to call after the write operation is complete.
74
+ * @param flushCallback Callback function to call after the flush operation is complete.
56
75
  */
57
76
  write(data: T | T[], { flushCallback, writeCallback }?: {
58
77
  flushCallback?: (data?: T[]) => Promise<void>;
package/dist/generic.js CHANGED
@@ -57,7 +57,7 @@ export class BatchWriter {
57
57
  // Call the insert function provided in the params to perform the actual database insertion.
58
58
  if (!this.params.insertFn)
59
59
  throw new Error('BATCHWRITER: No insert function provided in params');
60
- await this.params.insertFn(this.dataBucketFlushOps);
60
+ await this.insertWithRetry(this.dataBucketFlushOps);
61
61
  Logger.info(`BATCHWRITER [${this.params.title}]: Flush | Length: ${this.dataBucketFlushOps.length} (session: ${sessionTimerResult}) (flush: ${flushTimer.get()})`);
62
62
  // Call the flush callback, if provided
63
63
  if (callback)
@@ -76,12 +76,37 @@ export class BatchWriter {
76
76
  throw error; // Re-throw to allow retry logic at higher level
77
77
  }
78
78
  }
79
+ /**
80
+ * Helper method to perform insert operations with retry logic for transient errors.
81
+ * This method will attempt to insert the data using the provided insert function,
82
+ * and if an error occurs, it will retry the operation with exponential backoff
83
+ * until the maximum number of retries is reached.
84
+ * @param data The data to insert.
85
+ * @returns A promise that resolves when the insert operation is successful, or rejects if all retries fail.
86
+ */
87
+ async insertWithRetry(data) {
88
+ const maxRetries = this.params.max_retries ?? 3;
89
+ const retryBaseDelayMs = this.params.retry_base_delay_ms ?? 1000;
90
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
91
+ try {
92
+ await this.params.insertFn(data);
93
+ return;
94
+ }
95
+ catch (error) {
96
+ const parsedError = error;
97
+ const nextAttempt = attempt + 1;
98
+ const delayMs = retryBaseDelayMs * (2 ** attempt);
99
+ Logger.error(`BATCHWRITER [${this.params.title}]: Transient insert error (${parsedError.code ?? 'unknown'}). Retrying ${nextAttempt}/${maxRetries} in ${delayMs}ms. ${parsedError.message}`);
100
+ await new Promise(resolve => setTimeout(resolve, delayMs));
101
+ }
102
+ }
103
+ }
79
104
  /**
80
105
  * Write data to the batch.
81
- * @param data The data to write
82
- * @param options Options for the write operation (reserved for future use)
83
- * @param writeCallback Callback function to call after the write operation is complete
84
- * @param flushCallback Callback function to call after the flush operation is complete
106
+ * @param data The data to write.
107
+ * @param options Options for the write operation (reserved for future use).
108
+ * @param writeCallback Callback function to call after the write operation is complete.
109
+ * @param flushCallback Callback function to call after the flush operation is complete.
85
110
  */
86
111
  async write(data, { flushCallback, writeCallback } = {}) {
87
112
  //
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/writers",
3
- "version": "20260326.1644.50",
3
+ "version": "20260326.2139.30",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"