feat(core): Improve health check (#6205)

* remove unnecesary Db re-initialization

this is from before we added `Db.init` in `WorkflowRunnerProcess`

* feat(core): Improved health check

* make health check not care about DB connections

* close DB connections, and shutdown the timer
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-05-10 08:27:04 +00:00
committed by GitHub
parent e3f47994b1
commit 9e7b9fb443
9 changed files with 184 additions and 153 deletions

View File

@@ -159,33 +159,17 @@ export abstract class AbstractServer {
protected setupPushServer() {}
private async setupHealthCheck() {
this.app.use((req, res, next) => {
if (!Db.isInitialized) {
sendErrorResponse(res, new ServiceUnavailableError('Database is not ready!'));
} else next();
// health check should not care about DB connections
this.app.get('/healthz', async (req, res) => {
res.send({ status: 'ok' });
});
// Does very basic health check
this.app.get('/healthz', async (req, res) => {
Logger.debug('Health check started!');
const connection = Db.getConnection();
try {
if (!connection.isInitialized) {
// Connection is not active
throw new ServiceUnavailableError('No active database connection!');
}
// DB ping
await connection.query('SELECT 1');
} catch (error) {
ErrorReporter.error(error);
Logger.error('No Database connection!');
return sendErrorResponse(res, new ServiceUnavailableError('No Database connection!'));
}
Logger.debug('Health check completed successfully!');
sendSuccessResponse(res, { status: 'ok' }, true, 200);
const { connectionState } = Db;
this.app.use((req, res, next) => {
if (connectionState.connected) {
if (connectionState.migrated) next();
else res.send('n8n is starting up. Please wait');
} else sendErrorResponse(res, new ServiceUnavailableError('Database is not ready!'));
});
if (config.getEnv('executions.mode') === 'queue') {
@@ -400,8 +384,8 @@ export abstract class AbstractServer {
);
}
async start(): Promise<void> {
const { app, externalHooks, protocol, sslKey, sslCert } = this;
async init(): Promise<void> {
const { app, protocol, sslKey, sslCert } = this;
if (protocol === 'https' && sslKey && sslCert) {
const https = await import('https');
@@ -431,6 +415,12 @@ export abstract class AbstractServer {
await new Promise<void>((resolve) => this.server.listen(PORT, ADDRESS, () => resolve()));
await this.setupHealthCheck();
console.log(`n8n ready on ${ADDRESS}, port ${PORT}`);
}
async start(): Promise<void> {
await this.setupErrorHandlers();
this.setupPushServer();
await this.setupCommonMiddlewares();
@@ -438,11 +428,7 @@ export abstract class AbstractServer {
this.setupDevMiddlewares();
}
await this.setupHealthCheck();
await this.configure();
console.log(`n8n ready on ${ADDRESS}, port ${PORT}`);
console.log(`Version: ${N8N_VERSION}`);
const defaultLocale = config.getEnv('defaultLocale');
@@ -450,7 +436,7 @@ export abstract class AbstractServer {
console.log(`Locale: ${defaultLocale}`);
}
await externalHooks.run('n8n.ready', [this, config]);
await this.externalHooks.run('n8n.ready', [this, config]);
}
}