Skip to content

Commit

Permalink
fix(core): Fix $getWorkflowStaticData on task runners (#12153)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com>
  • Loading branch information
ivov and tomi authored Dec 11, 2024
1 parent dce0c58 commit b479f14
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
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 @@ -244,6 +245,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 @@ -138,6 +138,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 @@ -146,6 +148,7 @@ export class JsTaskRunner extends TaskRunner {
return {
result,
customData: data.runExecutionData.resultData.metadata,
staticData: workflow.staticData.__dataChanged ? workflow.staticData : undefined,
};
}

Expand Down Expand Up @@ -200,7 +203,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 @@ -273,7 +276,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

0 comments on commit b479f14

Please # to comment.