Skip to content

Commit

Permalink
fix(jobs): closing logic
Browse files Browse the repository at this point in the history
- close the app if db healthCheck fails 5 times in a row (not after the
  first failure)
- when closing first close the webserver and then destroy the db client
  • Loading branch information
TBonnin committed Jan 15, 2025
1 parent 3e5a6f7 commit 6593397
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions packages/jobs/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ try {

// We are using a setTimeout because we don't want overlapping setInterval if the DB is down
let healthCheck: NodeJS.Timeout | undefined;
let healthCheckFailures = 0;
const check = async () => {
const MAX_FAILURES = 5;
const TIMEOUT = 1000;
try {
await db.knex.raw('SELECT 1').timeout(1000);
healthCheck = setTimeout(check, 1000);
await db.knex.raw('SELECT 1').timeout(TIMEOUT);
healthCheckFailures = 0;
healthCheck = setTimeout(check, TIMEOUT);
} catch (err) {
logger.error('HealthCheck failed...', err);
void close();
healthCheckFailures += 1;
logger.error(`HealthCheck failed (${healthCheckFailures} times)...`, err);
if (healthCheckFailures > MAX_FAILURES) {
void close();
} else {
healthCheck = setTimeout(check, TIMEOUT);
}
}
};
void check();
Expand All @@ -39,8 +48,8 @@ try {
processor.stop();
otlp.stop();
await runnersFleet.stop();
await db.knex.destroy();
srv.close(() => {
srv.close(async () => {
await db.knex.destroy();
process.exit();
});
});
Expand Down

0 comments on commit 6593397

Please sign in to comment.