Skip to content

Commit

Permalink
Remove the singleton in favor of bean injecting the inngest config
Browse files Browse the repository at this point in the history
I think someone with better spring boot knowledge can shape
a better interface to create the inngest client + CommHandler but
this is already better than manually creating a singleton and
having everything in a random controller.

Note: It might be better to not have a second adapter package and
instead have these spring boot helpers as a feature variant. But
I'll see how it goes with this PR first:

#27

I'm also adding a way to build the Inngest client configuration
possibly through a Builder pattern targeted to the specific
environment that the user wants to use.
  • Loading branch information
KiKoS0 committed Feb 25, 2024
1 parent 0b7d86c commit fec731e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 88 deletions.
2 changes: 1 addition & 1 deletion inngest-core/src/main/kotlin/com/inngest/Comm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ data class CommError(

val jsonMediaType = "application/json".toMediaType()

class CommHandler(val functions: HashMap<String, InngestFunction>) {
class CommHandler(val functions: HashMap<String, InngestFunction>, val client: Inngest? = null) {
private fun getHeaders(): Map<String, String> {
return mapOf(
"Content-Type" to "application/json",
Expand Down
2 changes: 0 additions & 2 deletions inngest-core/src/main/kotlin/com/inngest/Inngest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.inngest

// import okhttp3.RequestBody.Companion.toRequestBody

class Inngest {
constructor(
app_id: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.inngest.springboot;

import com.inngest.CommHandler;
import com.inngest.Inngest;
import com.inngest.InngestFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;

public abstract class InngestConfiguration {
protected abstract HashMap<String, InngestFunction> functions();

@Bean
protected abstract Inngest inngestClient();


@Bean
protected CommHandler commHandler(@Autowired Inngest inngestClient) {
return new CommHandler(functions(), inngestClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.inngest.springbootdemo;


import com.inngest.*;
import com.inngest.springboot.InngestConfiguration;
import kotlin.jvm.functions.Function2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.time.Duration;
import java.util.HashMap;

@Configuration
public class DemoConfiguration extends InngestConfiguration {

@Override
public HashMap<String, InngestFunction> functions() {
String followUpEvent = "user.signup.completed";
FunctionTrigger fnTrigger = new FunctionTrigger("user-signup", null, null);
FunctionTrigger[] triggers = {fnTrigger};
FunctionOptions fnConfig = new FunctionOptions("fn-id-slug", "My function!", triggers);

Function2<FunctionContext, Step, HashMap<String, String>> handler = (ctx, step) -> {
int x = 10;

System.out.println("-> handler called " + ctx.getEvent().getName());

int y = step.run("add-ten", () -> x + 10, Integer.class);

Result res = step.run("cast-to-type-add-ten", () -> {
System.out.println("-> running step 1!! " + x);
return new Result(y + 10);
}, Result.class);

System.out.println("res" + res);

step.waitForEvent("wait-for-hello", "hello", "10m", null);

int add = step.run("add-one-hundred", () -> {
System.out.println("-> running step 2 :) " + (res != null ? res.sum : ""));
return (res != null ? res.sum : 0) + 100;
}, Integer.class);

step.sleep("wait-one-sec", Duration.ofSeconds(2));

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");
}};
};

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", new InngestFunction(fnConfig, handler));
functions.put("fn-follow-up", new InngestFunction(followupFnConfig, followupHandler));

return functions;
}

@Bean
public Inngest inngestClient() {
return new Inngest("spring_demo");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.inngest.springbootdemo;

import com.inngest.Inngest;
import com.inngest.InngestFunction;
import com.inngest.springboot.InngestConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;

public class DemoTestConfiguration extends InngestConfiguration {
protected HashMap<String, InngestFunction> functions() {
return new HashMap<>();
}

@Bean
protected Inngest inngestClient() {
return new Inngest("spring_test_demo");
}
}

0 comments on commit fec731e

Please sign in to comment.