From 98dba52131f58761d117fb3283f14e143f9511b7 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 8 Jan 2024 18:06:58 -0700 Subject: [PATCH] fix(healthcheck): make healthcheck dependent on result of prefetching contracts --- src/router.ts | 3 ++- src/system.ts | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/router.ts b/src/router.ts index b4bdba8..3d6e034 100644 --- a/src/router.ts +++ b/src/router.ts @@ -30,6 +30,7 @@ import { } from './routes'; import { swaggerDocs } from './routes/swagger'; import { KoaContext } from './types'; +import { getPrefetchStatusCode } from './system'; const router: Router = new Router(); @@ -37,7 +38,7 @@ const router: Router = new Router(); router.get('/healthcheck', (ctx) => { ctx.body = { timestamp: new Date(), - status: 200, + status: getPrefetchStatusCode(), message: 'Hello world.', }; }); diff --git a/src/system.ts b/src/system.ts index 4827815..748082f 100644 --- a/src/system.ts +++ b/src/system.ts @@ -20,9 +20,15 @@ import { prefetchContractTxIds } from './config'; import logger from './logger'; import { warp } from './middleware'; -export const prefetchContracts = () => { +let successfullyPrefetchedContracts = false; + +export const getPrefetchStatusCode = () => { + return successfullyPrefetchedContracts ? 200 : 503; +}; + +export const prefetchContracts = async () => { // don't wait - just fire and forget - Promise.all( + const prefetchResults = await Promise.all( prefetchContractTxIds.map((contractTxId: string) => { const startTimestamp = Date.now(); logger.info('Pre-fetching contract state...', { @@ -42,6 +48,7 @@ export const prefetchContracts = () => { endTimestamp, durationMs: endTimestamp - startTimestamp, }); + return true; }) .catch((error: unknown) => { const endTimestamp = Date.now(); @@ -53,7 +60,16 @@ export const prefetchContracts = () => { endTimestamp, durationMs: endTimestamp - startTimestamp, }); + return false; }); }), ); + // update our healthcheck flag + successfullyPrefetchedContracts = prefetchResults.every( + (result) => result === true, + ); + logger.info('Finished pre-fetching contracts', { + success: successfullyPrefetchedContracts, + contractTxIds: prefetchContractTxIds, + }); };