mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 12:57:29 -08:00
🐛 Fix db connection check /healthz (#2041)
* 🐛 fix db connection check
* 👕 fix missing semi
* ⏪ Revert "🐛 Hardcode typeorm@0.2.34 as new version makes health-check fail"
This reverts commit ddee2ec47c
.
* 🐛 fix health-check in WebhookServer
This commit is contained in:
parent
72d57537de
commit
ffecbc7004
|
@ -120,7 +120,7 @@
|
||||||
"sqlite3": "^5.0.1",
|
"sqlite3": "^5.0.1",
|
||||||
"sse-channel": "^3.1.1",
|
"sse-channel": "^3.1.1",
|
||||||
"tslib": "1.14.1",
|
"tslib": "1.14.1",
|
||||||
"typeorm": "0.2.34",
|
"typeorm": "^0.2.30",
|
||||||
"winston": "^3.3.3"
|
"winston": "^3.3.3"
|
||||||
},
|
},
|
||||||
"jest": {
|
"jest": {
|
||||||
|
|
|
@ -466,16 +466,18 @@ class App {
|
||||||
// Does very basic health check
|
// Does very basic health check
|
||||||
this.app.get('/healthz', async (req: express.Request, res: express.Response) => {
|
this.app.get('/healthz', async (req: express.Request, res: express.Response) => {
|
||||||
|
|
||||||
const connectionManager = getConnectionManager();
|
const connection = getConnectionManager().get();
|
||||||
|
|
||||||
if (connectionManager.connections.length === 0) {
|
try {
|
||||||
const error = new ResponseHelper.ResponseError('No Database connection found!', undefined, 503);
|
if (connection.isConnected === false) {
|
||||||
return ResponseHelper.sendErrorResponse(res, error);
|
// Connection is not active
|
||||||
}
|
throw new Error('No active database connection!');
|
||||||
|
}
|
||||||
if (connectionManager.connections[0].isConnected === false) {
|
// DB ping
|
||||||
// Connection is not active
|
await connection.query('SELECT 1');
|
||||||
const error = new ResponseHelper.ResponseError('Database connection not active!', undefined, 503);
|
} catch (err) {
|
||||||
|
LoggerProxy.error('No Database connection!', err);
|
||||||
|
const error = new ResponseHelper.ResponseError('No Database connection!', undefined, 503);
|
||||||
return ResponseHelper.sendErrorResponse(res, error);
|
return ResponseHelper.sendErrorResponse(res, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,7 +111,7 @@ export function registerProductionWebhooks() {
|
||||||
}
|
}
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
|
|
||||||
app: express.Application;
|
app: express.Application;
|
||||||
activeWorkflowRunner: ActiveWorkflowRunner.ActiveWorkflowRunner;
|
activeWorkflowRunner: ActiveWorkflowRunner.ActiveWorkflowRunner;
|
||||||
endpointWebhook: string;
|
endpointWebhook: string;
|
||||||
|
@ -129,12 +129,12 @@ class App {
|
||||||
protocol: string;
|
protocol: string;
|
||||||
sslKey: string;
|
sslKey: string;
|
||||||
sslCert: string;
|
sslCert: string;
|
||||||
|
|
||||||
presetCredentialsLoaded: boolean;
|
presetCredentialsLoaded: boolean;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.app = express();
|
this.app = express();
|
||||||
|
|
||||||
this.endpointWebhook = config.get('endpoints.webhook') as string;
|
this.endpointWebhook = config.get('endpoints.webhook') as string;
|
||||||
this.saveDataErrorExecution = config.get('executions.saveDataOnError') as string;
|
this.saveDataErrorExecution = config.get('executions.saveDataOnError') as string;
|
||||||
this.saveDataSuccessExecution = config.get('executions.saveDataOnSuccess') as string;
|
this.saveDataSuccessExecution = config.get('executions.saveDataOnSuccess') as string;
|
||||||
|
@ -143,22 +143,22 @@ class App {
|
||||||
this.maxExecutionTimeout = config.get('executions.maxTimeout') as number;
|
this.maxExecutionTimeout = config.get('executions.maxTimeout') as number;
|
||||||
this.timezone = config.get('generic.timezone') as string;
|
this.timezone = config.get('generic.timezone') as string;
|
||||||
this.restEndpoint = config.get('endpoints.rest') as string;
|
this.restEndpoint = config.get('endpoints.rest') as string;
|
||||||
|
|
||||||
this.activeWorkflowRunner = ActiveWorkflowRunner.getInstance();
|
this.activeWorkflowRunner = ActiveWorkflowRunner.getInstance();
|
||||||
|
|
||||||
this.activeExecutionsInstance = ActiveExecutions.getInstance();
|
this.activeExecutionsInstance = ActiveExecutions.getInstance();
|
||||||
|
|
||||||
this.protocol = config.get('protocol');
|
this.protocol = config.get('protocol');
|
||||||
this.sslKey = config.get('ssl_key');
|
this.sslKey = config.get('ssl_key');
|
||||||
this.sslCert = config.get('ssl_cert');
|
this.sslCert = config.get('ssl_cert');
|
||||||
|
|
||||||
this.externalHooks = ExternalHooks();
|
this.externalHooks = ExternalHooks();
|
||||||
|
|
||||||
this.presetCredentialsLoaded = false;
|
this.presetCredentialsLoaded = false;
|
||||||
this.endpointPresetCredentials = config.get('credentials.overwrite.endpoint') as string;
|
this.endpointPresetCredentials = config.get('credentials.overwrite.endpoint') as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current epoch time
|
* Returns the current epoch time
|
||||||
*
|
*
|
||||||
|
@ -168,15 +168,15 @@ class App {
|
||||||
getCurrentDate(): Date {
|
getCurrentDate(): Date {
|
||||||
return new Date();
|
return new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async config(): Promise<void> {
|
async config(): Promise<void> {
|
||||||
|
|
||||||
this.versions = await GenericHelpers.getVersions();
|
this.versions = await GenericHelpers.getVersions();
|
||||||
|
|
||||||
// Compress the response data
|
// Compress the response data
|
||||||
this.app.use(compression());
|
this.app.use(compression());
|
||||||
|
|
||||||
// Make sure that each request has the "parsedUrl" parameter
|
// Make sure that each request has the "parsedUrl" parameter
|
||||||
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||||
(req as ICustomRequest).parsedUrl = parseUrl(req);
|
(req as ICustomRequest).parsedUrl = parseUrl(req);
|
||||||
|
@ -184,7 +184,7 @@ class App {
|
||||||
req.rawBody = Buffer.from('', 'base64');
|
req.rawBody = Buffer.from('', 'base64');
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Support application/json type post data
|
// Support application/json type post data
|
||||||
this.app.use(bodyParser.json({
|
this.app.use(bodyParser.json({
|
||||||
limit: '16mb', verify: (req, res, buf) => {
|
limit: '16mb', verify: (req, res, buf) => {
|
||||||
|
@ -192,7 +192,7 @@ class App {
|
||||||
req.rawBody = buf;
|
req.rawBody = buf;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Support application/xml type post data
|
// Support application/xml type post data
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
this.app.use(bodyParser.xml({
|
this.app.use(bodyParser.xml({
|
||||||
|
@ -202,14 +202,14 @@ class App {
|
||||||
explicitArray: false, // Only put properties in array if length > 1
|
explicitArray: false, // Only put properties in array if length > 1
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
this.app.use(bodyParser.text({
|
this.app.use(bodyParser.text({
|
||||||
limit: '16mb', verify: (req, res, buf) => {
|
limit: '16mb', verify: (req, res, buf) => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req.rawBody = buf;
|
req.rawBody = buf;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
//support application/x-www-form-urlencoded post data
|
//support application/x-www-form-urlencoded post data
|
||||||
this.app.use(bodyParser.urlencoded({ extended: false,
|
this.app.use(bodyParser.urlencoded({ extended: false,
|
||||||
verify: (req, res, buf) => {
|
verify: (req, res, buf) => {
|
||||||
|
@ -217,7 +217,7 @@ class App {
|
||||||
req.rawBody = buf;
|
req.rawBody = buf;
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if (process.env['NODE_ENV'] !== 'production') {
|
if (process.env['NODE_ENV'] !== 'production') {
|
||||||
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||||
// Allow access also from frontend when developing
|
// Allow access also from frontend when developing
|
||||||
|
@ -227,64 +227,65 @@ class App {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
this.app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||||
if (Db.collections.Workflow === null) {
|
if (Db.collections.Workflow === null) {
|
||||||
const error = new ResponseHelper.ResponseError('Database is not ready!', undefined, 503);
|
const error = new ResponseHelper.ResponseError('Database is not ready!', undefined, 503);
|
||||||
return ResponseHelper.sendErrorResponse(res, error);
|
return ResponseHelper.sendErrorResponse(res, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
// Healthcheck
|
// Healthcheck
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
|
|
||||||
|
|
||||||
// Does very basic health check
|
// Does very basic health check
|
||||||
this.app.get('/healthz', async (req: express.Request, res: express.Response) => {
|
this.app.get('/healthz', async (req: express.Request, res: express.Response) => {
|
||||||
|
|
||||||
const connectionManager = getConnectionManager();
|
const connection = getConnectionManager().get();
|
||||||
|
|
||||||
if (connectionManager.connections.length === 0) {
|
try {
|
||||||
const error = new ResponseHelper.ResponseError('No Database connection found!', undefined, 503);
|
if (connection.isConnected === false) {
|
||||||
|
// Connection is not active
|
||||||
|
throw new Error('No active database connection!');
|
||||||
|
}
|
||||||
|
// DB ping
|
||||||
|
await connection.query('SELECT 1');
|
||||||
|
} catch (err) {
|
||||||
|
const error = new ResponseHelper.ResponseError('No Database connection!', undefined, 503);
|
||||||
return ResponseHelper.sendErrorResponse(res, error);
|
return ResponseHelper.sendErrorResponse(res, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connectionManager.connections[0].isConnected === false) {
|
|
||||||
// Connection is not active
|
|
||||||
const error = new ResponseHelper.ResponseError('Database connection not active!', undefined, 503);
|
|
||||||
return ResponseHelper.sendErrorResponse(res, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Everything fine
|
// Everything fine
|
||||||
const responseData = {
|
const responseData = {
|
||||||
status: 'ok',
|
status: 'ok',
|
||||||
};
|
};
|
||||||
|
|
||||||
ResponseHelper.sendSuccessResponse(res, responseData, true, 200);
|
ResponseHelper.sendSuccessResponse(res, responseData, true, 200);
|
||||||
});
|
});
|
||||||
|
|
||||||
registerProductionWebhooks.apply(this);
|
registerProductionWebhooks.apply(this);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function start(): Promise<void> {
|
export async function start(): Promise<void> {
|
||||||
const PORT = config.get('port');
|
const PORT = config.get('port');
|
||||||
const ADDRESS = config.get('listen_address');
|
const ADDRESS = config.get('listen_address');
|
||||||
|
|
||||||
const app = new App();
|
const app = new App();
|
||||||
|
|
||||||
await app.config();
|
await app.config();
|
||||||
|
|
||||||
let server;
|
let server;
|
||||||
|
|
||||||
if (app.protocol === 'https' && app.sslKey && app.sslCert) {
|
if (app.protocol === 'https' && app.sslKey && app.sslCert) {
|
||||||
const https = require('https');
|
const https = require('https');
|
||||||
const privateKey = readFileSync(app.sslKey, 'utf8');
|
const privateKey = readFileSync(app.sslKey, 'utf8');
|
||||||
|
@ -295,12 +296,12 @@ export async function start(): Promise<void> {
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
server = http.createServer(app.app);
|
server = http.createServer(app.app);
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen(PORT, ADDRESS, async () => {
|
server.listen(PORT, ADDRESS, async () => {
|
||||||
const versions = await GenericHelpers.getVersions();
|
const versions = await GenericHelpers.getVersions();
|
||||||
console.log(`n8n ready on ${ADDRESS}, port ${PORT}`);
|
console.log(`n8n ready on ${ADDRESS}, port ${PORT}`);
|
||||||
console.log(`Version: ${versions.cli}`);
|
console.log(`Version: ${versions.cli}`);
|
||||||
|
|
||||||
await app.externalHooks.run('n8n.ready', [app]);
|
await app.externalHooks.run('n8n.ready', [app]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue