Skip to content

Commit 83fb471

Browse files
committed
fix(workflows): make execution claims race-safe
1 parent 1e05212 commit 83fb471

11 files changed

Lines changed: 674 additions & 228 deletions

File tree

apps/api/src/controllers/__tests__/Actions.track-durability.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ describe('POST /v1/track durability', () => {
115115
triggerConfig: {eventName: 'shipment.created'},
116116
});
117117
const completeStep = vi
118-
.spyOn(servicePrisma.workflowStepExecution, 'update')
118+
.spyOn(servicePrisma.workflowStepExecution, 'updateMany')
119119
.mockRejectedValueOnce(new Error('injected failure after workflow enrollment'));
120120

121121
const response = await request(createTrackApp(projectId))
@@ -456,7 +456,9 @@ describe('POST /v1/track durability', () => {
456456
if (
457457
args.where?.workflowId === workflow.id &&
458458
args.where?.contactId === contact.id &&
459-
args.where?.status === 'RUNNING'
459+
args.where?.status &&
460+
typeof args.where.status === 'object' &&
461+
'in' in args.where.status
460462
) {
461463
return null;
462464
}

apps/api/src/services/EventService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,16 +695,16 @@ export class EventService {
695695
return;
696696
}
697697
} else {
698-
// If re-entry is allowed, only check if there's a currently RUNNING execution
699-
const runningExecution = await prisma.workflowExecution.findFirst({
698+
// Re-entry allows a later run, not overlap with a delay or event wait.
699+
const activeExecution = await prisma.workflowExecution.findFirst({
700700
where: {
701701
workflowId,
702702
contactId,
703-
status: 'RUNNING',
703+
status: {in: ['RUNNING', 'WAITING']},
704704
},
705705
});
706706

707-
if (runningExecution) {
707+
if (activeExecution) {
708708
return;
709709
}
710710
}

0 commit comments

Comments
 (0)