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 { CommentReadModel } from '@task-stream/api/tracker/domain';
import { AddCommentCommand } from './commands/add-comment.command';
import { CommandResult } from './commands/command-result';
@@ -12,7 +13,7 @@ import { GetCommentsQuery } from './queries/get-comments.query';
@Injectable()
export class CommentsService {
constructor(
private readonly commandBus: CommandBus,
private readonly commands: CommandRunner,
private readonly queryBus: QueryBus,
) {}
@@ -22,7 +23,7 @@ export class CommentsService {
body: string,
replyTo: string | null = null,
): Promise<CommandResult> {
return this.commandBus.execute(
return this.commands.execute(
new AddCommentCommand(actorId, issueId, body, replyTo),
);
}
@@ -32,7 +33,7 @@ export class CommentsService {
commentId: string,
body: string,
): Promise<CommandResult> {
return this.commandBus.execute(
return this.commands.execute(
new EditCommentCommand(actorId, commentId, body),
);
}
@@ -44,15 +45,13 @@ export class CommentsService {
emoji: string,
on: boolean,
): Promise<CommandResult> {
return this.commandBus.execute(
return this.commands.execute(
new ReactToCommentCommand(actorId, commentId, emoji, on),
);
}
delete(actorId: string, commentId: string): Promise<CommandResult> {
return this.commandBus.execute(
new DeleteCommentCommand(actorId, commentId),
);
return this.commands.execute(new DeleteCommentCommand(actorId, commentId));
}
list(