fix(api): retry commands that clash with a concurrent write

Services run commands through a CommandRunner that runs one again (up to
5 times, waiting a little longer each time) when its aggregate changed
underneath, e.g. a saga writing to the same ticket. Each command loads its
aggregate fresh and stores its events in one transaction, so a
ConcurrencyError means nothing was stored and running it again is safe.
People no longer get a 409 for it, and the e2e suite stops flaking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-15 14:00:08 +02:00
parent 76312c079d
commit e14f445e6b
23 changed files with 208 additions and 114 deletions

View File

@@ -1,5 +1,6 @@
import { CommandRunner } from '@task-stream/api/shared/application';
import { Injectable } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import { QueryBus } from '@nestjs/cqrs';
import { ForbiddenError } from '@task-stream/api/shared/domain';
import {
AddGitRepositoryCommand,
@@ -30,7 +31,7 @@ export interface GitDelivery {
@Injectable()
export class GitService {
constructor(
private readonly commandBus: CommandBus,
private readonly commands: CommandRunner,
private readonly queryBus: QueryBus,
private readonly findProjects: FindProjectsRepository,
private readonly integrations: GitIntegrationsRepository,
@@ -47,7 +48,7 @@ export class GitService {
projectId: string,
name: string,
): Promise<GitRepository> {
return this.commandBus.execute(
return this.commands.execute(
new AddGitRepositoryCommand(actorId, projectId, name),
);
}
@@ -57,7 +58,7 @@ export class GitService {
projectId: string,
repositoryId: string,
): Promise<GitRepository> {
return this.commandBus.execute(
return this.commands.execute(
new RotateGitRepositorySecretCommand(actorId, projectId, repositoryId),
);
}
@@ -67,7 +68,7 @@ export class GitService {
projectId: string,
repositoryId: string,
): Promise<void> {
return this.commandBus.execute(
return this.commands.execute(
new RemoveGitRepositoryCommand(actorId, projectId, repositoryId),
);
}
@@ -104,7 +105,7 @@ export class GitService {
delivery.body,
);
if (!mentions.length) return { recorded: 0 };
const recorded: number = await this.commandBus.execute(
const recorded: number = await this.commands.execute(
new RecordGitMentionsCommand(project.id, repository.name, mentions),
);
return { recorded };