-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(healthchecks): refactor out of modules (#3465)
- Loading branch information
1 parent
f716d0c
commit de4235a
Showing
8 changed files
with
139 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { healthCheckLogger } from '@/logging/logging' | ||
import { db } from '@/db/knex' | ||
import { highFrequencyMetricsCollectionPeriodMs } from '@/modules/shared/helpers/envHelper' | ||
import { | ||
handleLivenessFactory, | ||
handleReadinessFactory, | ||
knexFreeDbConnectionSamplerFactory, | ||
isRedisAlive, | ||
isPostgresAlive, | ||
FreeConnectionsCalculator | ||
} from '@/healthchecks/health' | ||
import { Application } from 'express' | ||
|
||
export const initFactory: () => ( | ||
app: Application, | ||
isInitial: boolean | ||
) => Promise<void> = () => { | ||
let knexFreeDbConnectionSamplerLiveness: FreeConnectionsCalculator & { | ||
start: () => void | ||
} | ||
let knexFreeDbConnectionSamplerReadiness: FreeConnectionsCalculator & { | ||
start: () => void | ||
} | ||
return async (app, isInitial) => { | ||
healthCheckLogger.info('💓 Init health check') | ||
if (isInitial) { | ||
knexFreeDbConnectionSamplerLiveness = knexFreeDbConnectionSamplerFactory({ | ||
db, | ||
collectionPeriod: highFrequencyMetricsCollectionPeriodMs(), | ||
sampledDuration: 600000 //number of ms over which to average the database connections, before declaring not alive. 10 minutes. | ||
}) | ||
knexFreeDbConnectionSamplerLiveness.start() | ||
|
||
knexFreeDbConnectionSamplerReadiness = knexFreeDbConnectionSamplerFactory({ | ||
db, | ||
collectionPeriod: highFrequencyMetricsCollectionPeriodMs(), | ||
sampledDuration: 20000 //number of ms over which to average the database connections, before declaring unready. 20 seconds. | ||
}) | ||
knexFreeDbConnectionSamplerReadiness.start() | ||
} | ||
const livenessHandler = handleLivenessFactory({ | ||
isRedisAlive, | ||
isPostgresAlive, | ||
freeConnectionsCalculator: knexFreeDbConnectionSamplerLiveness | ||
}) | ||
|
||
app.get('/liveness', async (req, res) => { | ||
const result = await livenessHandler() | ||
res.status(200).json({ status: 'ok', ...result }) | ||
}) | ||
|
||
app.get('/readiness', async (req, res) => { | ||
const result = await handleReadinessFactory({ | ||
isRedisAlive, | ||
isPostgresAlive, | ||
freeConnectionsCalculator: knexFreeDbConnectionSamplerReadiness | ||
})() | ||
res.status(200).json({ status: 'ok', ...result }) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* In JS catch clauses can receive not only Errors, but pretty much any other | ||
* kind of data type, so you can use this helper to ensure that | ||
* whatever is passed in is a real error. | ||
* If it is not a real error, it will be wrapped in a new error | ||
* with the provided message and the original error as the cause. | ||
*/ | ||
export function ensureErrorOrWrapAsCause(e: unknown, fallbackMessage?: string): Error { | ||
if (e instanceof Error) return e | ||
return new Error(fallbackMessage, { cause: e }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ | |
}, | ||
"include": [ | ||
"db/**/*", | ||
"healthchecks/**/*", | ||
"logging/**/*", | ||
"modules/**/*", | ||
"bin/**/*", | ||
|