Skip to content

Commit

Permalink
Handle edge case where user reuses step with stepNumber after it was …
Browse files Browse the repository at this point in the history
…already allocated in loop
  • Loading branch information
albertchae committed Sep 12, 2024
1 parent 43373c8 commit 1fd5308
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder)
public Integer execute(FunctionContext ctx, Step step) {
int runningCount = 10;

// explicitly naming a step that the SDK will try to use in the loop shouldn't break the loop
int effectivelyFinalVariableForLambda1 = runningCount;
runningCount = step.run("add-num:3", () -> effectivelyFinalVariableForLambda1 + 50, Integer.class);

for (int i = 0; i < 5; i++) {
int effectivelyFinalVariableForLambda2 = runningCount;
// The actual stepIds used will be add-num, add-num:1, add-num:2, add-num:4, add-num:5
runningCount = step.run("add-num", () -> effectivelyFinalVariableForLambda2 + 10, Integer.class);
}

// explicitly reusing step names that the SDK used during the loop should both execute
// These will be modified to add-num:4:1 and add-num:4:2 respectively
int effectivelyFinalVariableForLambda3 = runningCount;
runningCount = step.run("add-num:4", () -> effectivelyFinalVariableForLambda3 + 30, Integer.class);
int effectivelyFinalVariableForLambda4 = runningCount;
runningCount = step.run("add-num:4", () -> effectivelyFinalVariableForLambda4 + 30, Integer.class);

return runningCount;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ void testStepsInLoopExecuteCorrectly() throws Exception {
RunEntry<Object> loopRun = devServer.runsByEvent(loopEvent).first();
assertEquals("Completed", loopRun.getStatus());

assertEquals(110, loopRun.getOutput());
assertEquals(170, loopRun.getOutput());
}
}
9 changes: 4 additions & 5 deletions inngest/src/main/kotlin/com/inngest/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ class State(
}

private fun findNextAvailableStepId(id: String): String {
if (id !in stepIdsToNextStepNumber) {
stepIdsToNextStepNumber[id] = 1
if (id !in stepIds) {
return id
}

// start with the seen count so far for current stepId
// but loop over all seen stepIds to make sure a user didn't explicitly define
// a step using the same step number
var stepNumber = stepIdsToNextStepNumber[id]
var stepNumber = stepIdsToNextStepNumber.getOrDefault(id, 1)
while ("$id:$stepNumber" in stepIds) {
stepNumber = stepNumber!! + 1
stepNumber = stepNumber + 1
}
// now we know stepNumber is unused and can be used for the current stepId
// save stepNumber + 1 to the hash for next time
stepIdsToNextStepNumber[id] = stepNumber!! + 1
stepIdsToNextStepNumber[id] = stepNumber + 1

return "$id:$stepNumber"
}
Expand Down

0 comments on commit 1fd5308

Please sign in to comment.