fix: Enable crash journal only in production mode (no-changelog) (#4948)

* consolidate various `NODE_ENV` checks in the `cli` package

* enable crash journal only in production
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2022-12-16 15:27:49 +01:00
committed by GitHub
parent 2a7cb0192a
commit 323bd78067
7 changed files with 20 additions and 19 deletions

View File

@@ -3,6 +3,7 @@ import { mkdir, utimes, open, rm } from 'fs/promises';
import { join, dirname } from 'path'; import { join, dirname } from 'path';
import { UserSettings } from 'n8n-core'; import { UserSettings } from 'n8n-core';
import { LoggerProxy, sleep } from 'n8n-workflow'; import { LoggerProxy, sleep } from 'n8n-workflow';
import { inProduction } from '@/constants';
export const touchFile = async (filePath: string): Promise<void> => { export const touchFile = async (filePath: string): Promise<void> => {
await mkdir(dirname(filePath), { recursive: true }); await mkdir(dirname(filePath), { recursive: true });
@@ -18,6 +19,8 @@ export const touchFile = async (filePath: string): Promise<void> => {
const journalFile = join(UserSettings.getUserN8nFolderPath(), 'crash.journal'); const journalFile = join(UserSettings.getUserN8nFolderPath(), 'crash.journal');
export const init = async () => { export const init = async () => {
if (!inProduction) return;
if (existsSync(journalFile)) { if (existsSync(journalFile)) {
// Crash detected // Crash detected
LoggerProxy.error('Last session crashed'); LoggerProxy.error('Last session crashed');

View File

@@ -14,9 +14,8 @@ import type {
IExecutionFlattedDb, IExecutionFlattedDb,
IExecutionResponse, IExecutionResponse,
IWorkflowDb, IWorkflowDb,
} from './Interfaces'; } from '@/Interfaces';
import { inDevelopment } from '@/constants';
const inDevelopment = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
/** /**
* Special Error which allows to return also an error code and http status code * Special Error which allows to return also an error code and http status code

View File

@@ -3,6 +3,7 @@ import { LoggerProxy } from 'n8n-workflow';
import { getLogger, Logger } from '@/Logger'; import { getLogger, Logger } from '@/Logger';
import { User } from '@db/entities/User'; import { User } from '@db/entities/User';
import * as Db from '@/Db'; import * as Db from '@/Db';
import { inTest } from '@/constants';
export abstract class BaseCommand extends Command { export abstract class BaseCommand extends Command {
logger: Logger; logger: Logger;
@@ -19,7 +20,7 @@ export abstract class BaseCommand extends Command {
} }
async finally(): Promise<void> { async finally(): Promise<void> {
if (process.env.NODE_ENV === 'test') return; if (inTest) return;
this.exit(); this.exit();
} }

View File

@@ -7,8 +7,7 @@ import { tmpdir } from 'os';
import { mkdtempSync } from 'fs'; import { mkdtempSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import { schema } from './schema'; import { schema } from './schema';
import { inTest, inE2ETests } from '@/constants';
const inE2ETests = process.env.E2E_TESTS === 'true';
if (inE2ETests) { if (inE2ETests) {
// Skip loading config from env variables in end-to-end tests // Skip loading config from env variables in end-to-end tests
@@ -36,10 +35,10 @@ config.getEnv = config.get;
if (!inE2ETests) { if (!inE2ETests) {
// Overwrite default configuration with settings which got defined in // Overwrite default configuration with settings which got defined in
// optional configuration files // optional configuration files
const { N8N_CONFIG_FILES, NODE_ENV } = process.env; const { N8N_CONFIG_FILES } = process.env;
if (N8N_CONFIG_FILES !== undefined) { if (N8N_CONFIG_FILES !== undefined) {
const configFiles = N8N_CONFIG_FILES.split(','); const configFiles = N8N_CONFIG_FILES.split(',');
if (NODE_ENV !== 'test') { if (!inTest) {
console.log(`\nLoading configuration overwrites from:\n - ${configFiles.join('\n - ')}\n`); console.log(`\nLoading configuration overwrites from:\n - ${configFiles.join('\n - ')}\n`);
} }

View File

@@ -4,6 +4,12 @@
import { resolve, join, dirname } from 'path'; import { resolve, join, dirname } from 'path';
import { RESPONSE_ERROR_MESSAGES as CORE_RESPONSE_ERROR_MESSAGES, UserSettings } from 'n8n-core'; import { RESPONSE_ERROR_MESSAGES as CORE_RESPONSE_ERROR_MESSAGES, UserSettings } from 'n8n-core';
const { NODE_ENV, E2E_TESTS } = process.env;
export const inProduction = NODE_ENV === 'production';
export const inDevelopment = !NODE_ENV || NODE_ENV === 'development';
export const inTest = NODE_ENV === 'test';
export const inE2ETests = E2E_TESTS === 'true';
export const CLI_DIR = resolve(__dirname, '..'); export const CLI_DIR = resolve(__dirname, '..');
export const TEMPLATES_DIR = join(CLI_DIR, 'templates'); export const TEMPLATES_DIR = join(CLI_DIR, 'templates');
export const NODES_BASE_DIR = join(CLI_DIR, '..', 'nodes-base'); export const NODES_BASE_DIR = join(CLI_DIR, '..', 'nodes-base');

View File

@@ -4,6 +4,7 @@ import { UserSettings } from 'n8n-core';
import type { QueryRunner } from 'typeorm/query-runner/QueryRunner'; import type { QueryRunner } from 'typeorm/query-runner/QueryRunner';
import config from '@/config'; import config from '@/config';
import { getLogger } from '@/Logger'; import { getLogger } from '@/Logger';
import { inTest } from '@/constants';
const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json'; const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json';
@@ -37,10 +38,7 @@ export function loadSurveyFromDisk(): string | null {
let logFinishTimeout: NodeJS.Timeout; let logFinishTimeout: NodeJS.Timeout;
export function logMigrationStart( export function logMigrationStart(migrationName: string, disableLogging = inTest): void {
migrationName: string,
disableLogging = process.env.NODE_ENV === 'test',
): void {
if (disableLogging) return; if (disableLogging) return;
if (!logFinishTimeout) { if (!logFinishTimeout) {
@@ -52,10 +50,7 @@ export function logMigrationStart(
clearTimeout(logFinishTimeout); clearTimeout(logFinishTimeout);
} }
export function logMigrationEnd( export function logMigrationEnd(migrationName: string, disableLogging = inTest): void {
migrationName: string,
disableLogging = process.env.NODE_ENV === 'test',
): void {
if (disableLogging) return; if (disableLogging) return;
getLogger().debug(`Finished migration ${migrationName}`); getLogger().debug(`Finished migration ${migrationName}`);

View File

@@ -1,8 +1,6 @@
import { inDevelopment } from '@/constants';
import type { RequestHandler } from 'express'; import type { RequestHandler } from 'express';
const { NODE_ENV } = process.env;
const inDevelopment = !NODE_ENV || NODE_ENV === 'development';
export const corsMiddleware: RequestHandler = (req, res, next) => { export const corsMiddleware: RequestHandler = (req, res, next) => {
if (inDevelopment && 'origin' in req.headers) { if (inDevelopment && 'origin' in req.headers) {
// Allow access also from frontend when developing // Allow access also from frontend when developing