-
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.
Remove the singleton in favor of bean injecting the inngest config
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
Showing
6 changed files
with
124 additions
and
88 deletions.
There are no files selected for viewing
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
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
22 changes: 22 additions & 0 deletions
22
inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestConfiguration.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,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); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoConfiguration.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,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"); | ||
} | ||
} | ||
|
85 changes: 0 additions & 85 deletions
85
inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.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,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"); | ||
} | ||
} |