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): Remove circular dependency in WorkflowService and ActiveWorkflowRunner #8128

Merged
merged 2 commits into from
Dec 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
22 changes: 12 additions & 10 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

import { Container, Service } from 'typedi';
import { Service } from 'typedi';
import { ActiveWorkflows, NodeExecuteFunctions } from 'n8n-core';

import type {
Expand Down Expand Up @@ -59,7 +58,6 @@ import { NodeTypes } from '@/NodeTypes';
import { WorkflowRunner } from '@/WorkflowRunner';
import { ExternalHooks } from '@/ExternalHooks';
import { whereClause } from './UserManagement/UserManagementHelper';
import { WorkflowService } from './workflows/workflow.service';
import { WebhookNotFoundError } from './errors/response-errors/webhook-not-found.error';
import { In } from 'typeorm';
import { WebhookService } from './services/webhook.service';
Expand All @@ -70,6 +68,7 @@ import { MultiMainSetup } from '@/services/orchestration/main/MultiMainSetup.ee'
import { ActivationErrorsService } from '@/ActivationErrors.service';
import type { Scope } from '@n8n/permissions';
import { NotFoundError } from './errors/response-errors/not-found.error';
import { WorkflowStaticDataService } from '@/workflows/workflowStaticData.service';

@Service()
export class ActiveWorkflowRunner implements IWebhookManager {
Expand All @@ -95,6 +94,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
private readonly multiMainSetup: MultiMainSetup,
private readonly activationErrorsService: ActivationErrorsService,
private readonly executionService: ExecutionsService,
private readonly workflowStaticDataService: WorkflowStaticDataService,
) {}

async init() {
Expand Down Expand Up @@ -214,10 +214,12 @@ export class ActiveWorkflowRunner implements IWebhookManager {
undefined,
request,
response,
(error: Error | null, data: object) => {
async (error: Error | null, data: object) => {
if (error !== null) {
return reject(error);
}
// Save static data if it changed
await this.workflowStaticDataService.saveStaticData(workflow);
ivov marked this conversation as resolved.
Show resolved Hide resolved
resolve(data);
},
);
Expand Down Expand Up @@ -413,7 +415,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
}
await this.webhookService.populateCache();

await Container.get(WorkflowService).saveStaticData(workflow);
await this.workflowStaticDataService.saveStaticData(workflow);
}

/**
Expand Down Expand Up @@ -452,7 +454,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
await workflow.deleteWebhook(webhookData, NodeExecuteFunctions, mode, 'update');
}

await Container.get(WorkflowService).saveStaticData(workflow);
await this.workflowStaticDataService.saveStaticData(workflow);

await this.webhookService.deleteWorkflowWebhooks(workflowId);
}
Expand Down Expand Up @@ -525,7 +527,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
donePromise?: IDeferredPromise<IRun | undefined>,
): void => {
this.logger.debug(`Received event to trigger execution for workflow "${workflow.name}"`);
void Container.get(WorkflowService).saveStaticData(workflow);
void this.workflowStaticDataService.saveStaticData(workflow);
const executePromise = this.runWorkflow(
workflowData,
node,
Expand Down Expand Up @@ -582,7 +584,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
donePromise?: IDeferredPromise<IRun | undefined>,
): void => {
this.logger.debug(`Received trigger for workflow "${workflow.name}"`);
void Container.get(WorkflowService).saveStaticData(workflow);
void this.workflowStaticDataService.saveStaticData(workflow);

const executePromise = this.runWorkflow(
workflowData,
Expand Down Expand Up @@ -817,7 +819,7 @@ export class ActiveWorkflowRunner implements IWebhookManager {
await this.activationErrorsService.unset(workflowId);

const triggerCount = this.countTriggers(workflow, additionalData);
await Container.get(WorkflowService).updateWorkflowTriggerCount(workflow.id, triggerCount);
await this.workflowRepository.updateWorkflowTriggerCount(workflow.id, triggerCount);
} catch (e) {
const error = e instanceof Error ? e : new Error(`${e}`);
await this.activationErrorsService.set(workflowId, error.message);
Expand All @@ -827,7 +829,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 Container.get(WorkflowService).saveStaticData(workflow);
await this.workflowStaticDataService.saveStaticData(workflow);
}

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/cli/src/WebhookHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
import { EventsService } from '@/services/events.service';
import { OwnershipService } from './services/ownership.service';
import { parseBody } from './middlewares';
import { WorkflowService } from './workflows/workflow.service';
import { Logger } from './Logger';
import { NotFoundError } from './errors/response-errors/not-found.error';
import { InternalServerError } from './errors/response-errors/internal-server.error';
Expand Down Expand Up @@ -386,9 +385,6 @@ export async function executeWebhook(
};
}

// Save static data if it changed
await Container.get(WorkflowService).saveStaticData(workflow);

ivov marked this conversation as resolved.
Show resolved Hide resolved
const additionalKeys: IWorkflowDataProxyAdditionalKeys = {
$executionId: executionId,
};
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import * as WebhookHelpers from '@/WebhookHelpers';
import * as WorkflowHelpers from '@/WorkflowHelpers';
import { findSubworkflowStart, isWorkflowIdValid } from '@/utils';
import { PermissionChecker } from './UserManagement/PermissionChecker';
import { WorkflowService } from './workflows/workflow.service';
import { InternalHooks } from '@/InternalHooks';
import { ExecutionRepository } from '@db/repositories/execution.repository';
import { EventsService } from '@/services/events.service';
Expand All @@ -67,6 +66,8 @@ import { restoreBinaryDataId } from './executionLifecycleHooks/restoreBinaryData
import { toSaveSettings } from './executionLifecycleHooks/toSaveSettings';
import { Logger } from './Logger';
import { saveExecutionProgress } from './executionLifecycleHooks/saveExecutionProgress';
import { WorkflowStaticDataService } from './workflows/workflowStaticData.service';
import { WorkflowRepository } from './databases/repositories/workflow.repository';

const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType');

Expand Down Expand Up @@ -418,7 +419,7 @@ function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks {
if (!isManualMode && isWorkflowIdValid(this.workflowData.id) && newStaticData) {
// Workflow is saved so update in database
try {
await Container.get(WorkflowService).saveStaticDataById(
await Container.get(WorkflowStaticDataService).saveStaticDataById(
this.workflowData.id as string,
newStaticData,
);
Expand Down Expand Up @@ -564,7 +565,7 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
if (isWorkflowIdValid(this.workflowData.id) && newStaticData) {
// Workflow is saved so update in database
try {
await Container.get(WorkflowService).saveStaticDataById(
await Container.get(WorkflowStaticDataService).saveStaticDataById(
this.workflowData.id as string,
newStaticData,
);
Expand Down Expand Up @@ -714,7 +715,10 @@ export async function getWorkflowData(
if (workflowInfo.id !== undefined) {
const relations = config.getEnv('workflowTagsDisabled') ? [] : ['tags'];

workflowData = await Container.get(WorkflowService).get({ id: workflowInfo.id }, { relations });
workflowData = await Container.get(WorkflowRepository).get(
{ id: workflowInfo.id },
{ relations },
);

if (workflowData === undefined || workflowData === null) {
throw new ApplicationError('Workflow does not exist.', {
Expand Down
27 changes: 26 additions & 1 deletion packages/cli/src/databases/repositories/workflow.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm';
import { DataSource, Repository, type UpdateResult, type FindOptionsWhere } from 'typeorm';
import config from '@/config';
import { WorkflowEntity } from '../entities/WorkflowEntity';

@Service()
Expand All @@ -8,6 +9,13 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
super(WorkflowEntity, dataSource.manager);
}

async get(where: FindOptionsWhere<WorkflowEntity>, options?: { relations: string[] }) {
return this.findOne({
where,
relations: options?.relations,
});
}

async getAllActive() {
return this.find({
where: { active: true },
Expand All @@ -28,4 +36,21 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
});
return totalTriggerCount ?? 0;
}

async updateWorkflowTriggerCount(id: string, triggerCount: number): Promise<UpdateResult> {
const qb = this.createQueryBuilder('workflow');
return qb
.update()
.set({
triggerCount,
updatedAt: () => {
if (['mysqldb', 'mariadb'].includes(config.getEnv('database.type'))) {
return 'updatedAt';
}
return '"updatedAt"';
},
})
.where('id = :id', { id })
.execute();
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/executions/executions.service.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { IExecutionResponse, IExecutionFlattedResponse } from '@/Interfaces
import { EnterpriseWorkflowService } from '../workflows/workflow.service.ee';
import type { WorkflowWithSharingsAndCredentials } from '@/workflows/workflows.types';
import Container from 'typedi';
import { WorkflowService } from '@/workflows/workflow.service';
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';

export class EEExecutionsService extends ExecutionsService {
/**
Expand All @@ -26,7 +26,7 @@ export class EEExecutionsService extends ExecutionsService {

const relations = ['shared', 'shared.user', 'shared.role'];

const workflow = (await Container.get(WorkflowService).get(
const workflow = (await Container.get(WorkflowRepository).get(
{ id: execution.workflowId },
{ relations },
)) as WorkflowWithSharingsAndCredentials;
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/workflows/workflow.service.ee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.repository';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';

@Service()
export class EnterpriseWorkflowService {
Expand All @@ -26,6 +27,7 @@ export class EnterpriseWorkflowService {
private readonly userService: UserService,
private readonly roleService: RoleService,
private readonly sharedWorkflowRepository: SharedWorkflowRepository,
private readonly workflowRepository: WorkflowRepository,
) {}

async isOwned(
Expand Down Expand Up @@ -182,7 +184,7 @@ export class EnterpriseWorkflowService {
}

async preventTampering(workflow: WorkflowEntity, workflowId: string, user: User) {
const previousVersion = await this.workflowService.get({ id: workflowId });
const previousVersion = await this.workflowRepository.get({ id: workflowId });

if (!previousVersion) {
throw new NotFoundError('Workflow not found');
Expand Down
67 changes: 4 additions & 63 deletions packages/cli/src/workflows/workflow.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Container, { Service } from 'typedi';
import type { IDataObject, INode, IPinData } from 'n8n-workflow';
import { NodeApiError, ErrorReporterProxy as ErrorReporter, Workflow } from 'n8n-workflow';
import type { FindManyOptions, FindOptionsSelect, FindOptionsWhere, UpdateResult } from 'typeorm';
import type { INode, IPinData } from 'n8n-workflow';
import { NodeApiError, Workflow } from 'n8n-workflow';
import type { FindManyOptions, FindOptionsSelect, FindOptionsWhere } from 'typeorm';
import { In, Like } from 'typeorm';
import pick from 'lodash/pick';
import omit from 'lodash/omit';
Expand All @@ -25,7 +25,7 @@ import { whereClause } from '@/UserManagement/UserManagementHelper';
import { InternalHooks } from '@/InternalHooks';
import { WorkflowRepository } from '@db/repositories/workflow.repository';
import { OwnershipService } from '@/services/ownership.service';
import { isStringArray, isWorkflowIdValid } from '@/utils';
import { isStringArray } from '@/utils';
import { WorkflowHistoryService } from './workflowHistory/workflowHistory.service.ee';
import { BinaryDataService } from 'n8n-core';
import type { Scope } from '@n8n/permissions';
Expand Down Expand Up @@ -120,13 +120,6 @@ export class WorkflowService {
return pinnedTriggers.find((pt) => pt.name === checkNodeName) ?? null; // partial execution
}

async get(workflow: FindOptionsWhere<WorkflowEntity>, options?: { relations: string[] }) {
return this.workflowRepository.findOne({
where: workflow,
relations: options?.relations,
});
}

async getMany(sharedWorkflowIds: string[], options?: ListQuery.Options) {
if (sharedWorkflowIds.length === 0) return { workflows: [], count: 0 };

Expand Down Expand Up @@ -512,56 +505,4 @@ export class WorkflowService {

return sharedWorkflow.workflow;
}

async updateWorkflowTriggerCount(id: string, triggerCount: number): Promise<UpdateResult> {
const qb = this.workflowRepository.createQueryBuilder('workflow');
return qb
.update()
.set({
triggerCount,
updatedAt: () => {
if (['mysqldb', 'mariadb'].includes(config.getEnv('database.type'))) {
return 'updatedAt';
}
return '"updatedAt"';
},
})
.where('id = :id', { id })
.execute();
}

/**
* Saves the static data if it changed
*/
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 {
await this.saveStaticDataById(workflow.id, workflow.staticData);
workflow.staticData.__dataChanged = false;
} catch (error) {
ErrorReporter.error(error);
this.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 Data: "${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
*/
async saveStaticDataById(workflowId: string, newStaticData: IDataObject): Promise<void> {
await this.workflowRepository.update(workflowId, {
staticData: newStaticData,
});
}
}
41 changes: 41 additions & 0 deletions packages/cli/src/workflows/workflowStaticData.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Service } from 'typedi';
import { type IDataObject, type Workflow, ErrorReporterProxy as ErrorReporter } from 'n8n-workflow';
import { Logger } from '@/Logger';
import { WorkflowRepository } from '@db/repositories/workflow.repository';
import { isWorkflowIdValid } from '@/utils';

@Service()
export class WorkflowStaticDataService {
constructor(
private readonly logger: Logger,
private readonly workflowRepository: WorkflowRepository,
) {}

/** Saves the static data if it changed */
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 {
await this.saveStaticDataById(workflow.id, workflow.staticData);
workflow.staticData.__dataChanged = false;
} catch (error) {
ErrorReporter.error(error);
this.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 Data: "${error.message}"`,
{ workflowId: workflow.id },
);
}
}
}
}

/** Saves the given static data on workflow */
async saveStaticDataById(workflowId: string, newStaticData: IDataObject): Promise<void> {
await this.workflowRepository.update(workflowId, {
staticData: newStaticData,
});
}
}
Loading
Loading