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,10 +1,19 @@
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { Prisma, Task } from "@prisma/client";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { ActivityService } from "../activity/activity.service";
|
||||
import { TaskStatus, TaskPriority } from "@prisma/client";
|
||||
import type { CreateTaskDto, UpdateTaskDto, QueryTasksDto } from "./dto";
|
||||
|
||||
type TaskWithRelations = Task & {
|
||||
assignee: { id: string; name: string; email: string } | null;
|
||||
creator: { id: string; name: string; email: string };
|
||||
project: { id: string; name: string; color: string | null } | null;
|
||||
subtasks?: (Task & {
|
||||
assignee: { id: string; name: string; email: string } | null;
|
||||
})[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Service for managing tasks
|
||||
*/
|
||||
@@ -18,7 +27,11 @@ export class TasksService {
|
||||
/**
|
||||
* Create a new task
|
||||
*/
|
||||
async create(workspaceId: string, userId: string, createTaskDto: CreateTaskDto) {
|
||||
async create(
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
createTaskDto: CreateTaskDto
|
||||
): Promise<Omit<TaskWithRelations, "subtasks">> {
|
||||
const assigneeConnection = createTaskDto.assigneeId
|
||||
? { connect: { id: createTaskDto.assigneeId } }
|
||||
: undefined;
|
||||
@@ -79,7 +92,15 @@ export class TasksService {
|
||||
/**
|
||||
* Get paginated tasks with filters
|
||||
*/
|
||||
async findAll(query: QueryTasksDto) {
|
||||
async findAll(query: QueryTasksDto): Promise<{
|
||||
data: Omit<TaskWithRelations, "subtasks">[];
|
||||
meta: {
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
};
|
||||
}> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 50;
|
||||
const skip = (page - 1) * limit;
|
||||
@@ -159,7 +180,7 @@ export class TasksService {
|
||||
/**
|
||||
* Get a single task by ID
|
||||
*/
|
||||
async findOne(id: string, workspaceId: string) {
|
||||
async findOne(id: string, workspaceId: string): Promise<TaskWithRelations> {
|
||||
const task = await this.prisma.task.findUnique({
|
||||
where: {
|
||||
id,
|
||||
@@ -195,7 +216,12 @@ export class TasksService {
|
||||
/**
|
||||
* Update a task
|
||||
*/
|
||||
async update(id: string, workspaceId: string, userId: string, updateTaskDto: UpdateTaskDto) {
|
||||
async update(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
userId: string,
|
||||
updateTaskDto: UpdateTaskDto
|
||||
): Promise<Omit<TaskWithRelations, "subtasks">> {
|
||||
// Verify task exists
|
||||
const existingTask = await this.prisma.task.findUnique({
|
||||
where: { id, workspaceId },
|
||||
@@ -305,7 +331,7 @@ export class TasksService {
|
||||
/**
|
||||
* Delete a task
|
||||
*/
|
||||
async remove(id: string, workspaceId: string, userId: string) {
|
||||
async remove(id: string, workspaceId: string, userId: string): Promise<void> {
|
||||
// Verify task exists
|
||||
const task = await this.prisma.task.findUnique({
|
||||
where: { id, workspaceId },
|
||||
|
||||
Reference in New Issue
Block a user