Skip to content

Commit

Permalink
fix(core): Prevent executions from becoming forever running (#7569)
Browse files Browse the repository at this point in the history
Fixes CP-867
Possibly also fixes PAY-323 and PAY-412
  • Loading branch information
netroy committed Nov 1, 2023
1 parent 0746783 commit 9bdb85c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 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
7 changes: 2 additions & 5 deletions packages/cli/src/WorkflowRunner.ts
Expand Up @@ -306,13 +306,9 @@ export class WorkflowRunner {
{ executionId },
);
let workflowExecution: PCancelable<IRun>;
await Container.get(ExecutionRepository).updateStatus(executionId, 'running');

try {
this.logger.verbose(
`Execution for workflow ${data.workflowData.name} was assigned id ${executionId}`,
{ executionId },
);

additionalData.hooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(
data,
executionId,
Expand Down Expand Up @@ -701,6 +697,7 @@ export class WorkflowRunner {
const executionId = await this.activeExecutions.add(data, subprocess, restartExecutionId);

(data as unknown as IWorkflowExecutionDataProcessWithExecution).executionId = executionId;
await Container.get(ExecutionRepository).updateStatus(executionId, 'running');

const workflowHooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(data, executionId);

Expand Down
13 changes: 6 additions & 7 deletions packages/cli/src/commands/worker.ts
Expand Up @@ -112,13 +112,11 @@ export class Worker extends BaseCommand {

async runJob(job: Job, nodeTypes: INodeTypes): Promise<JobResponse> {
const { executionId, loadStaticData } = job.data;
const fullExecutionData = await Container.get(ExecutionRepository).findSingleExecution(
executionId,
{
includeData: true,
unflattenData: true,
},
);
const executionRepository = Container.get(ExecutionRepository);
const fullExecutionData = await executionRepository.findSingleExecution(executionId, {
includeData: true,
unflattenData: true,
});

if (!fullExecutionData) {
this.logger.error(
Expand All @@ -133,6 +131,7 @@ export class Worker extends BaseCommand {
this.logger.info(
`Start job: ${job.id} (Workflow ID: ${workflowId} | Execution: ${executionId})`,
);
await executionRepository.updateStatus(executionId, 'running');

const workflowOwner = await Container.get(OwnershipService).getWorkflowOwnerCached(workflowId);

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,11 +298,15 @@ 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
// are resumed after waiting for some time, as a new startedAt is set)
const { id, data, workflowData, startedAt, ...executionInformation } = execution;
const { id, data, workflowId, workflowData, startedAt, ...executionInformation } = execution;
if (Object.keys(executionInformation).length > 0) {
await this.update({ id: executionId }, executionInformation);
}
Expand Down

0 comments on commit 9bdb85c

Please sign in to comment.