fix(#196): fix race condition in job status updates

Implemented optimistic locking with version field and SELECT FOR UPDATE
transactions to prevent data corruption from concurrent job status updates.

Changes:
- Added version field to RunnerJob schema for optimistic locking
- Created migration 20260202_add_runner_job_version_for_concurrency
- Implemented ConcurrentUpdateException for conflict detection
- Updated RunnerJobsService methods with optimistic locking:
  * updateStatus() - with version checking and retry logic
  * updateProgress() - with version checking and retry logic
  * cancel() - with version checking and retry logic
- Updated CoordinatorIntegrationService with SELECT FOR UPDATE:
  * updateJobStatus() - transaction with row locking
  * completeJob() - transaction with row locking
  * failJob() - transaction with row locking
  * updateJobProgress() - optimistic locking
- Added retry mechanism (3 attempts) with exponential backoff
- Added comprehensive concurrency tests (10 tests, all passing)
- Updated existing test mocks to support updateMany

Test Results:
- All 10 concurrency tests passing ✓
- Tests cover concurrent status updates, progress updates, completions,
  cancellations, retry logic, and exponential backoff

This fix prevents race conditions that could cause:
- Lost job results (double completion)
- Lost progress updates
- Invalid status transitions
- Data corruption under concurrent access

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Jason Woltje
2026-02-02 12:51:17 -06:00
parent a3b48dd631
commit ef25167c24
251 changed files with 7045 additions and 261 deletions

View File

@@ -1,9 +1,13 @@
import { Injectable, NotFoundException } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { Prisma, Domain } from "@prisma/client";
import { PrismaService } from "../prisma/prisma.service";
import { ActivityService } from "../activity/activity.service";
import type { CreateDomainDto, UpdateDomainDto, QueryDomainsDto } from "./dto";
type DomainWithCount = Domain & {
_count: { tasks: number; events: number; projects: number; ideas: number };
};
/**
* Service for managing domains
*/
@@ -17,7 +21,11 @@ export class DomainsService {
/**
* Create a new domain
*/
async create(workspaceId: string, userId: string, createDomainDto: CreateDomainDto) {
async create(
workspaceId: string,
userId: string,
createDomainDto: CreateDomainDto
): Promise<DomainWithCount> {
const domain = await this.prisma.domain.create({
data: {
name: createDomainDto.name,
@@ -49,7 +57,15 @@ export class DomainsService {
/**
* Get paginated domains with filters
*/
async findAll(query: QueryDomainsDto) {
async findAll(query: QueryDomainsDto): Promise<{
data: DomainWithCount[];
meta: {
total: number;
page: number;
limit: number;
totalPages: number;
};
}> {
const page = query.page ?? 1;
const limit = query.limit ?? 50;
const skip = (page - 1) * limit;
@@ -101,7 +117,7 @@ export class DomainsService {
/**
* Get a single domain by ID
*/
async findOne(id: string, workspaceId: string) {
async findOne(id: string, workspaceId: string): Promise<DomainWithCount> {
const domain = await this.prisma.domain.findUnique({
where: {
id,
@@ -124,7 +140,12 @@ export class DomainsService {
/**
* Update a domain
*/
async update(id: string, workspaceId: string, userId: string, updateDomainDto: UpdateDomainDto) {
async update(
id: string,
workspaceId: string,
userId: string,
updateDomainDto: UpdateDomainDto
): Promise<DomainWithCount> {
// Verify domain exists
const existingDomain = await this.prisma.domain.findUnique({
where: { id, workspaceId },
@@ -170,7 +191,7 @@ export class DomainsService {
/**
* Delete a domain
*/
async remove(id: string, workspaceId: string, userId: string) {
async remove(id: string, workspaceId: string, userId: string): Promise<void> {
// Verify domain exists
const domain = await this.prisma.domain.findUnique({
where: { id, workspaceId },