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

Commit

Permalink
Merge pull request #89 from ar-io/PE-5340-hydrate-contracts
Browse files Browse the repository at this point in the history
fix(PE-5340): make healthcheck dependent on result of prefetching…
  • Loading branch information
dtfiedler authored Jan 9, 2024
2 parents 32ddb90 + 98dba52 commit 3788394
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
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,
});
};

0 comments on commit 3788394

Please sign in to comment.