From 11ec42dabbae0a48dee667d98f045b7317e736ed Mon Sep 17 00:00:00 2001 From: Trevor O Date: Thu, 12 Sep 2024 15:53:00 -0700 Subject: [PATCH] adding an injectable error handler for listen() --- javascript/.gitignore | 3 ++- javascript/src/rpc.ts | 4 ++-- javascript/src/utils/polling.ts | 11 ++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/javascript/.gitignore b/javascript/.gitignore index 7fb9ee4..41bd35a 100644 --- a/javascript/.gitignore +++ b/javascript/.gitignore @@ -2,4 +2,5 @@ node_modules yarn-error.log .vscode dist -tsconfig.tsbuildinfo \ No newline at end of file +tsconfig.tsbuildinfo +package-lock.json diff --git a/javascript/src/rpc.ts b/javascript/src/rpc.ts index d715e57..0ac77cb 100644 --- a/javascript/src/rpc.ts +++ b/javascript/src/rpc.ts @@ -76,13 +76,13 @@ export class RetoolRPC { /** * Asynchronously starts listening for incoming Retool function invocations. */ - async listen(): Promise { + async listen(onError?: (error: unknown) => Promise): Promise { this._logger.info('Starting RPC agent') const registerResult = await loopWithBackoff(this._pollingIntervalMs, this._logger, () => this.registerAgent()) if (registerResult === 'done') { this._logger.info('Agent registered') this._logger.info('Starting processing query') - loopWithBackoff(this._pollingIntervalMs, this._logger, () => this.fetchQueryAndExecute()) + loopWithBackoff(this._pollingIntervalMs, this._logger, () => this.fetchQueryAndExecute(), onError) } } diff --git a/javascript/src/utils/polling.ts b/javascript/src/utils/polling.ts index 08d8f6a..5aae84d 100644 --- a/javascript/src/utils/polling.ts +++ b/javascript/src/utils/polling.ts @@ -15,6 +15,7 @@ export async function loopWithBackoff( pollingIntervalMs: number, logger: LoggerService, callback: () => Promise, + onError?: (error: unknown) => Promise, ): Promise { let delayTimeMs = CONNECTION_ERROR_INITIAL_TIMEOUT_MS let lastLoopTimestamp = Date.now() @@ -36,9 +37,13 @@ export async function loopWithBackoff( await sleep(pollingIntervalMs) delayTimeMs = Math.max(delayTimeMs / 2, CONNECTION_ERROR_INITIAL_TIMEOUT_MS) } catch (err: unknown) { - logger.error('Error running RPC agent', err) - await sleep(delayTimeMs) - delayTimeMs = Math.min(delayTimeMs * 2, CONNECTION_ERROR_RETRY_MAX_MS) + logger.debug('Error running RPC agent', err) + if (onError) { + await onError(err) + } else { + await sleep(delayTimeMs) + delayTimeMs = Math.min(delayTimeMs * 2, CONNECTION_ERROR_RETRY_MAX_MS) + } } } }