Skip to content

Commit

Permalink
MET-425: Use common healthchecks from fastify-extras
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewi-wd committed Nov 29, 2024
1 parent 408e416 commit 3e2e1bd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 50 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@fastify/swagger": "^9.4.0",
"@lokalise/backend-http-client": "^2.4.0",
"@lokalise/background-jobs-common": "^9.0.0",
"@lokalise/fastify-extras": "^25.3.0",
"@lokalise/fastify-extras": "^25.4.1",
"@lokalise/healthcheck-utils": "^1.4.0",
"@lokalise/id-utils": "^2.2.0",
"@lokalise/node-core": "^13.1.0",
Expand Down
12 changes: 5 additions & 7 deletions src/app.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ describe('app', () => {
})

describe('healthcheck', () => {
it('Returns health check information', async () => {
it('Returns public health check information', async () => {
const response = await app.inject().get('/').end()

expect(response.json()).toMatchObject({
healthChecks: {
heartbeat: 'HEALTHY',
redis: 'HEALTHY',
postgres: 'HEALTHY',
},
gitCommitSha: 'sha',
heartbeat: 'HEALTHY',
version: '1',
})
expect(response.statusCode).toBe(200)
})

it('Returns public health check information', async () => {
it('Returns private health check information', async () => {
const response = await app.inject().get('/health').end()

expect(response.json()).toMatchObject({
Expand Down
23 changes: 2 additions & 21 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import fastifySwagger from '@fastify/swagger'
import {
amplitudePlugin,
bugsnagPlugin,
commonHealthcheckPlugin,
getRequestIdFastifyAppConfig,
metricsPlugin,
newrelicTransactionManagerPlugin,
publicHealthcheckPlugin,
requestContextProviderPlugin,
} from '@lokalise/fastify-extras'
import { type CommonLogger, resolveLogger } from '@lokalise/node-core'
Expand All @@ -25,7 +25,6 @@ import scalarFastifyApiReference from '@scalar/fastify-api-reference'
import { type AwilixContainer, asFunction } from 'awilix'
import fastify from 'fastify'
import type { FastifyInstance } from 'fastify'
import customHealthCheck from 'fastify-custom-healthcheck'
import fastifyGracefulShutdown from 'fastify-graceful-shutdown'
import fastifyNoIcon from 'fastify-no-icon'
import {
Expand All @@ -42,7 +41,6 @@ import { errorHandler } from './infrastructure/errors/errorHandler.js'
import {
dbHealthCheck,
redisHealthCheck,
registerHealthChecks,
} from './infrastructure/healthchecks/healthchecksWrappers.js'
import { SINGLETON_CONFIG, registerDependencies } from './infrastructure/parentDiConfig.js'
import type { DependencyOverrides } from './infrastructure/parentDiConfig.js'
Expand Down Expand Up @@ -244,20 +242,7 @@ export async function getApp(
}

if (configOverrides.healthchecksEnabled !== false) {
await app.register(customHealthCheck, {
path: '/',
logLevel: 'warn',
info: {
env: appConfig.nodeEnv,
app_version: appConfig.appVersion,
git_commit_sha: appConfig.gitCommitSha,
},
schema: false,
exposeFailure: false,
})

await app.register(publicHealthcheckPlugin, {
url: '/health',
await app.register(commonHealthcheckPlugin, {
healthChecks: [
{
name: 'postgres',
Expand Down Expand Up @@ -318,10 +303,6 @@ export async function getApp(
return Promise.resolve()
})
}

if (configOverrides.healthchecksEnabled !== false) {
registerHealthChecks(app)
}
})

try {
Expand Down
17 changes: 0 additions & 17 deletions src/infrastructure/healthchecks/healthchecksWrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ import type { HealthChecker } from '@lokalise/fastify-extras'
import type { Either } from '@lokalise/node-core'
import type { FastifyInstance } from 'fastify'

import type { AppInstance } from '../../app.js'

export const wrapHealthCheck = (app: AppInstance, healthCheck: HealthChecker) => {
return async () => {
const response = await healthCheck(app as unknown as FastifyInstance)
if (response.error) {
throw response.error
}
}
}

export const redisHealthCheck: HealthChecker = (
app: FastifyInstance,
): Promise<Either<Error, true>> => {
Expand All @@ -38,9 +27,3 @@ export const dbHealthCheck: HealthChecker = (
}
return Promise.resolve({ result: true })
}

export function registerHealthChecks(app: AppInstance) {
app.addHealthCheck('heartbeat', () => true)
app.addHealthCheck('redis', wrapHealthCheck(app, redisHealthCheck))
app.addHealthCheck('postgres', wrapHealthCheck(app, dbHealthCheck))
}

0 comments on commit 3e2e1bd

Please sign in to comment.