-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
fed7cba
to
4a366ed
Compare
ed938dc
to
24d874a
Compare
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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
?
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice 👍
val onFailureMethod = this.javaClass.getMethod("onFailure", FunctionContext::class.java, Step::class.java) | ||
|
||
if (onFailureMethod.declaringClass != InngestFunction::class.java) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 } } ```
24d874a
to
d8d9b23
Compare
Merging this now and we'll address feedback in follow-ups. |
Summary
TheWithFailureHandler
interface has a single methodonFailure
thatis called when the Inngest function fails.
Following this discussion we went with the 4th approach of having an
optionally overridable
onFailure
method inInngestFunction
. Andconditionally 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:
Checklist
Related