Skip to content

Commit

Permalink
INN-3321 - Config options builder (#54)
Browse files Browse the repository at this point in the history
* Config options builder WIP

* Fix builder json serialization

* Get serve url and app id passed through to builder

* Add event id, batch demo

* Improve example

* Refactor builder class

* Cleanup add tests

* Add concurrency scope and enum converter

* Add config builder test

* Update to use config builder.

* Fix linter warnings

* Update README
  • Loading branch information
djfarrelly authored Jul 22, 2024
1 parent 85e258a commit 6d13104
Show file tree
Hide file tree
Showing 41 changed files with 825 additions and 377 deletions.
81 changes: 52 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@
<summary>Kotlin</summary>

```kotlin
import com.inngest.InngestFunction
import com.inngest.FunctionOptions
import com.inngest.FunctionTrigger

val myFunction = InngestFunction(
FunctionOptions(
id = "fn-id-slug",
name = "My function!",
triggers = arrayOf(FunctionTrigger(event = "user.signup")),
),
) { ctx, step ->
val x = 10

val res =
step.run<Int>("add-ten") { ->
x + 10
}
val add: Int =
step.run("multiply-by-100") {
res * 100
}
step.sleep("wait-one-minute", Duration.ofSeconds(60))

step.run("last-step") { res * add }

hashMapOf("message" to "success")
class TranscodeVideo : InngestFunction() {
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder =
builder
.id("process-video")
.name("Process video upload")
.triggerEvent("media/video.uploaded")
.concurrency(10)

override fun execute(
ctx: FunctionContext,
step: Step,
): HashMap<String, Any> {
val transcription =
step.run("transcribe-video") {
// Download video, run through transcription model, return output
"Hi there, My name is Jamie..." // dummy example content
}

val summary =
step.run("summarize") {
// Send t
"Hi there, My name is Jamie..." // dummy example content
}

step.run("save-results") {
// Save summary, to your database
// database.save(event.data["videoId"], transcription, summary)
}

return hashMapOf("restored" to false)
}
}
```

Expand All @@ -43,9 +48,28 @@ val myFunction = InngestFunction(
<summary>Java (Coming soon)</summary>
</details>

## Declaring dependencies
## Defining configuration

WIP
Define your function's configuration using the `config` method and the `InngestFunctionConfigBuilder` class.
The `config` method must be overridden and an `id` is required. All options should are discoverable via
the builder class passed as the only argument to the `config` method.

<details open>
<summary>Kotlin</summary>

```kotlin
class TranscodeVideo : InngestFunction() {
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder =
builder
.id("process-video")
.name("Process video upload")
.triggerEvent("media/video.uploaded")
.concurrency(10)

}
```

</details>

## Contributing [WIP]

Expand All @@ -57,7 +81,6 @@ make dev-ktor

This runs a `ktor` web server to test the SDK against the dev server.


To run the `spring-boot` test server:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

import java.util.LinkedHashMap;

@FunctionConfig(
id = "fn-follow-up",
name = "My follow up function!"
)
@FunctionEventTrigger(event = "user.signup.completed")
@FunctionEventTrigger(event = "random-event")
public class FollowupFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("fn-follow-up")
.name("My follow up function!")
.triggerEvent("user.signup.completed")
.triggerEvent("random-event");
}

@Override
public LinkedHashMap<String, Object> execute(@NotNull FunctionContext ctx, @NotNull Step step) {
System.out.println("-> follow up handler called " + ctx.getEvent().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@
import java.time.Duration;
import java.util.HashMap;

@FunctionConfig(id = "fn-id-slug", name = "My function!")
@FunctionEventTrigger(event = "user-signup")
public class UserSignupFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("fn-id-slug")
.name("My Function!")
.triggerEvent("user-signup");
}

@Override
public HashMap<String, String> execute(@NotNull FunctionContext ctx, @NotNull Step step) {
int x = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

@FunctionConfig(id = "custom-result-fn", name = "Custom Result Function")
@FunctionEventTrigger(event = "test/custom.result.step")
public class CustomStepFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("custom-result-fn")
.name("Custom Result Function")
.triggerEvent("test/custom.result.step");
}

private final int count = 0;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

@FunctionConfig(id = "no-step-fn", name = "No Step Function")
@FunctionEventTrigger(event = "test/no-step")
public class EmptyStepFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("no-step-fn")
.name("No Step Function")
.triggerEvent("test/no-step");
}

@Override
public String execute(FunctionContext ctx, Step step) {
return "hello world";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

@FunctionConfig(id = "non-retriable-fn", name = "NonRetriable Function")
@FunctionEventTrigger(event = "test/non.retriable")
public class NonRetriableErrorFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("non-retriable-fn")
.name("NonRetriable Function")
.triggerEvent("test/non.retriable");
}

@Override
public String execute(FunctionContext ctx, Step step) {
step.run("fail-step", () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

@FunctionConfig(id = "retriable-fn", name = "Retriable Function")
@FunctionEventTrigger(event = "test/retriable")
public class RetriableErrorFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("retriable-fn")
.name("Retriable Function")
.triggerEvent("test/retriable");
}

static int retryCount = 0;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;

@FunctionConfig(id = "send-fn", name = "Send Function")
@FunctionEventTrigger(event = "test/send")
public class SendEventFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("send-fn")
.name("Send Function")
.triggerEvent("test/send");
}

@Override
public SendEventsResponse execute(FunctionContext ctx, Step step) {
return step.sendEvent("send-test", new InngestEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

import java.time.Duration;

@FunctionConfig(id = "sleep-fn", name = "Sleep Function")
@FunctionEventTrigger(event = "test/sleep")
public class SleepStepFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("sleep-fn")
.name("Sleep Function")
.triggerEvent("test/sleep");
}

@Override
public Integer execute(FunctionContext ctx, Step step) {
int result = step.run("num", () -> 42, Integer.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

@FunctionConfig(id = "two-steps-fn", name = "Two Steps Function")
@FunctionEventTrigger(event = "test/two.steps")
public class TwoStepsFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("two-steps-fn")
.name("Two Steps Function")
.triggerEvent("test/two.steps");
}


private final int count = 0;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

@FunctionConfig(id = "wait-for-event-fn", name = "Wait for Event Function")
@FunctionEventTrigger(event = "test/wait-for-event")
public class WaitForEventFunction extends InngestFunction {

@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("wait-for-event-fn")
.name("Wait for Event Function")
.triggerEvent("test/wait-for-event");
}

@Override
public String execute(FunctionContext ctx, Step step) {
Object event = step.waitForEvent("wait-test",
Expand Down
16 changes: 11 additions & 5 deletions inngest-test-server/src/main/kotlin/com/inngest/testserver/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ data class Result(

const val FOLLOW_UP_EVENT_NAME = "user.signup.completed"

data class IngestData(val message: String)

fun Application.module() {
val inngest = Inngest(appId = "ktor-dev")

routing {
serve("/api/inngest", inngest, listOf(ProcessAlbum(), RestoreFromGlacier()))
serve(
"/api/inngest", inngest,
listOf(
ProcessAlbum(),
RestoreFromGlacier(),
ProcessUserSignup(),
TranscodeVideo(),
),
)
}
}

fun main() {
var port = 8080
val port = 8080

println("Test server running on port " + port)
println("Test server running on port $port")

embeddedServer(
Netty,
Expand Down

This file was deleted.

Loading

0 comments on commit 6d13104

Please sign in to comment.