Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Make execution and its data creation atomic #10276

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/cli/src/ActiveExecutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ExecutionCancelledError,
sleep,
} from 'n8n-workflow';
import { strict as assert } from 'node:assert';

import type {
ExecutionPayload,
Expand Down Expand Up @@ -74,9 +75,7 @@ export class ActiveExecutions {
}

executionId = await this.executionRepository.createNewExecution(fullExecutionData);
if (executionId === undefined) {
throw new ApplicationError('There was an issue assigning an execution id to the execution');
}
Comment on lines -77 to -79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check has never made sense. the only reason the types here suggest that executionId could be missing is because we are reusing the variable. This code will never run, since if creating an execution fails, we'll have an unhandled error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. That's why I changed it to an assert so it satisfies the type system

assert(executionId);

await this.concurrencyControl.throttle({ mode, executionId });
executionStatus = 'running';
Expand Down
28 changes: 19 additions & 9 deletions packages/cli/src/databases/repositories/execution.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,27 @@ export class ExecutionRepository extends Repository<ExecutionEntity> {
return rest;
}

/**
* Insert a new execution and its execution data using a transaction.
*/
async createNewExecution(execution: ExecutionPayload): Promise<string> {
const { data, workflowData, ...rest } = execution;
const { identifiers: inserted } = await this.insert(rest);
const { id: executionId } = inserted[0] as { id: string };
const { connections, nodes, name, settings } = workflowData ?? {};
await this.executionDataRepository.insert({
executionId,
workflowData: { connections, nodes, name, settings, id: workflowData.id },
data: stringify(data),
return await this.manager.transaction(async (transactionManager) => {
const { data, workflowData, ...rest } = execution;
const insertResult = await transactionManager.insert(ExecutionEntity, rest);
const { id: executionId } = insertResult.identifiers[0] as { id: string };

const { connections, nodes, name, settings } = workflowData ?? {};
await this.executionDataRepository.createExecutionDataForExecution(
{
executionId,
workflowData: { connections, nodes, name, settings, id: workflowData.id },
data: stringify(data),
},
transactionManager,
);

return String(executionId);
});
return String(executionId);
}

async markAsCrashed(executionIds: string | string[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import { Service } from 'typedi';
import type { EntityManager } from '@n8n/typeorm';
import type { IWorkflowBase } from 'n8n-workflow';
import { DataSource, In, Repository } from '@n8n/typeorm';
import { ExecutionData } from '../entities/ExecutionData';

export interface CreateExecutionDataOpts extends Pick<ExecutionData, 'data' | 'executionId'> {
workflowData: Pick<IWorkflowBase, 'connections' | 'nodes' | 'name' | 'settings' | 'id'>;
}

@Service()
export class ExecutionDataRepository extends Repository<ExecutionData> {
constructor(dataSource: DataSource) {
super(ExecutionData, dataSource.manager);
}

async createExecutionDataForExecution(
executionData: CreateExecutionDataOpts,
transactionManager: EntityManager,
) {
const { data, executionId, workflowData } = executionData;

return await transactionManager.insert(ExecutionData, {
executionId,
data,
workflowData,
});
}

async findByExecutionIds(executionIds: string[]) {
return await this.find({
select: ['workflowData'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,34 @@ describe('ExecutionRepository', () => {
});
expect(executionData?.data).toEqual('[{"resultData":"1"},{}]');
});

it('should not create execution if execution data insert fails', async () => {
const executionRepo = Container.get(ExecutionRepository);
const executionDataRepo = Container.get(ExecutionDataRepository);

const workflow = await createWorkflow({ settings: { executionOrder: 'v1' } });
jest
.spyOn(executionDataRepo, 'createExecutionDataForExecution')
.mockRejectedValueOnce(new Error());

await expect(
async () =>
await executionRepo.createNewExecution({
workflowId: workflow.id,
data: {
//@ts-expect-error This is not needed for tests
resultData: {},
},
workflowData: workflow,
mode: 'manual',
startedAt: new Date(),
status: 'new',
finished: false,
}),
).rejects.toThrow();

const executionEntities = await executionRepo.find();
expect(executionEntities).toBeEmptyArray();
});
});
});
Loading