Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor(core): Include execution progress in save settings (no-changelog) #7769

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,9 @@ export function hookFunctionsPreExecute(parentProcessMode?: string): IWorkflowEx
data: ITaskData,
executionData: IRunExecutionData,
): Promise<void> {
const saveExecutionProgress = config.getEnv('executions.saveExecutionProgress');
const workflowSettings = this.workflowData.settings;
if (workflowSettings !== undefined) {
if (workflowSettings.saveExecutionProgress === false) {
return;
}
if (workflowSettings.saveExecutionProgress !== true && !saveExecutionProgress) {
return;
}
} else if (!saveExecutionProgress) {
return;
}
const saveSettings = toSaveSettings(this.workflowData.settings);

if (!saveSettings.progress) return;

try {
logger.debug(
Expand Down
18 changes: 15 additions & 3 deletions packages/cli/src/executionLifecycleHooks/toSaveSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import config from '@/config';
import type { IWorkflowSettings } from 'n8n-workflow';

/**
* Return whether a workflow execution is configured to be saved or not,
* for error executions, success executions, and manual executions.
* Return whether a workflow execution is configured to be saved or not:
*
* - `error`: Whether to save failed executions in production.
* - `success`: Whether to successful executions in production.
* - `manual`: Whether to save successful or failed manual executions.
* - `progress`: Whether to save execution progress, i.e. after each node's execution.
*/
export function toSaveSettings(workflowSettings: IWorkflowSettings = {}) {
const DEFAULTS = {
ERROR: config.getEnv('executions.saveDataOnError'),
SUCCESS: config.getEnv('executions.saveDataOnSuccess'),
MANUAL: config.getEnv('executions.saveDataManualExecutions'),
PROGRESS: config.getEnv('executions.saveExecutionProgress'),
};

return {
Expand All @@ -19,6 +24,13 @@ export function toSaveSettings(workflowSettings: IWorkflowSettings = {}) {
success: workflowSettings.saveDataSuccessExecution
? workflowSettings.saveDataSuccessExecution !== 'none'
: DEFAULTS.SUCCESS !== 'none',
manual: workflowSettings?.saveManualExecutions ?? DEFAULTS.MANUAL,
manual:
workflowSettings === undefined || workflowSettings.saveManualExecutions === 'DEFAULT'
? DEFAULTS.MANUAL
: workflowSettings.saveManualExecutions ?? DEFAULTS.MANUAL,
progress:
workflowSettings === undefined || workflowSettings.saveExecutionProgress === 'DEFAULT'
? DEFAULTS.PROGRESS
: workflowSettings.saveExecutionProgress ?? DEFAULTS.PROGRESS,
};
}
154 changes: 154 additions & 0 deletions packages/cli/test/unit/execution-lifecyle/toSaveSettings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import config from '@/config';
import { toSaveSettings } from '@/executionLifecycleHooks/toSaveSettings';

afterEach(() => {
config.load(config.default);
});

describe('failed production executions', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnError', 'none');

const saveSettings = toSaveSettings({ saveDataErrorExecution: 'all' });

expect(saveSettings.error).toBe(true);

config.set('executions.saveDataOnError', 'all');

const _saveSettings = toSaveSettings({ saveDataErrorExecution: 'none' });

expect(_saveSettings.error).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataOnError', 'all');

const saveSettings = toSaveSettings();

expect(saveSettings.error).toBe(true);

config.set('executions.saveDataOnError', 'none');

const _saveSettings = toSaveSettings();

expect(_saveSettings.error).toBe(false);
});
});

describe('sucessful production executions', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnSuccess', 'none');

const saveSettings = toSaveSettings({ saveDataSuccessExecution: 'all' });

expect(saveSettings.success).toBe(true);

config.set('executions.saveDataOnSuccess', 'all');

const _saveSettings = toSaveSettings({ saveDataSuccessExecution: 'none' });

expect(_saveSettings.success).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataOnSuccess', 'all');

const saveSettings = toSaveSettings();

expect(saveSettings.success).toBe(true);

config.set('executions.saveDataOnSuccess', 'none');

const _saveSettings = toSaveSettings();

expect(_saveSettings.success).toBe(false);
});
});

describe('manual executions', () => {
it('should favor workflow setting over default', () => {
config.set('executions.saveDataManualExecutions', false);

const saveSettings = toSaveSettings({ saveManualExecutions: true });

expect(saveSettings.manual).toBe(true);

config.set('executions.saveDataManualExecutions', true);

const _saveSettings = toSaveSettings({ saveManualExecutions: false });

expect(_saveSettings.manual).toBe(false);
});

it('should favor fall back to default if workflow setting is explicit default', () => {
config.set('executions.saveDataManualExecutions', true);

const saveSettings = toSaveSettings({ saveManualExecutions: 'DEFAULT' });

expect(saveSettings.manual).toBe(true);

config.set('executions.saveDataManualExecutions', false);

const _saveSettings = toSaveSettings({ saveManualExecutions: 'DEFAULT' });

expect(_saveSettings.manual).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataManualExecutions', true);

const saveSettings = toSaveSettings();

expect(saveSettings.manual).toBe(true);

config.set('executions.saveDataManualExecutions', false);

const _saveSettings = toSaveSettings();

expect(_saveSettings.manual).toBe(false);
});
});

describe('execution progress', () => {
it('should favor workflow setting over default', () => {
config.set('executions.saveExecutionProgress', false);

const saveSettings = toSaveSettings({ saveExecutionProgress: true });

expect(saveSettings.progress).toBe(true);

config.set('executions.saveExecutionProgress', true);

const _saveSettings = toSaveSettings({ saveExecutionProgress: false });

expect(_saveSettings.progress).toBe(false);
});

it('should favor fall back to default if workflow setting is explicit default', () => {
config.set('executions.saveExecutionProgress', true);

const saveSettings = toSaveSettings({ saveExecutionProgress: 'DEFAULT' });

expect(saveSettings.progress).toBe(true);

config.set('executions.saveExecutionProgress', false);

const _saveSettings = toSaveSettings({ saveExecutionProgress: 'DEFAULT' });

expect(_saveSettings.progress).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveExecutionProgress', true);

const saveSettings = toSaveSettings();

expect(saveSettings.progress).toBe(true);

config.set('executions.saveExecutionProgress', false);

const _saveSettings = toSaveSettings();

expect(_saveSettings.progress).toBe(false);
});
});
99 changes: 1 addition & 98 deletions packages/cli/test/unit/execution.lifecycle.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { restoreBinaryDataId } from '@/executionLifecycleHooks/restoreBinaryDataId';
import { BinaryDataService } from 'n8n-core';
import { mockInstance } from '../shared/mocking';
import { toSaveSettings } from '@/executionLifecycleHooks/toSaveSettings';

import type { IRun } from 'n8n-workflow';
import config from '@/config';

Expand Down Expand Up @@ -140,100 +140,3 @@ for (const mode of ['filesystem-v2', 's3'] as const) {
expect(binaryDataService.rename).not.toHaveBeenCalled();
});
}

describe('toSaveSettings()', () => {
afterEach(() => {
jest.restoreAllMocks();
config.load(config.default);
});

describe('when setting `error`', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnError', 'none');

const saveSettings = toSaveSettings({ saveDataErrorExecution: 'all' });

expect(saveSettings.error).toBe(true);

config.set('executions.saveDataOnError', 'all');

const _saveSettings = toSaveSettings({ saveDataErrorExecution: 'none' });

expect(_saveSettings.error).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataOnError', 'all');

const saveSettings = toSaveSettings();

expect(saveSettings.error).toBe(true);

config.set('executions.saveDataOnError', 'none');

const _saveSettings = toSaveSettings();

expect(_saveSettings.error).toBe(false);
});
});

describe('when setting `success`', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnSuccess', 'none');

const saveSettings = toSaveSettings({ saveDataSuccessExecution: 'all' });

expect(saveSettings.success).toBe(true);

config.set('executions.saveDataOnSuccess', 'all');

const _saveSettings = toSaveSettings({ saveDataSuccessExecution: 'none' });

expect(_saveSettings.success).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataOnSuccess', 'all');

const saveSettings = toSaveSettings();

expect(saveSettings.success).toBe(true);

config.set('executions.saveDataOnSuccess', 'none');

const _saveSettings = toSaveSettings();

expect(_saveSettings.success).toBe(false);
});
});

describe('when setting `manual`', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataManualExecutions', false);

const saveSettings = toSaveSettings({ saveManualExecutions: true });

expect(saveSettings.manual).toBe(true);

config.set('executions.saveDataManualExecutions', true);

const _saveSettings = toSaveSettings({ saveManualExecutions: false });

expect(_saveSettings.manual).toBe(false);
});

it('should fall back to default if no workflow setting', () => {
config.set('executions.saveDataManualExecutions', true);

const saveSettings = toSaveSettings();

expect(saveSettings.manual).toBe(true);

config.set('executions.saveDataManualExecutions', false);

const _saveSettings = toSaveSettings();

expect(_saveSettings.manual).toBe(false);
});
});
});