From 1e2db857d0fbd49ecd96d853931e8882ce0ac80b Mon Sep 17 00:00:00 2001 From: Marek Fedorovic Date: Wed, 13 Sep 2023 14:02:23 +1000 Subject: [PATCH] fix: Use better error handling inside replicator install() --- .../src/cfg/config-kafka.ts | 4 ++-- cohort_banking_replicator_js/src/database.ts | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cohort_banking_replicator_js/src/cfg/config-kafka.ts b/cohort_banking_replicator_js/src/cfg/config-kafka.ts index 95a19fc4..dd5991f1 100644 --- a/cohort_banking_replicator_js/src/cfg/config-kafka.ts +++ b/cohort_banking_replicator_js/src/cfg/config-kafka.ts @@ -3,8 +3,8 @@ import { JsKafkaConfig } from "cohort_sdk_js" const kafkaConfig: JsKafkaConfig = { brokers: ["127.0.0.1:9092"], topic: "dev.ksp.certification", - clientId: "cohortr-replicator-js", - groupId: "cohortr-replicator-js", + clientId: "cohort-replicator-js3", + groupId: "cohort-replicator-js3", producerSendTimeoutMs: 10, logLevel: "info", producerConfigOverrides: {}, diff --git a/cohort_banking_replicator_js/src/database.ts b/cohort_banking_replicator_js/src/database.ts index d110708c..ea498f4b 100644 --- a/cohort_banking_replicator_js/src/database.ts +++ b/cohort_banking_replicator_js/src/database.ts @@ -95,12 +95,13 @@ export class Database { metric.callbackInstallCount++ try { + let recentError = null as any while (attemptNr <= retry.maxAttempts) { attemptNr++ if (attemptNr > retry.maxAttempts) { const elapsedSec = (Date.now() - startedAt) / 1000.0 - throw `Statemap install timed out after: ${elapsedSec} sec` + throw { message: `Statemap install timed out after: ${elapsedSec} sec`, error: recentError } } await cnn.query('BEGIN') @@ -145,15 +146,22 @@ export class Database { break } catch (e) { - await cnn.query('ROLLBACK') + let error = { installError: e, rollbackError: null } + try { + await cnn.query('ROLLBACK') + } catch (e) { + error.rollbackError = e + } finally { + recentError = error + } + await new Promise(resolve => setTimeout(resolve, retry.delayMs)) } } } finally { cnn?.release() + metric.attemptsUsed = attemptNr + this.metricsChannel.postMessage(metric) } - - metric.attemptsUsed = attemptNr - this.metricsChannel.postMessage(metric) } } \ No newline at end of file