fix: Report app startup and DB migration errors to Sentry (#5127)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-01-11 18:28:35 +01:00
committed by GitHub
parent 3c109ffab1
commit a573db2ef7
4 changed files with 74 additions and 79 deletions

View File

@@ -1,9 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/await-thenable */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import path from 'path';
@@ -17,7 +13,7 @@ import replaceStream from 'replacestream';
import { promisify } from 'util';
import glob from 'fast-glob';
import { LoggerProxy, sleep } from 'n8n-workflow';
import { LoggerProxy, ErrorReporterProxy as ErrorReporter, sleep } from 'n8n-workflow';
import { createHash } from 'crypto';
import config from '@/config';
@@ -49,7 +45,20 @@ const open = require('open');
const pipeline = promisify(stream.pipeline);
let activeWorkflowRunner: ActiveWorkflowRunner.ActiveWorkflowRunner | undefined;
let processExitCode = 0;
const exitWithCrash = async (message: string, error: unknown) => {
ErrorReporter.error(new Error(message, { cause: error }), { level: 'fatal' });
await sleep(2000);
process.exit(1);
};
const exitSuccessFully = async () => {
try {
await CrashJournal.cleanup();
} finally {
process.exit();
}
};
export class Start extends Command {
static description = 'Starts n8n. Makes Web-UI available and starts active workflows';
@@ -101,12 +110,6 @@ export class Start extends Command {
static async stopProcess() {
getLogger().info('\nStopping n8n...');
const exit = () => {
CrashJournal.cleanup().finally(() => {
process.exit(processExitCode);
});
};
try {
// Stop with trying to activate workflows that could not be activated
activeWorkflowRunner?.removeAllQueuedWorkflowActivations();
@@ -114,11 +117,11 @@ export class Start extends Command {
const externalHooks = ExternalHooks();
await externalHooks.run('n8n.stop', []);
setTimeout(() => {
setTimeout(async () => {
// In case that something goes wrong with shutdown we
// kill after max. 30 seconds no matter what
console.log('process exited after 30s');
exit();
await exitSuccessFully();
}, 30000);
await InternalHooksManager.getInstance().onN8nStop();
@@ -159,10 +162,10 @@ export class Start extends Command {
//finally shut down Event Bus
await eventBus.close();
} catch (error) {
console.error('There was an error shutting down n8n.', error);
await exitWithCrash('There was an error shutting down n8n.', error);
}
exit();
await exitSuccessFully();
}
static async generateStaticAssets() {
@@ -236,12 +239,9 @@ export class Start extends Command {
try {
// Start directly with the init of the database to improve startup time
const startDbInitPromise = Db.init().catch((error: Error) => {
logger.error(`There was an error initializing DB: "${error.message}"`);
processExitCode = 1;
process.emit('exit', processExitCode);
});
const startDbInitPromise = Db.init().catch(async (error: Error) =>
exitWithCrash('There was an error initializing DB', error),
);
// Make sure the settings exist
const userSettings = await UserSettings.prepareUserSettings();
@@ -456,9 +456,7 @@ export class Start extends Command {
});
}
} catch (error) {
console.error('There was an error', error);
processExitCode = 1;
process.emit('exit', processExitCode);
await exitWithCrash('There was an error', error);
}
}
}