perf(core): Introduce concurrency control for main mode (#9453)
This commit is contained in:
@@ -18,6 +18,8 @@ import type {
|
||||
import { isWorkflowIdValid } from '@/utils';
|
||||
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
||||
import { Logger } from '@/Logger';
|
||||
import { ConcurrencyControlService } from './concurrency/concurrency-control.service';
|
||||
import config from './config';
|
||||
|
||||
@Service()
|
||||
export class ActiveExecutions {
|
||||
@@ -31,6 +33,7 @@ export class ActiveExecutions {
|
||||
constructor(
|
||||
private readonly logger: Logger,
|
||||
private readonly executionRepository: ExecutionRepository,
|
||||
private readonly concurrencyControl: ConcurrencyControlService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -38,12 +41,13 @@ export class ActiveExecutions {
|
||||
*/
|
||||
async add(executionData: IWorkflowExecutionDataProcess, executionId?: string): Promise<string> {
|
||||
let executionStatus: ExecutionStatus = executionId ? 'running' : 'new';
|
||||
const mode = executionData.executionMode;
|
||||
if (executionId === undefined) {
|
||||
// Is a new execution so save in DB
|
||||
|
||||
const fullExecutionData: ExecutionPayload = {
|
||||
data: executionData.executionData!,
|
||||
mode: executionData.executionMode,
|
||||
mode,
|
||||
finished: false,
|
||||
startedAt: new Date(),
|
||||
workflowData: executionData.workflowData,
|
||||
@@ -64,10 +68,14 @@ export class ActiveExecutions {
|
||||
if (executionId === undefined) {
|
||||
throw new ApplicationError('There was an issue assigning an execution id to the execution');
|
||||
}
|
||||
|
||||
await this.concurrencyControl.throttle({ mode, executionId });
|
||||
executionStatus = 'running';
|
||||
} else {
|
||||
// Is an existing execution we want to finish so update in DB
|
||||
|
||||
await this.concurrencyControl.throttle({ mode, executionId });
|
||||
|
||||
const execution: Pick<IExecutionDb, 'id' | 'data' | 'waitTill' | 'status'> = {
|
||||
id: executionId,
|
||||
data: executionData.executionData!,
|
||||
@@ -128,6 +136,8 @@ export class ActiveExecutions {
|
||||
|
||||
// Remove from the list of active executions
|
||||
delete this.activeExecutions[executionId];
|
||||
|
||||
this.concurrencyControl.release({ mode: execution.executionData.executionMode });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,6 +201,10 @@ export class ActiveExecutions {
|
||||
let executionIds = Object.keys(this.activeExecutions);
|
||||
|
||||
if (cancelAll) {
|
||||
if (config.getEnv('executions.mode') === 'regular') {
|
||||
await this.concurrencyControl.removeAll(this.activeExecutions);
|
||||
}
|
||||
|
||||
const stopPromises = executionIds.map(
|
||||
async (executionId) => await this.stopExecution(executionId),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user