Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

fix(PE-5340): make healthcheck dependent on result of prefetching… #89

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ import {
} from './routes';
import { swaggerDocs } from './routes/swagger';
import { KoaContext } from './types';
import { getPrefetchStatusCode } from './system';

const router: Router = new Router();

// healthcheck
router.get('/healthcheck', (ctx) => {
ctx.body = {
timestamp: new Date(),
status: 200,
status: getPrefetchStatusCode(),
message: 'Hello world.',
};
});
Expand Down
20 changes: 18 additions & 2 deletions src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...', {
Expand All @@ -42,6 +48,7 @@ export const prefetchContracts = () => {
endTimestamp,
durationMs: endTimestamp - startTimestamp,
});
return true;
})
.catch((error: unknown) => {
const endTimestamp = Date.now();
Expand All @@ -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,
});
};
Loading