-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Per https://github.com/inngest/inngest/blob/main/docs/SDK_SPEC.md#512-ids-and-hashing, add `:n` starting with `:1` for repeated instances of a step id Refactored to be a combination of golang and inngest-js implementation to handle edge case of user defined stepId colliding - https://github.com/inngest/inngestgo/blob/0a00daba0b2db68ff0f080f787cf63f0a63b44d8/internal/sdkrequest/manager.go#L123-L131 - https://github.com/inngest/inngest-js/blob/79069e1a3d700624ce49b323922c113fc952bcc6/packages/inngest/src/components/execution/v1.ts#L819-L831 * Use combination of hash and loop to find next unused stepId The hash means we don't have to loop from 0 every time and for most cases will just correctly return us the next unused stepId, but looping afterwards guarantees we don't collide with a user defined stepId. So this will be O(1) in most cases and potentially O(n) for pathological functions that have many steps manually named with the `:n` suffix * The inngest-js SDK currently optionally warns of parallel indexing, but this isn't in scope for beta so I left it out.
- Loading branch information
1 parent
2e01336
commit 5faeb64
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/LoopFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.FunctionContext; | ||
import com.inngest.InngestFunction; | ||
import com.inngest.InngestFunctionConfigBuilder; | ||
import com.inngest.Step; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class LoopFunction extends InngestFunction { | ||
|
||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("loop-fn") | ||
.name("Loop Function") | ||
.triggerEvent("test/loop"); | ||
} | ||
|
||
|
||
@Override | ||
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...pring-boot-demo/src/test/java/com/inngest/springbootdemo/LoopFunctionIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.inngest.Inngest; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@IntegrationTest | ||
@Execution(ExecutionMode.CONCURRENT) | ||
class LoopFunctionIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testStepsInLoopExecuteCorrectly() throws Exception { | ||
String loopEvent = InngestFunctionTestHelpers.sendEvent(client, "test/loop").getIds()[0]; | ||
Thread.sleep(2000); | ||
|
||
RunEntry<Object> loopRun = devServer.runsByEvent(loopEvent).first(); | ||
assertEquals("Completed", loopRun.getStatus()); | ||
|
||
assertEquals(170, loopRun.getOutput()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters