2023-02-10 05:59:20 -08:00
|
|
|
import { Command } from '@oclif/command';
|
|
|
|
import type { INodeTypes } from 'n8n-workflow';
|
|
|
|
import { LoggerProxy, ErrorReporterProxy as ErrorReporter, sleep } from 'n8n-workflow';
|
|
|
|
import type { IUserSettings } from 'n8n-core';
|
|
|
|
import { BinaryDataManager, UserSettings } from 'n8n-core';
|
2023-01-27 05:56:56 -08:00
|
|
|
import { getLogger } from '@/Logger';
|
2023-02-10 05:59:20 -08:00
|
|
|
import config from '@/config';
|
2022-11-09 06:25:00 -08:00
|
|
|
import * as Db from '@/Db';
|
2023-02-10 05:59:20 -08:00
|
|
|
import * as CrashJournal from '@/CrashJournal';
|
2022-12-16 06:27:49 -08:00
|
|
|
import { inTest } from '@/constants';
|
2023-02-10 05:59:20 -08:00
|
|
|
import { CredentialTypes } from '@/CredentialTypes';
|
|
|
|
import { CredentialsOverwrites } from '@/CredentialsOverwrites';
|
|
|
|
import { InternalHooksManager } from '@/InternalHooksManager';
|
|
|
|
import { initErrorHandling } from '@/ErrorReporting';
|
|
|
|
import { ExternalHooks } from '@/ExternalHooks';
|
|
|
|
import { NodeTypes } from '@/NodeTypes';
|
|
|
|
import type { LoadNodesAndCredentialsClass } from '@/LoadNodesAndCredentials';
|
|
|
|
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
|
|
|
|
import type { IExternalHooksClass } from '@/Interfaces';
|
|
|
|
|
|
|
|
export const UM_FIX_INSTRUCTION =
|
|
|
|
'Please fix the database by running ./packages/cli/bin/n8n user-management:reset';
|
2022-06-25 21:03:46 -07:00
|
|
|
|
|
|
|
export abstract class BaseCommand extends Command {
|
2023-02-10 05:59:20 -08:00
|
|
|
protected logger = LoggerProxy.init(getLogger());
|
|
|
|
|
|
|
|
protected externalHooks: IExternalHooksClass;
|
|
|
|
|
|
|
|
protected loadNodesAndCredentials: LoadNodesAndCredentialsClass;
|
|
|
|
|
|
|
|
protected nodeTypes: INodeTypes;
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected userSettings: IUserSettings;
|
2022-06-25 21:03:46 -07:00
|
|
|
|
|
|
|
async init(): Promise<void> {
|
2023-02-10 05:59:20 -08:00
|
|
|
await initErrorHandling();
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
this.stopProcess = this.stopProcess.bind(this);
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
// Make sure the settings exist
|
|
|
|
this.userSettings = await UserSettings.prepareUserSettings();
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
this.loadNodesAndCredentials = LoadNodesAndCredentials();
|
|
|
|
await this.loadNodesAndCredentials.init();
|
|
|
|
this.nodeTypes = NodeTypes(this.loadNodesAndCredentials);
|
|
|
|
const credentialTypes = CredentialTypes(this.loadNodesAndCredentials);
|
|
|
|
CredentialsOverwrites(credentialTypes);
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
await InternalHooksManager.init(this.userSettings.instanceId ?? '', this.nodeTypes);
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
await Db.init().catch(async (error: Error) =>
|
|
|
|
this.exitWithCrash('There was an error initializing DB', error),
|
|
|
|
);
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected async stopProcess() {
|
|
|
|
// This needs to be overridden
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected async initCrashJournal() {
|
|
|
|
await CrashJournal.init();
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected async exitSuccessFully() {
|
|
|
|
try {
|
|
|
|
await CrashJournal.cleanup();
|
|
|
|
} finally {
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected async exitWithCrash(message: string, error: unknown) {
|
|
|
|
ErrorReporter.error(new Error(message, { cause: error }), { level: 'fatal' });
|
|
|
|
await sleep(2000);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected async initBinaryManager() {
|
|
|
|
const binaryDataConfig = config.getEnv('binaryDataManager');
|
|
|
|
await BinaryDataManager.init(binaryDataConfig, true);
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
protected async initExternalHooks() {
|
|
|
|
this.externalHooks = ExternalHooks();
|
|
|
|
await this.externalHooks.init();
|
|
|
|
}
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2023-02-10 05:59:20 -08:00
|
|
|
async finally(error: Error | undefined) {
|
|
|
|
if (inTest || this.id === 'start') return;
|
|
|
|
if (Db.isInitialized) await Db.connection.destroy();
|
|
|
|
this.exit(error ? 1 : 0);
|
2022-06-25 21:03:46 -07:00
|
|
|
}
|
|
|
|
}
|