Skip to content

Commit dfa5507

Browse files
committed
fix: only update execution success if all iterations succeed
1 parent 06a878b commit dfa5507

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/backend/src/services/action.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ async function enqueueFirstForEachStep({
4949
flowId: flowId,
5050
executionId: executionId,
5151
stepId: firstStepInForEach.id,
52-
metadata: { ...metadata, iteration: i + 1 },
52+
metadata: {
53+
...metadata,
54+
iteration: i + 1,
55+
...(i === iterations - 1 && { isLastIteration: true }),
56+
},
5357
},
5458
jobOptions: {
5559
...DEFAULT_JOB_OPTIONS,
@@ -200,7 +204,7 @@ export const processAction = async (options: ProcessActionOptions) => {
200204
const dataOut = $.actionOutput.data?.raw ?? null
201205
const iterations = Math.min(
202206
FOR_EACH_MAX_ITERATIONS,
203-
Number(dataOut.iterations ?? 0),
207+
Number(dataOut?.iterations ?? 0),
204208
)
205209

206210
// NOTE: unlikely that iterations will be negative, but just in case

packages/backend/src/workers/helpers/make-action-worker.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import {
66
type WorkerProOptions,
77
} from '@taskforcesh/bullmq-pro'
88

9+
import {
10+
TOOLBOX_ACTIONS,
11+
TOOLBOX_APP_KEY,
12+
} from '@/apps/toolbox/common/constants'
913
import appConfig from '@/config/app'
1014
import { createRedisClient } from '@/config/redis'
1115
import { WORKER_CONCURRENCY } from '@/config/workers'
@@ -23,6 +27,7 @@ import {
2327
import logger from '@/helpers/logger'
2428
import tracer from '@/helpers/tracer'
2529
import Execution from '@/models/execution'
30+
import ExecutionStep from '@/models/execution-step'
2631
import Flow from '@/models/flow'
2732
import Step from '@/models/step'
2833
import { enqueueActionJob, makeActionJobId } from '@/queues/action'
@@ -157,7 +162,30 @@ export function makeActionWorker(
157162
})
158163
}
159164

160-
if (!nextStep) {
165+
/**
166+
* FOR-EACH SPECIAL CASE
167+
* 1. nextStep is null for the for-each execution step
168+
* 2. execution with for-each is only a success if all iterations are a success
169+
*/
170+
const isForEach =
171+
currStep.appKey === TOOLBOX_APP_KEY &&
172+
currStep.key === TOOLBOX_ACTIONS.FOR_EACH
173+
174+
if (!nextStep && !isForEach) {
175+
if (nextStepMetadata?.isLastIteration) {
176+
const executionSteps = await ExecutionStep.query().where({
177+
execution_id: executionId,
178+
})
179+
180+
const areAllStepsSuccessful = executionSteps.every(
181+
(step) => step.status === 'success',
182+
)
183+
if (!areAllStepsSuccessful) {
184+
await Execution.setStatus(executionId, 'failure')
185+
return
186+
}
187+
}
188+
161189
await Execution.setStatus(executionId, 'success')
162190
return
163191
}

0 commit comments

Comments
 (0)