refactor(core): Refactor WorkflowStatistics code (no-changelog) (#6617)

refactor(core): Refactor WorkflowStatistics code
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-07-18 11:28:24 +02:00
committed by GitHub
parent e7091d6726
commit f4a18ba87d
20 changed files with 432 additions and 571 deletions

View File

@@ -15,7 +15,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable func-names */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { BinaryDataManager, eventEmitter, UserSettings, WorkflowExecute } from 'n8n-core';
import { BinaryDataManager, UserSettings, WorkflowExecute } from 'n8n-core';
import type {
IDataObject,
@@ -43,6 +43,7 @@ import {
} from 'n8n-workflow';
import pick from 'lodash/pick';
import { Container } from 'typedi';
import type { FindOptionsWhere } from 'typeorm';
import { LessThanOrEqual, In } from 'typeorm';
import { DateUtils } from 'typeorm/util/DateUtils';
@@ -67,10 +68,10 @@ import { getWorkflowOwner } from '@/UserManagement/UserManagementHelper';
import { findSubworkflowStart, isWorkflowIdValid } from '@/utils';
import { PermissionChecker } from './UserManagement/PermissionChecker';
import { WorkflowsService } from './workflows/workflows.services';
import { Container } from 'typedi';
import { InternalHooks } from '@/InternalHooks';
import type { ExecutionMetadata } from '@db/entities/ExecutionMetadata';
import { ExecutionRepository } from '@db/repositories';
import { EventsService } from '@/services/events.service';
const ERROR_TRIGGER_TYPE = config.getEnv('nodes.errorTriggerType');
@@ -273,6 +274,7 @@ export async function saveExecutionMetadata(
*
*/
function hookFunctionsPush(): IWorkflowExecuteHooks {
const pushInstance = Container.get(Push);
return {
nodeExecuteBefore: [
async function (this: WorkflowHooks, nodeName: string): Promise<void> {
@@ -289,7 +291,6 @@ function hookFunctionsPush(): IWorkflowExecuteHooks {
workflowId: this.workflowData.id,
});
const pushInstance = Container.get(Push);
pushInstance.send('nodeExecuteBefore', { executionId, nodeName }, sessionId);
},
],
@@ -307,7 +308,6 @@ function hookFunctionsPush(): IWorkflowExecuteHooks {
workflowId: this.workflowData.id,
});
const pushInstance = Container.get(Push);
pushInstance.send('nodeExecuteAfter', { executionId, nodeName, data }, sessionId);
},
],
@@ -324,7 +324,6 @@ function hookFunctionsPush(): IWorkflowExecuteHooks {
if (sessionId === undefined) {
return;
}
const pushInstance = Container.get(Push);
pushInstance.send(
'executionStarted',
{
@@ -390,7 +389,6 @@ function hookFunctionsPush(): IWorkflowExecuteHooks {
retryOf,
};
const pushInstance = Container.get(Push);
pushInstance.send('executionFinished', sendData, sessionId);
},
],
@@ -399,7 +397,6 @@ function hookFunctionsPush(): IWorkflowExecuteHooks {
export function hookFunctionsPreExecute(parentProcessMode?: string): IWorkflowExecuteHooks {
const externalHooks = Container.get(ExternalHooks);
return {
workflowExecuteBefore: [
async function (this: WorkflowHooks, workflow: Workflow): Promise<void> {
@@ -514,23 +511,17 @@ export function hookFunctionsPreExecute(parentProcessMode?: string): IWorkflowEx
*
*/
function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks {
const internalHooks = Container.get(InternalHooks);
const eventsService = Container.get(EventsService);
return {
nodeExecuteBefore: [
async function (this: WorkflowHooks, nodeName: string): Promise<void> {
void Container.get(InternalHooks).onNodeBeforeExecute(
this.executionId,
this.workflowData,
nodeName,
);
void internalHooks.onNodeBeforeExecute(this.executionId, this.workflowData, nodeName);
},
],
nodeExecuteAfter: [
async function (this: WorkflowHooks, nodeName: string): Promise<void> {
void Container.get(InternalHooks).onNodePostExecute(
this.executionId,
this.workflowData,
nodeName,
);
void internalHooks.onNodePostExecute(this.executionId, this.workflowData, nodeName);
},
],
workflowExecuteBefore: [],
@@ -711,17 +702,13 @@ function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks {
);
}
} finally {
eventEmitter.emit(
eventEmitter.types.workflowExecutionCompleted,
this.workflowData,
fullRunData,
);
eventsService.emit('workflowExecutionCompleted', this.workflowData, fullRunData);
}
},
],
nodeFetchedData: [
async (workflowId: string, node: INode) => {
eventEmitter.emit(eventEmitter.types.nodeFetchedData, workflowId, node);
eventsService.emit('nodeFetchedData', workflowId, node);
},
],
};
@@ -734,6 +721,7 @@ function hookFunctionsSave(parentProcessMode?: string): IWorkflowExecuteHooks {
*
*/
function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
const eventsService = Container.get(EventsService);
return {
nodeExecuteBefore: [],
nodeExecuteAfter: [],
@@ -834,17 +822,13 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
this.retryOf,
);
} finally {
eventEmitter.emit(
eventEmitter.types.workflowExecutionCompleted,
this.workflowData,
fullRunData,
);
eventsService.emit('workflowExecutionCompleted', this.workflowData, fullRunData);
}
},
],
nodeFetchedData: [
async (workflowId: string, node: INode) => {
eventEmitter.emit(eventEmitter.types.nodeFetchedData, workflowId, node);
eventsService.emit('nodeFetchedData', workflowId, node);
},
],
};
@@ -951,10 +935,12 @@ async function executeWorkflow(
parentWorkflowSettings?: IWorkflowSettings;
},
): Promise<Array<INodeExecutionData[] | null> | IWorkflowExecuteProcess> {
const internalHooks = Container.get(InternalHooks);
const externalHooks = Container.get(ExternalHooks);
await externalHooks.init();
const nodeTypes = Container.get(NodeTypes);
const activeExecutions = Container.get(ActiveExecutions);
const workflowData =
options.loadedWorkflowData ??
@@ -984,10 +970,10 @@ async function executeWorkflow(
executionId =
options.parentExecutionId !== undefined
? options.parentExecutionId
: await Container.get(ActiveExecutions).add(runData);
: await activeExecutions.add(runData);
}
void Container.get(InternalHooks).onWorkflowBeforeExecute(executionId || '', runData);
void internalHooks.onWorkflowBeforeExecute(executionId || '', runData);
let data;
try {
@@ -1077,7 +1063,7 @@ async function executeWorkflow(
}
// remove execution from active executions
Container.get(ActiveExecutions).remove(executionId, fullRunData);
activeExecutions.remove(executionId, fullRunData);
await Container.get(ExecutionRepository).updateExistingExecution(
executionId,
@@ -1092,21 +1078,16 @@ async function executeWorkflow(
await externalHooks.run('workflow.postExecute', [data, workflowData, executionId]);
void Container.get(InternalHooks).onWorkflowPostExecute(
executionId,
workflowData,
data,
additionalData.userId,
);
void internalHooks.onWorkflowPostExecute(executionId, workflowData, data, additionalData.userId);
if (data.finished === true) {
// Workflow did finish successfully
Container.get(ActiveExecutions).remove(executionId, data);
activeExecutions.remove(executionId, data);
const returnData = WorkflowHelpers.getDataLastExecutedNodeData(data);
return returnData!.data!.main;
}
Container.get(ActiveExecutions).remove(executionId, data);
activeExecutions.remove(executionId, data);
// Workflow did fail
const { error } = data.data.resultData;
// eslint-disable-next-line @typescript-eslint/no-throw-literal