Skip to content

Commit

Permalink
Add a second follow up function to locally test sendEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
KiKoS0 committed Feb 24, 2024
1 parent 9fbe38e commit 007eb3a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionContext, Step, HashMap<String, String>> handler = (ctx, step) -> {
int x = 10;
Expand All @@ -39,14 +45,30 @@ public static synchronized CommHandler getInstance() {

step.run("last-step", () -> (res != null ? res.sum : 0) * add, Integer.class);

HashMap<String, String> data = new HashMap<String, String>() {{
put("hello", "world");
}};
step.sendEvent("followup-event-id", new InngestEvent(followUpEvent, data));

return new HashMap<String, String>() {{
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<FunctionContext, Step, LinkedHashMap<String, Object>> followupHandler = (ctx, step) -> {
System.out.println("-> follow up handler called " + ctx.getEvent().getName());
return ctx.getEvent().getData();
};

HashMap<String, InngestFunction> 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);
}
Expand Down
25 changes: 20 additions & 5 deletions inngest-test-server/src/main/kotlin/com/inngest/App.kt
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -56,6 +53,8 @@ data class Result(
val sum: Int,
)

const val followUpEvent = "user.signup.completed"

val fn =
InngestFunction(
FunctionOptions(
Expand Down Expand Up @@ -92,10 +91,26 @@ 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
Expand Down

0 comments on commit 007eb3a

Please sign in to comment.