Skip to content

Commit

Permalink
Switch to an overridable onFailure method in InngestFunction
Browse files Browse the repository at this point in the history
This removes the `WithFailureHandler` interface and instead provides an
overridable `onFailure` method in `InngestFunction` that can be used to
define a function to be called when the function fails.

It's used in the same way but without the need to implement an interface

```kotlin

class MyFunction : InngestFunction() {
    override fun execute(ctx: FunctionContext, step: Step): Any? {
        // Do something
    }

    override fun onFailure(ctx: FunctionContext, step: Step): Any? {
        // Handle failure
    }
}

```
  • Loading branch information
KiKoS0 committed Sep 10, 2024
1 parent 538da75 commit 24d874a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class WithOnFailureFunction extends InngestFunction implements WithFailureHandler {
public class WithOnFailureFunction extends InngestFunction {
@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
@IntegrationTest
@Execution(ExecutionMode.CONCURRENT)
class WithOnFailureIntegrationTest {
@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register("http://localhost:8080");
}

@Autowired
private DevServerComponent devServer;

Expand All @@ -35,7 +30,7 @@ static void setup(@Autowired CommHandler handler) {
@Test
void testWithOnFailureShouldCallOnFailure() throws Exception {
String eventName = "test/with-on-failure";
String eventId = InngestFunctionTestHelpers.sendEvent(client, eventName).first();
String eventId = InngestFunctionTestHelpers.sendEvent(client, eventName).getIds()[0];

Thread.sleep(sleepTime);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.inngest.testserver

import com.inngest.*

class SlackFailureReport :
InngestFunction(),
WithFailureHandler {
class SlackFailureReport : InngestFunction() {
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder =
builder
.id("always-fail-fn")
Expand Down
20 changes: 19 additions & 1 deletion inngest/src/main/kotlin/com/inngest/InngestFunction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ abstract class InngestFunction {
}

internal fun toFailureHandler(appId: String): InternalInngestFunction? {
if (this is WithFailureHandler) {
val onFailureMethod = this.javaClass.getMethod("onFailure", FunctionContext::class.java, Step::class.java)

if (onFailureMethod.declaringClass != InngestFunction::class.java) {
val fnConfig = buildConfig()
val fullyQualifiedId = "$appId-${fnConfig.id}"
val fnName = fnConfig.name ?: fnConfig.id
Expand All @@ -45,4 +47,20 @@ abstract class InngestFunction {
}
return null
}

/**
* Provide a function to be called if your function fails, meaning
* that it ran out of retries and was unable to complete successfully.
*
* This is useful for sending warning notifications or cleaning up
* after a failure and supports all the same functionality as a
* regular handler.
*
* @param ctx The function context including event(s) that triggered the function
* @param step A class with methods to define steps within the function
*/
open fun onFailure(
ctx: FunctionContext,
step: Step,
): Any? = null
}
14 changes: 0 additions & 14 deletions inngest/src/main/kotlin/com/inngest/WithFailureHandler.kt

This file was deleted.

0 comments on commit 24d874a

Please sign in to comment.