-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7338ad
Implement an interface for functions that need to handle failures.
KiKoS0 080d8ac
Add a function with an onFailure handler and a test for it
KiKoS0 861ef1f
Add a Kotlin function example that has a failure handler
KiKoS0 cb8582d
Make name in InngestFunctionConfigBuilder internal
KiKoS0 de78a21
Run formatting
KiKoS0 46b2323
Switch to an overridable `onFailure` method in `InngestFunction`
KiKoS0 d8d9b23
Address PR comments related to code clarity
KiKoS0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/EventsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
class EventsResponse { | ||
EventEntry[] data; | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
class EventEntry { | ||
String id; | ||
String name; | ||
|
||
String internal_id; | ||
|
||
EventEntryData data; | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
class EventEntryData{ | ||
EventData event; | ||
} | ||
|
||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
class EventData { | ||
String name; | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
...ot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/WithOnFailureFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.*; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class WithOnFailureFunction extends InngestFunction { | ||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("with-on-failure-function") | ||
.name("With On Failure Function") | ||
.triggerEvent("test/with-on-failure"); | ||
} | ||
|
||
@Override | ||
public String execute(FunctionContext ctx, Step step) { | ||
step.run("fail-step", () -> { | ||
throw new NonRetriableError("something fatally went wrong"); | ||
}, String.class); | ||
|
||
return "Success"; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String onFailure(@NotNull FunctionContext ctx, @NotNull Step step) { | ||
return "On Failure Success"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ring-boot-demo/src/test/java/com/inngest/springbootdemo/WithOnFailureIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.inngest.springbootdemo; | ||
|
||
import com.inngest.CommHandler; | ||
import com.inngest.Inngest; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.parallel.Execution; | ||
import org.junit.jupiter.api.parallel.ExecutionMode; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@IntegrationTest | ||
@Execution(ExecutionMode.CONCURRENT) | ||
class WithOnFailureIntegrationTest { | ||
@Autowired | ||
private DevServerComponent devServer; | ||
|
||
static int sleepTime = 5000; | ||
|
||
@Autowired | ||
private Inngest client; | ||
|
||
@Test | ||
void testWithOnFailureShouldCallOnFailure() throws Exception { | ||
String eventName = "test/with-on-failure"; | ||
String eventId = InngestFunctionTestHelpers.sendEvent(client, eventName).getIds()[0]; | ||
|
||
Thread.sleep(sleepTime); | ||
|
||
// Check that the original function failed | ||
RunEntry<Object> run = devServer.runsByEvent(eventId).first(); | ||
LinkedHashMap<String, String> output = (LinkedHashMap<String, String>) run.getOutput(); | ||
|
||
assertEquals("Failed", run.getStatus()); | ||
assertNotNull(run.getEnded_at()); | ||
assert output.get("name").contains("NonRetriableError"); | ||
|
||
// Check that the onFailure function was called | ||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
inngest-test-server/src/main/kotlin/com/inngest/testserver/SlackFailureReport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.inngest.testserver | ||
|
||
import com.inngest.* | ||
|
||
class SlackFailureReport : InngestFunction() { | ||
override fun config(builder: InngestFunctionConfigBuilder): InngestFunctionConfigBuilder = | ||
builder | ||
.id("always-fail-fn") | ||
.name("Always Fail Function") | ||
.triggerEvent("always-fail-fn") | ||
|
||
override fun execute( | ||
ctx: FunctionContext, | ||
step: Step, | ||
): String { | ||
step.run("throw exception") { | ||
throw RuntimeException("This function always fails") | ||
"Step result" | ||
} | ||
|
||
return "Success" | ||
} | ||
|
||
override fun onFailure( | ||
ctx: FunctionContext, | ||
step: Step, | ||
): String { | ||
step.run("send slack message") { | ||
"Sending a message to Slack" | ||
} | ||
|
||
return "onFailure Success" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. exactly yeah |
||
private var triggers: MutableList<InngestFunctionTrigger> = mutableListOf() | ||
private var concurrency: MutableList<Concurrency>? = null | ||
private var retries = 3 | ||
|
@@ -115,8 +115,14 @@ class InngestFunctionConfigBuilder { | |
scope: ConcurrencyScope? = null, | ||
): InngestFunctionConfigBuilder { | ||
when (scope) { | ||
ConcurrencyScope.ENVIRONMENT -> if (key == null) throw InngestInvalidConfigurationException("Concurrency key required with environment scope") | ||
ConcurrencyScope.ACCOUNT -> if (key == null) throw InngestInvalidConfigurationException("Concurrency key required with account scope") | ||
ConcurrencyScope.ENVIRONMENT -> | ||
if (key == null) { | ||
throw InngestInvalidConfigurationException("Concurrency key required with environment scope") | ||
} | ||
ConcurrencyScope.ACCOUNT -> | ||
if (key == null) { | ||
throw InngestInvalidConfigurationException("Concurrency key required with account scope") | ||
} | ||
ConcurrencyScope.FUNCTION -> {} | ||
null -> {} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 👍