From 70ac4e372eba98d579ce60969cd0db2933676062 Mon Sep 17 00:00:00 2001 From: albertchae <217050+albertchae@users.noreply.github.com> Date: Mon, 9 Sep 2024 07:03:57 -0700 Subject: [PATCH] Misc README and test-server additions (#79) * Misc README and test-server additions * Add safe navigation for return type of SendEventsResponse? --------- Co-authored-by: Dan Farrelly Co-authored-by: Albert Chae --- README.md | 25 ++++++++++++++++++- .../main/kotlin/com/inngest/testserver/App.kt | 21 ++++++++++++++++ .../com/inngest/testserver/ProcessAlbum.kt | 1 - .../com/inngest/testserver/WeeklyCleanup.kt | 24 ++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 inngest-test-server/src/main/kotlin/com/inngest/testserver/WeeklyCleanup.kt diff --git a/README.md b/README.md index f614b1cf..b37093cd 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ class TranscodeVideo : InngestFunction() { Java (Coming soon) -## Defining configuration +## Creating functions 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 @@ -63,6 +63,8 @@ the builder class passed as the only argument to the `config` method. Kotlin ```kotlin +import java.time.Duration + class TranscodeVideo : InngestFunction() { override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder = builder @@ -70,8 +72,29 @@ class TranscodeVideo : InngestFunction() { .name("Process video upload") .triggerEvent("media/video.uploaded") .retries(2) + .batchEvents(50, Duration.ofSeconds(30)) .concurrency(10) +} +``` + + + +## Sending events (_triggering functions_) + +
+ Kotlin +```kotlin +import java.time.Duration + +class TranscodeVideo : InngestFunction() { + override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder = + builder + .id("process-video") + .name("Process video upload") + .triggerEvent("media/video.uploaded") + .batchEvents(50, Duration.ofSeconds(30)) + .concurrency(10) } ``` diff --git a/inngest-test-server/src/main/kotlin/com/inngest/testserver/App.kt b/inngest-test-server/src/main/kotlin/com/inngest/testserver/App.kt index 30cf8e52..5e947fd1 100644 --- a/inngest-test-server/src/main/kotlin/com/inngest/testserver/App.kt +++ b/inngest-test-server/src/main/kotlin/com/inngest/testserver/App.kt @@ -6,6 +6,7 @@ import com.inngest.ktor.* import io.ktor.server.application.* import io.ktor.server.engine.* import io.ktor.server.netty.* +import io.ktor.server.response.* import io.ktor.server.routing.* data class Result( @@ -28,8 +29,28 @@ fun Application.module() { TranscodeVideo(), ImageFromPrompt(), PushToSlackChannel(), + WeeklyCleanup(), ), ) + get("/send") { + println("Sending event...") + try { + val event = + InngestEvent( + "delivery/process.requested", + mapOf( + "albumId" to "3d345a93-57c0-478b-a8ff-4d5f6a7df3b0", + "title" to "The Teal Album", + ), + ) + val res = inngest.send(event) + println(res) + call.respondText(res?.ids.toString()) + } catch (e: Exception) { + println(e) + call.respondText(e.toString()) + } + } } } diff --git a/inngest-test-server/src/main/kotlin/com/inngest/testserver/ProcessAlbum.kt b/inngest-test-server/src/main/kotlin/com/inngest/testserver/ProcessAlbum.kt index 25274cf1..aaad2c75 100644 --- a/inngest-test-server/src/main/kotlin/com/inngest/testserver/ProcessAlbum.kt +++ b/inngest-test-server/src/main/kotlin/com/inngest/testserver/ProcessAlbum.kt @@ -12,7 +12,6 @@ class ProcessAlbum : InngestFunction() { .id("ProcessAlbum") .name("Process Album!") .triggerEvent("delivery/process.requested") - .trigger(InngestFunctionTriggers.Cron("5 0 * 8 *")) .batchEvents(30, Duration.ofSeconds(10)) override fun execute( diff --git a/inngest-test-server/src/main/kotlin/com/inngest/testserver/WeeklyCleanup.kt b/inngest-test-server/src/main/kotlin/com/inngest/testserver/WeeklyCleanup.kt new file mode 100644 index 00000000..941f9437 --- /dev/null +++ b/inngest-test-server/src/main/kotlin/com/inngest/testserver/WeeklyCleanup.kt @@ -0,0 +1,24 @@ +package com.inngest.testserver + +import com.inngest.* + +/** + * A demo function that runs on a given cron schedule + */ +class WeeklyCleanup : InngestFunction() { + override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder = + builder + .id("weekly-cleanup") + .name("Weekly cleanup") + .triggerCron("0 0 * * 0") // Every Sunday at midnight + + override fun execute( + ctx: FunctionContext, + step: Step, + ): LinkedHashMap { + // this function will not have an event or events to utilize as it's a triggered on a cron schedule + // this is ideal for periodic jobs that don't fit an event-trigger pattern. + + return linkedMapOf("hello" to true) + } +}