Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement onFailure handler. #63

Merged
merged 7 commits into from
Sep 11, 2024
Merged

Conversation

KiKoS0
Copy link
Collaborator

@KiKoS0 KiKoS0 commented Aug 25, 2024

Summary

The WithFailureHandler interface has a single method onFailure that
is called when the Inngest function fails.

Following this discussion we went with the 4th approach of having an
optionally overridable onFailure method in InngestFunction. And
conditionally check if it's overridden through reflection and register it.

https://stackoverflow.com/questions/2315445/how-to-quickly-determine-if-a-method-is-overridden-in-java/2315467#2315467

An example of how to use it in Kotlin:

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

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

Checklist

  • Update documentation
  • Added unit/integration tests

Related

@KiKoS0 KiKoS0 self-assigned this Aug 25, 2024
Copy link

linear bot commented Aug 25, 2024

@KiKoS0 KiKoS0 requested a review from albertchae August 25, 2024 01:25
@KiKoS0 KiKoS0 force-pushed the riadh/inn-3326-implement-on-failure branch from fed7cba to 4a366ed Compare September 10, 2024 21:40
@KiKoS0 KiKoS0 marked this pull request as ready for review September 10, 2024 21:45
@KiKoS0 KiKoS0 force-pushed the riadh/inn-3326-implement-on-failure branch 2 times, most recently from ed938dc to 24d874a Compare September 10, 2024 21:52
Copy link
Collaborator

@albertchae albertchae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, probably fine to merge now per discussion on https://linear.app/inngest/issue/INN-3551/document-how-we-should-implement and we can iterate on any feedback after cc: @djfarrelly

private val functions =
functions
.mapValues { (_, fn) -> fn.toInngestFunction() }
.plus(generateFailureFunctions(functions, client))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: slightly confusing that functions being passed to generateFailureFunctions is referring to the param, maybe worth creating a separate local variable for failureFunctions that you then call with plus? or rename this local val to inngestFunctions?

Comment on lines +46 to +55
Optional<EventEntry> event = Arrays.stream(devServer.listEvents().getData())
.filter(e -> "inngest/function.failed".equals(e.getName()) && eventName.equals(e.getData().getEvent().getName()))
.findFirst();

assert event.isPresent();

RunEntry<Object> onFailureRun = devServer.runsByEvent(event.get().getInternal_id()).first();

assertEquals("Completed", onFailureRun.getStatus());
assertEquals("On Failure Success", (String) onFailureRun.getOutput());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍

Comment on lines 33 to 36
val onFailureMethod = this.javaClass.getMethod("onFailure", FunctionContext::class.java, Step::class.java)

if (onFailureMethod.declaringClass != InngestFunction::class.java) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! maybe add a short comment here like only generate failure handler if onFailure was overridden?

@@ -9,7 +9,7 @@ import java.time.Duration
// TODO: Throw illegal argument exception
class InngestFunctionConfigBuilder {
var id: String? = null
private var name: String? = null
internal var name: String? = null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is because you use it to build the function name for the failure function, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly yeah

I took a more OOP approach for this feature which is slightly different
than the other SDKs but we can discuss this further in the PR review.

The `WithFailureHandler` interface has a single method `onFailure` that
is called when the function fails. An example of how to use this:

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

    override fun onFailure(ctx: FunctionContext, step: Step): Any? {
        // Handle failure
    }
}
```
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
    }
}

```
@KiKoS0 KiKoS0 force-pushed the riadh/inn-3326-implement-on-failure branch from 24d874a to d8d9b23 Compare September 11, 2024 23:43
@KiKoS0
Copy link
Collaborator Author

KiKoS0 commented Sep 11, 2024

Merging this now and we'll address feedback in follow-ups.

@KiKoS0 KiKoS0 merged commit 31add6b into main Sep 11, 2024
9 checks passed
@KiKoS0 KiKoS0 deleted the riadh/inn-3326-implement-on-failure branch September 11, 2024 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants