Skip to content

Commit

Permalink
fix(core): Prevent executions from becoming forever running
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Nov 1, 2023
1 parent ce14f62 commit 297d67f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
15 changes: 4 additions & 11 deletions packages/cli/src/InternalHooks.ts
Expand Up @@ -27,7 +27,6 @@ import type { User } from '@db/entities/User';
import { N8N_VERSION } from '@/constants';
import { NodeTypes } from './NodeTypes';
import type { ExecutionMetadata } from '@db/entities/ExecutionMetadata';
import { ExecutionRepository } from '@db/repositories';
import { RoleService } from './services/role.service';
import type { EventPayloadWorkflow } from './eventbus/EventMessageClasses/EventMessageWorkflow';
import { determineFinalExecutionStatus } from './executionLifecycleHooks/shared/sharedHookFunctions';
Expand Down Expand Up @@ -55,7 +54,6 @@ export class InternalHooks implements IInternalHooksClass {
private telemetry: Telemetry,
private nodeTypes: NodeTypes,
private roleService: RoleService,
private executionRepository: ExecutionRepository,
eventsService: EventsService,
private readonly instanceSettings: InstanceSettings,
) {
Expand Down Expand Up @@ -256,15 +254,10 @@ export class InternalHooks implements IInternalHooksClass {
workflowName: (data as IWorkflowBase).name,
};
}
void Promise.all([
this.executionRepository.updateExistingExecution(executionId, {
status: 'running',
}),
eventBus.sendWorkflowEvent({
eventName: 'n8n.workflow.started',
payload,
}),
]);
void eventBus.sendWorkflowEvent({
eventName: 'n8n.workflow.started',
payload,
});
}

async onWorkflowCrashed(
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/WorkflowExecuteAdditionalData.ts
Expand Up @@ -619,6 +619,7 @@ function hookFunctionsSaveWorker(): IWorkflowExecuteHooks {
],
workflowExecuteBefore: [
async function (workflow: Workflow, data: IRunExecutionData): Promise<void> {
await Container.get(ExecutionRepository).updateStatus(this.executionId, 'running');
void internalHooks.onWorkflowBeforeExecute(this.executionId, this.workflowData);
},
],
Expand Down Expand Up @@ -844,6 +845,7 @@ async function executeWorkflow(
: await activeExecutions.add(runData);
}

await Container.get(ExecutionRepository).updateStatus(executionId, 'running');
void internalHooks.onWorkflowBeforeExecute(executionId || '', runData);

let data;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/WorkflowRunner.ts
Expand Up @@ -212,6 +212,7 @@ export class WorkflowRunner {
} else {
executionId = await this.runSubprocess(data, loadStaticData, executionId, responsePromise);
}
await Container.get(ExecutionRepository).updateStatus(executionId, 'running');
void Container.get(InternalHooks).onWorkflowBeforeExecute(executionId, data);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/WorkflowRunnerProcess.ts
Expand Up @@ -34,6 +34,7 @@ import {
WorkflowOperationError,
} from 'n8n-workflow';
import * as Db from '@/Db';
import { ExecutionRepository } from '@db/repositories';
import { ExternalHooks } from '@/ExternalHooks';
import type {
IWorkflowExecuteProcess,
Expand Down Expand Up @@ -221,6 +222,7 @@ class WorkflowRunnerProcess {
};
});

await Container.get(ExecutionRepository).updateStatus(executionId, 'running');
void Container.get(InternalHooks).onWorkflowBeforeExecute(executionId || '', runData);

let result: IRun;
Expand Down
Expand Up @@ -17,7 +17,7 @@ import type {
SelectQueryBuilder,
} from 'typeorm';
import { parse, stringify } from 'flatted';
import type { IExecutionsSummary, IRunExecutionData } from 'n8n-workflow';
import type { ExecutionStatus, IExecutionsSummary, IRunExecutionData } from 'n8n-workflow';
import { BinaryDataService } from 'n8n-core';
import type {
ExecutionPayload,
Expand Down Expand Up @@ -298,6 +298,10 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
return Promise.all([this.delete(ids.executionId), this.binaryDataService.deleteMany([ids])]);
}

async updateStatus(executionId: string, status: ExecutionStatus) {
await this.update({ id: executionId }, { status });
}

async updateExistingExecution(executionId: string, execution: Partial<IExecutionResponse>) {
// Se isolate startedAt because it must be set when the execution starts and should never change.
// So we prevent updating it, if it's sent (it usually is and causes problems to executions that
Expand Down

0 comments on commit 297d67f

Please sign in to comment.