Skip to content

Commit

Permalink
Merge pull request #222 from blockfrost/chore/exit-on-healthcheckfail
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek committed Jun 1, 2023
2 parents 2481af9 + cd006f8 commit dc7c5d7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"blockfrost",
"emurgo",
"from",
"HEALTHCHECK",
"keyhash",
"tailwindcss",
"to",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- process exits after health check fails for more than `HEALTHCHECK_FAIL_THRESHOLD_MS` (default 60s)

## [2.0.0] - 2023-04-05

### Added
Expand Down
14 changes: 0 additions & 14 deletions environment.d.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/constants/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ export const BLOCKFROST_REQUEST_CONCURRENCY = 500;

// How often should metrics be updated
export const METRICS_COLLECTOR_INTERVAL_MS = 10_000;

// If healthcheck repeatedly fails for duration longer than this constant the process exits
export const HEALTHCHECK_FAIL_THRESHOLD_MS = 6 * METRICS_COLLECTOR_INTERVAL_MS; // 6 health checks
1 change: 1 addition & 0 deletions src/utils/blockfrost-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const getBlockfrostClient = (options?: Partial<Options>) => {
return new BlockFrostAPI({
projectId: process.env.BLOCKFROST_PROJECT_ID,
customBackend: process.env.BLOCKFROST_BACKEND_URL || '',
// @ts-expect-error passing string network type
network: process.env.BLOCKFROST_NETWORK,
userAgent: `${packageJson.name}@${packageJson.version}`,
rateLimiter: false,
Expand Down
16 changes: 16 additions & 0 deletions src/utils/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { healthCheck } from './health';
import { assetMetadataLimiter, pLimiter, ratesLimiter } from './limiter';
import { logger } from './logger';
import { getPort } from './server';
import { HEALTHCHECK_FAIL_THRESHOLD_MS } from '../constants/config';

const port = getPort();

Expand All @@ -20,6 +21,7 @@ export class MetricsCollector {
intervalId: NodeJS.Timer | undefined;
wss: WebSocketServer;
healthy = true;
healthCheckFailingSince: number | null = null;

constructor(wss: WebSocketServer, interval: number) {
this.wss = wss;
Expand All @@ -31,9 +33,23 @@ export class MetricsCollector {
try {
await healthCheck(`ws://localhost:${port}`);
this.healthy = true;
this.healthCheckFailingSince = null;
} catch (error) {
logger.error(error);
this.healthy = false;
if (this.healthCheckFailingSince) {
const failDurationMs = Date.now() - this.healthCheckFailingSince;

if (failDurationMs > HEALTHCHECK_FAIL_THRESHOLD_MS) {
logger.error(
`Healthcheck failing for longer than ${HEALTHCHECK_FAIL_THRESHOLD_MS} ms. Exiting process.`,
);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
} else {
this.healthCheckFailingSince = Date.now();
}
}
logger.info('[HealthCheck] Health check done');
return {
Expand Down

0 comments on commit dc7c5d7

Please sign in to comment.