Skip to content

Commit

Permalink
refactor(core): Show deprecation warning if task runners are disabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov authored Feb 13, 2025
1 parent 0c6eb6e commit 4f8dd3d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
37 changes: 29 additions & 8 deletions packages/cli/src/deprecation/__tests__/deprecation.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { mockLogger } from '@test/mocking';
import { DeprecationService } from '../deprecation.service';

describe('DeprecationService', () => {
const toTest = (envVar: string, value: string, inUse: boolean) => {
const toTest = (envVar: string, value: string, mustWarn: boolean) => {
process.env[envVar] = value;
const deprecationService = new DeprecationService(mockLogger());

deprecationService.warn();

expect(deprecationService.isInUse(envVar)).toBe(inUse);
expect(deprecationService.mustWarn(envVar)).toBe(mustWarn);
};

test.each([
Expand All @@ -18,24 +18,45 @@ describe('DeprecationService', () => {
['EXECUTIONS_DATA_PRUNE_TIMEOUT', '1', true],
['N8N_CONFIG_FILES', '1', true],
['N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN', '1', true],
])('should detect when %s is in use', (envVar, value, inUse) => {
toTest(envVar, value, inUse);
])('should detect when %s is in use', (envVar, value, mustWarn) => {
toTest(envVar, value, mustWarn);
});

test.each([
['default', true],
['filesystem', false],
['s3', false],
])('should handle N8N_BINARY_DATA_MODE as %s', (mode, inUse) => {
toTest('N8N_BINARY_DATA_MODE', mode, inUse);
])('should handle N8N_BINARY_DATA_MODE as %s', (mode, mustWarn) => {
toTest('N8N_BINARY_DATA_MODE', mode, mustWarn);
});

test.each([
['sqlite', false],
['postgresdb', false],
['mysqldb', true],
['mariadb', true],
])('should handle DB_TYPE as %s', (dbType, inUse) => {
toTest('DB_TYPE', dbType, inUse);
])('should handle DB_TYPE as %s', (dbType, mustWarn) => {
toTest('DB_TYPE', dbType, mustWarn);
});

describe('N8N_RUNNERS_ENABLED', () => {
const envVar = 'N8N_RUNNERS_ENABLED';

test.each([
['false', true],
['', true],
['true', false],
[undefined /* warnIfMissing */, true],
])('should handle value: %s', (value, mustWarn) => {
if (value === undefined) {
delete process.env[envVar];
} else {
process.env[envVar] = value;
}

const deprecationService = new DeprecationService(mockLogger());
deprecationService.warn();
expect(deprecationService.mustWarn(envVar)).toBe(mustWarn);
});
});
});
42 changes: 26 additions & 16 deletions packages/cli/src/deprecation/deprecation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ type Deprecation = {
message: string;

/** Function to identify the specific value in the env var that is deprecated. */
checkValue?: (value: string) => boolean;
checkValue?: (value?: string) => boolean;

/** Whether to show a deprecation warning if the env var is missing. */
warnIfMissing?: boolean;
};

const SAFE_TO_REMOVE = 'Remove this environment variable; it is no longer needed.';

/** Responsible for warning about use of deprecated env vars. */
/** Responsible for warning about deprecations related to env vars. */
@Service()
export class DeprecationService {
private readonly deprecations: Deprecation[] = [
Expand All @@ -39,25 +42,32 @@ export class DeprecationService {
envVar: 'N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN',
message: `n8n no longer deregisters webhooks at startup and shutdown. ${SAFE_TO_REMOVE}`,
},
{
envVar: 'N8N_RUNNERS_ENABLED',
message:
'Running n8n without task runners is deprecated. Task runners will be turned on by default in a future version. Please set `N8N_RUNNERS_ENABLED=true` to enable task runners now and avoid potential issues in the future. Learn more: https://docs.n8n.io/hosting/configuration/task-runners/',
checkValue: (value?: string) => value?.toLowerCase() !== 'true' && value !== '1',
warnIfMissing: true,
},
];

/** Runtime state of deprecated env vars. */
private readonly state: Record<EnvVarName, { inUse: boolean }> = {};
/** Runtime state of deprecation-related env vars. */
private readonly state: Record<EnvVarName, { mustWarn: boolean }> = {};

constructor(private readonly logger: Logger) {}

warn() {
this.deprecations.forEach((d) => {
const envValue = process.env[d.envVar];
this.state[d.envVar] = {
inUse: d.checkValue
? envValue !== undefined && d.checkValue(envValue)
: envValue !== undefined,
mustWarn:
(d.warnIfMissing !== undefined && envValue === undefined) ||
(d.checkValue ? d.checkValue(envValue) : envValue !== undefined),
};
});

const inUse = Object.entries(this.state)
.filter(([, d]) => d.inUse)
const mustWarn = Object.entries(this.state)
.filter(([, d]) => d.mustWarn)
.map(([envVar]) => {
const deprecation = this.deprecations.find((d) => d.envVar === envVar);
if (!deprecation) {
Expand All @@ -66,19 +76,19 @@ export class DeprecationService {
return deprecation;
});

if (inUse.length === 0) return;
if (mustWarn.length === 0) return;

const header = `The following environment variable${
inUse.length === 1 ? ' is' : 's are'
} deprecated and will be removed in an upcoming version of n8n. Please take the recommended actions to update your configuration`;
const deprecations = inUse
const header = `There ${
mustWarn.length === 1 ? 'is a deprecation' : 'are deprecations'
} related to your environment variables. Please take the recommended actions to update your configuration`;
const deprecations = mustWarn
.map(({ envVar, message }) => ` - ${envVar} -> ${message}\n`)
.join('');

this.logger.warn(`\n${header}:\n${deprecations}`);
}

isInUse(envVar: string) {
return this.state[envVar]?.inUse ?? false;
mustWarn(envVar: string) {
return this.state[envVar]?.mustWarn ?? false;
}
}

0 comments on commit 4f8dd3d

Please sign in to comment.