refactor(core): Move static data handling to workflow service (no-changelog) (#7104)

This commit is contained in:
Iván Ovejero 2023-09-05 13:42:31 +02:00 committed by GitHub
parent 011ee2e04b
commit 0036a4726c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 52 deletions

View file

@ -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<IRun | undefined>,
): 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<IRun | undefined>,
): 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!);
}
/**

View file

@ -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,

View file

@ -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,
);

View file

@ -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<void> {
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<void> {
await Db.collections.Workflow.update(workflowId, {
staticData: newStaticData,
});
}
/**
* Returns the static data of workflow
*/

View file

@ -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<void> {
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<void> {
await Db.collections.Workflow.update(workflowId, {
staticData: newStaticData,
});
}
}