From bdb5162e3fff63d9dce20f126c609fafb966837a Mon Sep 17 00:00:00 2001 From: akumar Date: Fri, 22 Mar 2024 13:05:37 +0100 Subject: [PATCH] fix(facade): Add retry mechanism for set apiKey command incase consumer is not yet initialized --- .../src/modules/identity/api-key/api-key.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/facade/src/modules/identity/api-key/api-key.ts b/packages/facade/src/modules/identity/api-key/api-key.ts index 3a4aaa50..bf60e0c2 100644 --- a/packages/facade/src/modules/identity/api-key/api-key.ts +++ b/packages/facade/src/modules/identity/api-key/api-key.ts @@ -4,6 +4,7 @@ import type Application from 'koa'; import { Events, registerProtoMeta } from '@restorecommerce/kafka-client'; import { createServiceConfig } from '@restorecommerce/service-config'; import { protoMetadata as commandInterfaceMeta } from '@restorecommerce/rc-grpc-clients/dist/generated-server/io/restorecommerce/commandinterface.js'; +import * as retry from 'retry'; // const Router = require('koa-router'); @@ -93,11 +94,17 @@ const initApiKey = (logger: Logger, apiKey: boolean | string) => { } else { bootstrapApiKey = apiKey as string; } - exectueSetAPIKeyCommand(bootstrapApiKey, logger).then((resp) => { - logger.info('SetApiKey command response', resp); - logger.info(`Bootstrap API Key is: ${bootstrapApiKey}`); - }).catch(err => { - logger.error('Error executing setApiKey command', err); + const operation = retry.operation(); + operation.attempt(async () => { + exectueSetAPIKeyCommand(bootstrapApiKey, logger).then((resp) => { + logger.info('SetApiKey command response', resp); + logger.info(`Bootstrap API Key is: ${bootstrapApiKey}`); + }).catch(err => { + logger.error('Error executing setApiKey command', err); + operation.retry(err); + const attemptNo = (operation.attempts as () => number)(); + logger.info(`Retry exectueSetAPIKeyCommand, attempt no: ${attemptNo}`); + }); }); };