2024-01-26 04:49:39 -08:00
|
|
|
import { Service } from 'typedi';
|
|
|
|
import type PCancelable from 'p-cancelable';
|
2023-02-17 01:54:07 -08:00
|
|
|
import type {
|
|
|
|
IDeferredPromise,
|
|
|
|
IExecuteResponsePromiseData,
|
|
|
|
IRun,
|
|
|
|
ExecutionStatus,
|
|
|
|
} from 'n8n-workflow';
|
2024-02-06 09:09:50 -08:00
|
|
|
import { ApplicationError, createDeferredPromise, sleep } from 'n8n-workflow';
|
2019-08-08 11:38:25 -07:00
|
|
|
|
2023-01-27 05:56:56 -08:00
|
|
|
import type {
|
2023-09-20 06:21:42 -07:00
|
|
|
ExecutionPayload,
|
2019-08-08 11:38:25 -07:00
|
|
|
IExecutingWorkflowData,
|
2021-02-08 23:59:32 -08:00
|
|
|
IExecutionDb,
|
2020-10-22 06:46:03 -07:00
|
|
|
IExecutionsCurrentSummary,
|
2019-08-08 11:38:25 -07:00
|
|
|
IWorkflowExecutionDataProcess,
|
2022-11-09 06:25:00 -08:00
|
|
|
} from '@/Interfaces';
|
2023-03-16 07:34:13 -07:00
|
|
|
import { isWorkflowIdValid } from '@/utils';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { ExecutionRepository } from '@db/repositories/execution.repository';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2024-06-12 06:05:43 -07:00
|
|
|
import { ConcurrencyControlService } from './concurrency/concurrency-control.service';
|
|
|
|
import config from './config';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
@Service()
|
2019-06-23 03:35:23 -07:00
|
|
|
export class ActiveExecutions {
|
2024-04-18 22:50:18 -07:00
|
|
|
/**
|
|
|
|
* Active executions in the current process, not globally.
|
|
|
|
*/
|
2019-06-23 03:35:23 -07:00
|
|
|
private activeExecutions: {
|
2024-02-06 09:09:50 -08:00
|
|
|
[executionId: string]: IExecutingWorkflowData;
|
2019-06-23 03:35:23 -07:00
|
|
|
} = {};
|
|
|
|
|
2024-01-26 04:49:39 -08:00
|
|
|
constructor(
|
|
|
|
private readonly logger: Logger,
|
|
|
|
private readonly executionRepository: ExecutionRepository,
|
2024-06-12 06:05:43 -07:00
|
|
|
private readonly concurrencyControl: ConcurrencyControlService,
|
2024-01-26 04:49:39 -08:00
|
|
|
) {}
|
2023-10-25 07:35:22 -07:00
|
|
|
|
2024-06-28 11:05:09 -07:00
|
|
|
has(executionId: string) {
|
|
|
|
return this.activeExecutions[executionId] !== undefined;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Add a new active execution
|
|
|
|
*/
|
2024-01-30 03:51:40 -08:00
|
|
|
async add(executionData: IWorkflowExecutionDataProcess, executionId?: string): Promise<string> {
|
2023-02-17 01:54:07 -08:00
|
|
|
let executionStatus: ExecutionStatus = executionId ? 'running' : 'new';
|
2024-06-12 06:05:43 -07:00
|
|
|
const mode = executionData.executionMode;
|
2021-08-21 05:11:32 -07:00
|
|
|
if (executionId === undefined) {
|
|
|
|
// Is a new execution so save in DB
|
2021-02-08 23:59:32 -08:00
|
|
|
|
2023-09-20 06:21:42 -07:00
|
|
|
const fullExecutionData: ExecutionPayload = {
|
2021-08-21 05:11:32 -07:00
|
|
|
data: executionData.executionData!,
|
2024-06-12 06:05:43 -07:00
|
|
|
mode,
|
2021-08-21 05:11:32 -07:00
|
|
|
finished: false,
|
|
|
|
startedAt: new Date(),
|
|
|
|
workflowData: executionData.workflowData,
|
2023-02-17 01:54:07 -08:00
|
|
|
status: executionStatus,
|
2024-01-16 01:53:17 -08:00
|
|
|
workflowId: executionData.workflowData.id,
|
2021-08-21 05:11:32 -07:00
|
|
|
};
|
2021-02-08 23:59:32 -08:00
|
|
|
|
2021-08-21 05:11:32 -07:00
|
|
|
if (executionData.retryOf !== undefined) {
|
|
|
|
fullExecutionData.retryOf = executionData.retryOf.toString();
|
|
|
|
}
|
2021-05-29 11:31:21 -07:00
|
|
|
|
2023-01-02 08:42:32 -08:00
|
|
|
const workflowId = executionData.workflowData.id;
|
2023-03-16 07:34:13 -07:00
|
|
|
if (workflowId !== undefined && isWorkflowIdValid(workflowId)) {
|
2023-01-02 08:42:32 -08:00
|
|
|
fullExecutionData.workflowId = workflowId;
|
2021-08-21 05:11:32 -07:00
|
|
|
}
|
2021-02-08 23:59:32 -08:00
|
|
|
|
2024-01-26 04:49:39 -08:00
|
|
|
executionId = await this.executionRepository.createNewExecution(fullExecutionData);
|
2023-02-17 01:54:07 -08:00
|
|
|
if (executionId === undefined) {
|
2023-11-29 03:25:10 -08:00
|
|
|
throw new ApplicationError('There was an issue assigning an execution id to the execution');
|
2023-02-17 01:54:07 -08:00
|
|
|
}
|
2024-06-12 06:05:43 -07:00
|
|
|
|
|
|
|
await this.concurrencyControl.throttle({ mode, executionId });
|
2023-02-17 01:54:07 -08:00
|
|
|
executionStatus = 'running';
|
2021-08-21 05:11:32 -07:00
|
|
|
} else {
|
|
|
|
// Is an existing execution we want to finish so update in DB
|
|
|
|
|
2024-06-12 06:05:43 -07:00
|
|
|
await this.concurrencyControl.throttle({ mode, executionId });
|
|
|
|
|
2023-06-20 10:13:18 -07:00
|
|
|
const execution: Pick<IExecutionDb, 'id' | 'data' | 'waitTill' | 'status'> = {
|
2021-08-21 05:11:32 -07:00
|
|
|
id: executionId,
|
2023-06-20 10:13:18 -07:00
|
|
|
data: executionData.executionData!,
|
2021-08-21 05:11:32 -07:00
|
|
|
waitTill: null,
|
2023-02-17 01:54:07 -08:00
|
|
|
status: executionStatus,
|
2021-08-21 05:11:32 -07:00
|
|
|
};
|
|
|
|
|
2024-01-26 04:49:39 -08:00
|
|
|
await this.executionRepository.updateExistingExecution(executionId, execution);
|
2021-08-21 05:11:32 -07:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
this.activeExecutions[executionId] = {
|
2019-08-08 11:38:25 -07:00
|
|
|
executionData,
|
2019-07-22 11:29:06 -07:00
|
|
|
startedAt: new Date(),
|
2019-06-23 03:35:23 -07:00
|
|
|
postExecutePromises: [],
|
2023-02-17 01:54:07 -08:00
|
|
|
status: executionStatus,
|
2019-06-23 03:35:23 -07:00
|
|
|
};
|
|
|
|
|
2021-02-08 23:59:32 -08:00
|
|
|
return executionId;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2020-01-17 17:34:31 -08:00
|
|
|
/**
|
|
|
|
* Attaches an execution
|
|
|
|
*/
|
2023-07-31 02:00:48 -07:00
|
|
|
|
2020-01-17 17:34:31 -08:00
|
|
|
attachWorkflowExecution(executionId: string, workflowExecution: PCancelable<IRun>) {
|
2024-02-06 09:09:50 -08:00
|
|
|
this.getExecution(executionId).workflowExecution = workflowExecution;
|
2020-01-17 17:34:31 -08:00
|
|
|
}
|
|
|
|
|
2021-11-05 09:45:51 -07:00
|
|
|
attachResponsePromise(
|
|
|
|
executionId: string,
|
|
|
|
responsePromise: IDeferredPromise<IExecuteResponsePromiseData>,
|
|
|
|
): void {
|
2024-02-06 09:09:50 -08:00
|
|
|
this.getExecution(executionId).responsePromise = responsePromise;
|
2021-11-05 09:45:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
resolveResponsePromise(executionId: string, response: IExecuteResponsePromiseData): void {
|
2024-01-26 04:49:39 -08:00
|
|
|
const execution = this.activeExecutions[executionId];
|
|
|
|
execution?.responsePromise?.resolve(response);
|
2021-11-05 09:45:51 -07:00
|
|
|
}
|
|
|
|
|
refactor(core): Move event and telemetry handling into workers in queue mode (#7138)
# Motivation
In Queue mode, finished executions would cause the main instance to
always pull all execution data from the database, unflatten it and then
use it to send out event log events and telemetry events, as well as
required returns to Respond to Webhook nodes etc.
This could cause OOM errors when the data was large, since it had to be
fully unpacked and transformed on the main instance’s side, using up a
lot of memory (and time).
This PR attempts to limit this behaviour to only happen in those
required cases where the data has to be forwarded to some waiting
webhook, for example.
# Changes
Execution data is only required in cases, where the active execution has
a `postExecutePromise` attached to it. These usually forward the data to
some other endpoint (e.g. a listening webhook connection).
By adding a helper `getPostExecutePromiseCount()`, we can decide that in
cases where there is nothing listening at all, there is no reason to
pull the data on the main instance.
Previously, there would always be postExecutePromises because the
telemetry events were called. Now, these have been moved into the
workers, which have been given the various InternalHooks calls to their
hook function arrays, so they themselves issue these telemetry and event
calls.
This results in all event log messages to now be logged on the worker’s
event log, as well as the worker’s eventbus being the one to send out
the events to destinations. The main event log does…pretty much nothing.
We are not logging executions on the main event log any more, because
this would require all events to be replicated 1:1 from the workers to
the main instance(s) (this IS possible and implemented, see the worker’s
`replicateToRedisEventLogFunction` - but it is not enabled to reduce the
amount of traffic over redis).
Partial events in the main log could confuse the recovery process and
would result in, ironically, the recovery corrupting the execution data
by considering them crashed.
# Refactor
I have also used the opportunity to reduce duplicate code and move some
of the hook functionality into
`packages/cli/src/executionLifecycleHooks/shared/sharedHookFunctions.ts`
in preparation for a future full refactor of the hooks
2023-09-13 22:58:15 -07:00
|
|
|
getPostExecutePromiseCount(executionId: string): number {
|
|
|
|
return this.activeExecutions[executionId]?.postExecutePromises.length ?? 0;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Remove an active execution
|
|
|
|
*/
|
2019-08-08 11:38:25 -07:00
|
|
|
remove(executionId: string, fullRunData?: IRun): void {
|
2024-01-26 04:49:39 -08:00
|
|
|
const execution = this.activeExecutions[executionId];
|
|
|
|
if (execution === undefined) {
|
2019-06-23 03:35:23 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve all the waiting promises
|
2024-01-26 04:49:39 -08:00
|
|
|
for (const promise of execution.postExecutePromises) {
|
2019-06-23 03:35:23 -07:00
|
|
|
promise.resolve(fullRunData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from the list of active executions
|
|
|
|
delete this.activeExecutions[executionId];
|
2024-06-12 06:05:43 -07:00
|
|
|
|
|
|
|
this.concurrencyControl.release({ mode: execution.executionData.executionMode });
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces an execution to stop
|
|
|
|
*/
|
2024-01-30 03:51:40 -08:00
|
|
|
async stopExecution(executionId: string): Promise<IRun | undefined> {
|
2024-01-26 04:49:39 -08:00
|
|
|
const execution = this.activeExecutions[executionId];
|
|
|
|
if (execution === undefined) {
|
2019-06-23 03:35:23 -07:00
|
|
|
// There is no execution running with that id
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-30 03:51:40 -08:00
|
|
|
execution.workflowExecution!.cancel();
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.getPostExecutePromise(executionId);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-06 09:09:50 -08:00
|
|
|
* Returns a promise which will resolve with the data of the execution with the given id
|
2019-06-23 03:35:23 -07:00
|
|
|
*/
|
2019-08-08 11:38:25 -07:00
|
|
|
async getPostExecutePromise(executionId: string): Promise<IRun | undefined> {
|
2023-10-27 00:25:07 -07:00
|
|
|
// Create the promise which will be resolved when the execution finished
|
|
|
|
const waitPromise = await createDeferredPromise<IRun | undefined>();
|
2024-02-06 09:09:50 -08:00
|
|
|
this.getExecution(executionId).postExecutePromises.push(waitPromise);
|
2024-01-17 07:08:50 -08:00
|
|
|
return await waitPromise.promise();
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all the currently active executions
|
|
|
|
*/
|
|
|
|
getActiveExecutions(): IExecutionsCurrentSummary[] {
|
|
|
|
const returnData: IExecutionsCurrentSummary[] = [];
|
|
|
|
|
2019-08-08 11:38:25 -07:00
|
|
|
let data;
|
2023-07-31 02:00:48 -07:00
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
for (const id of Object.keys(this.activeExecutions)) {
|
2019-08-08 11:38:25 -07:00
|
|
|
data = this.activeExecutions[id];
|
2019-06-23 03:35:23 -07:00
|
|
|
returnData.push({
|
|
|
|
id,
|
2024-01-26 04:49:39 -08:00
|
|
|
retryOf: data.executionData.retryOf,
|
2019-08-08 11:38:25 -07:00
|
|
|
startedAt: data.startedAt,
|
|
|
|
mode: data.executionData.executionMode,
|
2024-01-26 04:49:39 -08:00
|
|
|
workflowId: data.executionData.workflowData.id,
|
2023-02-17 01:54:07 -08:00
|
|
|
status: data.status,
|
2019-06-23 03:35:23 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
2023-02-17 01:54:07 -08:00
|
|
|
|
2024-02-06 09:09:50 -08:00
|
|
|
setStatus(executionId: string, status: ExecutionStatus) {
|
|
|
|
this.getExecution(executionId).status = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
getStatus(executionId: string): ExecutionStatus {
|
|
|
|
return this.getExecution(executionId).status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Wait for all active executions to finish */
|
|
|
|
async shutdown(cancelAll = false) {
|
|
|
|
let executionIds = Object.keys(this.activeExecutions);
|
|
|
|
|
2024-07-02 08:07:07 -07:00
|
|
|
if (config.getEnv('executions.mode') === 'regular') {
|
|
|
|
// removal of active executions will no longer release capacity back,
|
|
|
|
// so that throttled executions cannot resume during shutdown
|
|
|
|
this.concurrencyControl.disable();
|
|
|
|
}
|
|
|
|
|
2024-02-06 09:09:50 -08:00
|
|
|
if (cancelAll) {
|
2024-06-12 06:05:43 -07:00
|
|
|
if (config.getEnv('executions.mode') === 'regular') {
|
|
|
|
await this.concurrencyControl.removeAll(this.activeExecutions);
|
|
|
|
}
|
|
|
|
|
2024-02-06 09:09:50 -08:00
|
|
|
const stopPromises = executionIds.map(
|
|
|
|
async (executionId) => await this.stopExecution(executionId),
|
2023-02-17 01:54:07 -08:00
|
|
|
);
|
2024-02-06 09:09:50 -08:00
|
|
|
|
|
|
|
await Promise.allSettled(stopPromises);
|
2023-02-17 01:54:07 -08:00
|
|
|
}
|
|
|
|
|
2024-02-06 09:09:50 -08:00
|
|
|
let count = 0;
|
|
|
|
while (executionIds.length !== 0) {
|
|
|
|
if (count++ % 4 === 0) {
|
|
|
|
this.logger.info(`Waiting for ${executionIds.length} active executions to finish...`);
|
|
|
|
}
|
|
|
|
|
|
|
|
await sleep(500);
|
|
|
|
executionIds = Object.keys(this.activeExecutions);
|
|
|
|
}
|
2023-02-17 01:54:07 -08:00
|
|
|
}
|
|
|
|
|
2024-02-06 09:09:50 -08:00
|
|
|
private getExecution(executionId: string): IExecutingWorkflowData {
|
2024-01-26 04:49:39 -08:00
|
|
|
const execution = this.activeExecutions[executionId];
|
2024-02-06 09:09:50 -08:00
|
|
|
if (!execution) {
|
|
|
|
throw new ApplicationError('No active execution found', { extra: { executionId } });
|
|
|
|
}
|
|
|
|
return execution;
|
2023-02-17 01:54:07 -08:00
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|