Skip to content

Commit

Permalink
Misc README and test-server additions (#79)
Browse files Browse the repository at this point in the history
* Misc README and test-server additions

* Add safe navigation for return type of SendEventsResponse?

---------

Co-authored-by: Dan Farrelly <[email protected]>
Co-authored-by: Albert Chae <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent c1b962d commit 70ac4e3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TranscodeVideo : InngestFunction() {
<summary>Java (Coming soon)</summary>
</details>

## 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
Expand All @@ -63,15 +63,38 @@ the builder class passed as the only argument to the `config` method.
<summary>Kotlin</summary>

```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")
.retries(2)
.batchEvents(50, Duration.ofSeconds(30))
.concurrency(10)
}
```

</details>

## Sending events (_triggering functions_)

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

```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)
}
```

Expand Down
21 changes: 21 additions & 0 deletions inngest-test-server/src/main/kotlin/com/inngest/testserver/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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())
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Any> {
// 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)
}
}

0 comments on commit 70ac4e3

Please sign in to comment.