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:
@@ -1,9 +1,14 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Prisma, Event } from "@prisma/client";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import type { CreateEventDto, UpdateEventDto, QueryEventsDto } from "./dto";
|
||||
|
||||
type EventWithRelations = Event & {
|
||||
creator: { id: string; name: string; email: string };
|
||||
project: { id: string; name: string; color: string | null } | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Service for managing events
|
||||
*/
|
||||
@@ -17,7 +22,11 @@ export class EventsService {
|
||||
/**
|
||||
* Create a new event
|
||||
*/
|
||||
async create(workspaceId: string, userId: string, createEventDto: CreateEventDto) {
|
||||
async create(
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
createEventDto: CreateEventDto
|
||||
): Promise<EventWithRelations> {
|
||||
const projectConnection = createEventDto.projectId
|
||||
? { connect: { id: createEventDto.projectId } }
|
||||
: undefined;
|
||||
@@ -60,7 +69,15 @@ export class EventsService {
|
||||
/**
|
||||
* Get paginated events with filters
|
||||
*/
|
||||
async findAll(query: QueryEventsDto) {
|
||||
async findAll(query: QueryEventsDto): Promise<{
|
||||
data: EventWithRelations[];
|
||||
meta: {
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
};
|
||||
}> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 50;
|
||||
const skip = (page - 1) * limit;
|
||||
@@ -125,7 +142,7 @@ export class EventsService {
|
||||
/**
|
||||
* Get a single event by ID
|
||||
*/
|
||||
async findOne(id: string, workspaceId: string) {
|
||||
async findOne(id: string, workspaceId: string): Promise<EventWithRelations> {
|
||||
const event = await this.prisma.event.findUnique({
|
||||
where: {
|
||||
id,
|
||||
@@ -151,7 +168,12 @@ export class EventsService {
|
||||
/**
|
||||
* Update an event
|
||||
*/
|
||||
async update(id: string, workspaceId: string, userId: string, updateEventDto: UpdateEventDto) {
|
||||
async update(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
updateEventDto: UpdateEventDto
|
||||
): Promise<EventWithRelations> {
|
||||
// Verify event exists
|
||||
const existingEvent = await this.prisma.event.findUnique({
|
||||
where: { id, workspaceId },
|
||||
@@ -208,7 +230,7 @@ export class EventsService {
|
||||
/**
|
||||
* Delete an event
|
||||
*/
|
||||
async remove(id: string, workspaceId: string, userId: string) {
|
||||
async remove(id: string, workspaceId: string, userId: string): Promise<void> {
|
||||
// Verify event exists
|
||||
const event = await this.prisma.event.findUnique({
|
||||
where: { id, workspaceId },
|
||||
|
||||
Reference in New Issue
Block a user