diff --git a/packages/cli/src/ActiveWorkflowRunner.ts b/packages/cli/src/ActiveWorkflowRunner.ts index c40a103049..f664d7d423 100644 --- a/packages/cli/src/ActiveWorkflowRunner.ts +++ b/packages/cli/src/ActiveWorkflowRunner.ts @@ -45,7 +45,6 @@ import type { } from '@/Interfaces'; import * as ResponseHelper from '@/ResponseHelper'; import * as WebhookHelpers from '@/WebhookHelpers'; -import * as WorkflowHelpers from '@/WorkflowHelpers'; import * as WorkflowExecuteAdditionalData from '@/WorkflowExecuteAdditionalData'; import config from '@/config'; @@ -444,7 +443,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { } await this.webhookService.populateCache(); // Save static data! - await WorkflowHelpers.saveStaticData(workflow); + await WorkflowsService.saveStaticData(workflow); } /** @@ -483,7 +482,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { await workflow.deleteWebhook(webhookData, NodeExecuteFunctions, mode, 'update', false); } - await WorkflowHelpers.saveStaticData(workflow); + await WorkflowsService.saveStaticData(workflow); await this.webhookService.deleteWorkflowWebhooks(workflowId); } @@ -561,7 +560,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { donePromise?: IDeferredPromise, ): void => { Logger.debug(`Received event to trigger execution for workflow "${workflow.name}"`); - void WorkflowHelpers.saveStaticData(workflow); + void WorkflowsService.saveStaticData(workflow); const executePromise = this.runWorkflow( workflowData, node, @@ -617,7 +616,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { donePromise?: IDeferredPromise, ): void => { Logger.debug(`Received trigger for workflow "${workflow.name}"`); - void WorkflowHelpers.saveStaticData(workflow); + void WorkflowsService.saveStaticData(workflow); const executePromise = this.runWorkflow( workflowData, @@ -814,7 +813,7 @@ export class ActiveWorkflowRunner implements IWebhookManager { // If for example webhooks get created it sometimes has to save the // id of them in the static data. So make sure that data gets persisted. - await WorkflowHelpers.saveStaticData(workflowInstance!); + await WorkflowsService.saveStaticData(workflowInstance!); } /** diff --git a/packages/cli/src/WebhookHelpers.ts b/packages/cli/src/WebhookHelpers.ts index 91e8c21a05..c5c05f4215 100644 --- a/packages/cli/src/WebhookHelpers.ts +++ b/packages/cli/src/WebhookHelpers.ts @@ -62,6 +62,7 @@ import type { WorkflowEntity } from '@db/entities/WorkflowEntity'; import { EventsService } from '@/services/events.service'; import { OwnershipService } from './services/ownership.service'; import { parseBody } from './middlewares'; +import { WorkflowsService } from './workflows/workflows.services'; const pipeline = promisify(stream.pipeline); @@ -359,7 +360,7 @@ export async function executeWebhook( } // Save static data if it changed - await WorkflowHelpers.saveStaticData(workflow); + await WorkflowsService.saveStaticData(workflow); const additionalKeys: IWorkflowDataProxyAdditionalKeys = { $executionId: executionId, diff --git a/packages/cli/src/WorkflowExecuteAdditionalData.ts b/packages/cli/src/WorkflowExecuteAdditionalData.ts index f67db960b6..1161c8c0ed 100644 --- a/packages/cli/src/WorkflowExecuteAdditionalData.ts +++ b/packages/cli/src/WorkflowExecuteAdditionalData.ts @@ -530,7 +530,7 @@ function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks { if (!isManualMode && isWorkflowIdValid(this.workflowData.id) && newStaticData) { // Workflow is saved so update in database try { - await WorkflowHelpers.saveStaticDataById( + await WorkflowsService.saveStaticDataById( this.workflowData.id as string, newStaticData, ); @@ -722,7 +722,7 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks { if (isWorkflowIdValid(this.workflowData.id) && newStaticData) { // Workflow is saved so update in database try { - await WorkflowHelpers.saveStaticDataById( + await WorkflowsService.saveStaticDataById( this.workflowData.id as string, newStaticData, ); diff --git a/packages/cli/src/WorkflowHelpers.ts b/packages/cli/src/WorkflowHelpers.ts index c5f06c0be8..e1a6e7057f 100644 --- a/packages/cli/src/WorkflowHelpers.ts +++ b/packages/cli/src/WorkflowHelpers.ts @@ -33,7 +33,6 @@ import type { User } from '@db/entities/User'; import omit from 'lodash/omit'; // eslint-disable-next-line import/no-cycle import { PermissionChecker } from './UserManagement/PermissionChecker'; -import { isWorkflowIdValid } from './utils'; import { UserService } from './services/user.service'; import type { SharedWorkflow } from '@db/entities/SharedWorkflow'; import type { RoleNames } from '@db/entities/Role'; @@ -274,45 +273,6 @@ export async function executeErrorWorkflow( } } -/** - * Saves the static data if it changed - */ -export async function saveStaticData(workflow: Workflow): Promise { - if (workflow.staticData.__dataChanged === true) { - // Static data of workflow changed and so has to be saved - if (isWorkflowIdValid(workflow.id)) { - // Workflow is saved so update in database - try { - // eslint-disable-next-line @typescript-eslint/no-use-before-define - await saveStaticDataById(workflow.id!, workflow.staticData); - workflow.staticData.__dataChanged = false; - } catch (error) { - ErrorReporter.error(error); - Logger.error( - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - `There was a problem saving the workflow with id "${workflow.id}" to save changed staticData: "${error.message}"`, - { workflowId: workflow.id }, - ); - } - } - } -} - -/** - * Saves the given static data on workflow - * - * @param {(string)} workflowId The id of the workflow to save data on - * @param {IDataObject} newStaticData The static data to save - */ -export async function saveStaticDataById( - workflowId: string, - newStaticData: IDataObject, -): Promise { - await Db.collections.Workflow.update(workflowId, { - staticData: newStaticData, - }); -} - /** * Returns the static data of workflow */ diff --git a/packages/cli/src/workflows/workflows.services.ts b/packages/cli/src/workflows/workflows.services.ts index 60b8c4306a..e8f5663802 100644 --- a/packages/cli/src/workflows/workflows.services.ts +++ b/packages/cli/src/workflows/workflows.services.ts @@ -1,6 +1,11 @@ import { Container } from 'typedi'; -import type { INode, IPinData } from 'n8n-workflow'; -import { NodeApiError, LoggerProxy, Workflow } from 'n8n-workflow'; +import type { IDataObject, INode, IPinData } from 'n8n-workflow'; +import { + NodeApiError, + ErrorReporterProxy as ErrorReporter, + LoggerProxy, + Workflow, +} from 'n8n-workflow'; import type { FindManyOptions, FindOptionsSelect, FindOptionsWhere, UpdateResult } from 'typeorm'; import { In, Like } from 'typeorm'; import pick from 'lodash/pick'; @@ -27,7 +32,7 @@ import { InternalHooks } from '@/InternalHooks'; import { WorkflowRepository } from '@/databases/repositories'; import { RoleService } from '@/services/role.service'; import { OwnershipService } from '@/services/ownership.service'; -import { isStringArray } from '@/utils'; +import { isStringArray, isWorkflowIdValid } from '@/utils'; export class WorkflowsService { static async getSharing( @@ -476,4 +481,40 @@ export class WorkflowsService { .where('id = :id', { id }) .execute(); } + + /** + * Saves the static data if it changed + */ + static async saveStaticData(workflow: Workflow): Promise { + if (workflow.staticData.__dataChanged === true) { + // Static data of workflow changed and so has to be saved + if (isWorkflowIdValid(workflow.id)) { + // Workflow is saved so update in database + try { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + await WorkflowsService.saveStaticDataById(workflow.id!, workflow.staticData); + workflow.staticData.__dataChanged = false; + } catch (error) { + ErrorReporter.error(error); + LoggerProxy.error( + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + `There was a problem saving the workflow with id "${workflow.id}" to save changed staticData: "${error.message}"`, + { workflowId: workflow.id }, + ); + } + } + } + } + + /** + * Saves the given static data on workflow + * + * @param {(string)} workflowId The id of the workflow to save data on + * @param {IDataObject} newStaticData The static data to save + */ + static async saveStaticDataById(workflowId: string, newStaticData: IDataObject): Promise { + await Db.collections.Workflow.update(workflowId, { + staticData: newStaticData, + }); + } }