diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java index bab481b2..757747ce 100644 --- a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java @@ -5,17 +5,23 @@ import java.time.Duration; import java.util.HashMap; +import java.util.LinkedHashMap; // NOTE: We probably don't need this singleton anymore // when revisiting the SDK's interface. public class InngestSingleton { private static CommHandler instance; + private static final String followUpEvent = "user.signup.completed"; + public static synchronized CommHandler getInstance() { if (instance == null) { FunctionTrigger fnTrigger = new FunctionTrigger("user-signup", null, null); - FunctionTrigger[] triggers = {fnTrigger}; - FunctionOptions fnConfig = new FunctionOptions("fn-id-slug", "My function!", triggers); + FunctionOptions fnConfig = new FunctionOptions( + "fn-id-slug", + "My function!", + new FunctionTrigger[]{fnTrigger} + ); Function2> handler = (ctx, step) -> { int x = 10; @@ -39,14 +45,30 @@ public static synchronized CommHandler getInstance() { step.run("last-step", () -> (res != null ? res.sum : 0) * add, Integer.class); + HashMap data = new HashMap() {{ + put("hello", "world"); + }}; + step.sendEvent("followup-event-id", new InngestEvent(followUpEvent, data)); + return new HashMap() {{ put("message", "cool - this finished running"); }}; }; - InngestFunction function = new InngestFunction(fnConfig, handler); + + FunctionTrigger followupFnTrigger = new FunctionTrigger(followUpEvent, null, null); + FunctionOptions followupFnConfig = new FunctionOptions( + "fn-follow-up", + "Follow up function!", + new FunctionTrigger[]{followupFnTrigger} + ); + Function2> followupHandler = (ctx, step) -> { + System.out.println("-> follow up handler called " + ctx.getEvent().getName()); + return ctx.getEvent().getData(); + }; HashMap functions = new HashMap<>(); - functions.put("fn-id-slug", function); + functions.put("fn-id-slug", new InngestFunction(fnConfig, handler)); + functions.put("fn-follow-up", new InngestFunction(followupFnConfig, followupHandler)); instance = new CommHandler(functions); } diff --git a/inngest-test-server/src/main/kotlin/com/inngest/App.kt b/inngest-test-server/src/main/kotlin/com/inngest/App.kt index 41c9e352..99c89ee1 100644 --- a/inngest-test-server/src/main/kotlin/com/inngest/App.kt +++ b/inngest-test-server/src/main/kotlin/com/inngest/App.kt @@ -1,10 +1,7 @@ package com.inngest.testserver import com.fasterxml.jackson.annotation.JsonProperty -import com.inngest.CommHandler -import com.inngest.FunctionOptions -import com.inngest.FunctionTrigger -import com.inngest.InngestFunction +import com.inngest.* import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.engine.* @@ -56,6 +53,8 @@ data class Result( val sum: Int, ) +const val followUpEvent = "user.signup.completed" + val fn = InngestFunction( FunctionOptions( @@ -92,10 +91,25 @@ val fn = step.sleep("wait-one-sec", Duration.ofSeconds(2)) step.run("last-step") { res.sum.times(add) ?: 0 } + + step.sendEvent("followup-event-id", InngestEvent(followUpEvent, data = hashMapOf("hello" to "world"))) + hashMapOf("message" to "cool - this finished running") } +val fn2 = + InngestFunction( + FunctionOptions( + id = "fn-follow-up", + name = "Follow up function!", + triggers = arrayOf(FunctionTrigger(event = followUpEvent)), + ), + ) { ctx, _ -> + println("-> followup fn called $ctx.event.name") + + ctx.event.data + } -val comm = CommHandler(functions = hashMapOf("fn-id-slug" to fn)) +val comm = CommHandler(functions = hashMapOf("fn-id-slug" to fn, "fn-follow-up" to fn2)) fun main() { var port = 8080