Skip to content

Commit

Permalink
Implement two example functions demonstrating try-catching
Browse files Browse the repository at this point in the history
  • Loading branch information
KiKoS0 committed Aug 29, 2024
1 parent 7862bf9 commit f997ad1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ fun Application.module() {
RestoreFromGlacier(),
ProcessUserSignup(),
TranscodeVideo(),
ImageFromPrompt(),
PushToSlackChannel(),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.inngest.testserver

import com.inngest.*

class ImageFromPrompt : InngestFunction() {
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder =
builder
.id("ImageFromPrompt")
.name("Image from Prompt")
.triggerEvent("media/prompt.created")

override fun execute(
ctx: FunctionContext,
step: Step,
): String {
val imageURL =
try {
step.run("generate-image-dall-e") {
// Call the DALL-E model to generate an image
throw Exception("Failed to generate image")

"example.com/image-dall-e.jpg"
}
} catch (e: StepError) {
// Fall back to a different image generation model
step.run("generate-image-midjourney") {
// Call the MidJourney model to generate an image
"example.com/image-midjourney.jpg"
}
}

try {
step.invoke<Map<String, Any>>(
"push-to-slack-channel",
"ktor-dev",
"PushToSlackChannel",
mapOf("image" to imageURL),
null,
)
} catch (e: StepError) {
// Pushing to Slack is not critical, so we can ignore the error, log it
// or handle it in some other way.
}

return imageURL
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.inngest.testserver

import com.inngest.*

class PushToSlackChannel : InngestFunction() {
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder =
builder
.id("PushToSlackChannel")
.name("Push to Slack Channel")
.triggerEvent("media/image.generated")

override fun execute(
ctx: FunctionContext,
step: Step,
): String =
step.run("push-to-slack-channel") {
// Call Slack API to push the image to a channel
throw NonRetriableError("Failed to push image to Slack channel ${ctx.event.data["image"]}")

"Image pushed to Slack channel"
}
}

0 comments on commit f997ad1

Please sign in to comment.