Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(core): Fix $getWorkflowStaticData on task runners #12153

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('JsTaskRunner', () => {
grantToken: 'grantToken',
maxConcurrency: 1,
taskBrokerUri: 'http://localhost',
taskTimeout: 60,
...baseRunnerOpts,
},
jsRunnerConfig: {
Expand Down Expand Up @@ -214,6 +215,7 @@ describe('JsTaskRunner', () => {
['$runIndex', 0],
['{ wf: $workflow }', { wf: { active: true, id: '1', name: 'Test Workflow' } }],
['$vars', { var: 'value' }],
['$getWorkflowStaticData("global")', {}],
],
'Node.js internal functions': [
['typeof Function', 'function'],
Expand Down
10 changes: 7 additions & 3 deletions packages/@n8n/task-runner/src/js-task-runner/js-task-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getAdditionalKeys } from 'n8n-core';
import { WorkflowDataProxy, Workflow } from 'n8n-workflow';
import { WorkflowDataProxy, Workflow, ObservableObject } from 'n8n-workflow';
import type {
CodeExecutionMode,
IWorkflowExecuteAdditionalData,
Expand Down Expand Up @@ -132,6 +132,8 @@ export class JsTaskRunner extends TaskRunner {
},
};

workflow.staticData = ObservableObject.create(workflow.staticData);

const result =
settings.nodeMode === 'runOnceForAllItems'
? await this.runForAllItems(task.taskId, settings, data, workflow, customConsole, signal)
Expand All @@ -140,6 +142,7 @@ export class JsTaskRunner extends TaskRunner {
return {
result,
customData: data.runExecutionData.resultData.metadata,
staticData: workflow.staticData.__dataChanged ? workflow.staticData : undefined,
};
}

Expand Down Expand Up @@ -194,7 +197,7 @@ export class JsTaskRunner extends TaskRunner {
module: {},
console: customConsole,
items: inputItems,

$getWorkflowStaticData: (type: 'global' | 'node') => workflow.getStaticData(type, data.node),
...this.getNativeVariables(),
...dataProxy,
...this.buildRpcCallObject(taskId),
Expand Down Expand Up @@ -267,7 +270,8 @@ export class JsTaskRunner extends TaskRunner {
module: {},
console: customConsole,
item,

$getWorkflowStaticData: (type: 'global' | 'node') =>
workflow.getStaticData(type, data.node),
...this.getNativeVariables(),
...dataProxy,
...this.buildRpcCallObject(taskId),
Expand Down
1 change: 1 addition & 0 deletions packages/@n8n/task-runner/src/runner-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface DataRequestResponse {
export interface TaskResultData {
result: INodeExecutionData[];
customData?: Record<string, string>;
staticData?: IDataObject;
}

export interface TaskData {
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/runners/task-managers/task-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TaskResultData, RequesterMessage, BrokerMessage, TaskData } from '@n8n/task-runner';
import { RPC_ALLOW_LIST } from '@n8n/task-runner';
import { createResultOk, createResultError } from 'n8n-workflow';
import type {
EnvProviderState,
IExecuteFunctions,
Expand All @@ -15,7 +16,6 @@ import type {
IWorkflowExecuteAdditionalData,
Result,
} from 'n8n-workflow';
import { createResultOk, createResultError } from 'n8n-workflow';
import { nanoid } from 'nanoid';
import { Service } from 'typedi';

Expand Down Expand Up @@ -158,6 +158,11 @@ export abstract class TaskManager {
});
}

const { staticData: incomingStaticData } = resultData;

// if the runner sent back static data, then it changed, so update it
if (incomingStaticData) workflow.overrideStaticData(incomingStaticData);

return createResultOk(resultData.result as TData);
} catch (e: unknown) {
return createResultError(e as TError);
Expand Down
7 changes: 7 additions & 0 deletions packages/workflow/src/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ export class Workflow {
this.expression = new Expression(this);
}

overrideStaticData(staticData?: IDataObject) {
this.staticData = ObservableObject.create(staticData || {}, undefined, {
ignoreEmptyOnFirstChild: true,
});
this.staticData.__dataChanged = true;
}

/**
* The default connections are by source node. This function rewrites them by destination nodes
* to easily find parent nodes.
Expand Down
Loading