Skip to content

Commit

Permalink
Add retries configuration (#64)
Browse files Browse the repository at this point in the history
* Add retries configuration to InngestFunction

* Add a test function with zero retries

* Address PR comments
  • Loading branch information
KiKoS0 authored Sep 1, 2024
1 parent cee727d commit be831e0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class TranscodeVideo : InngestFunction() {
.id("process-video")
.name("Process video upload")
.triggerEvent("media/video.uploaded")
.retries(2)
.concurrency(10)

override fun execute(
Expand Down Expand Up @@ -68,6 +69,7 @@ class TranscodeVideo : InngestFunction() {
.id("process-video")
.name("Process video upload")
.triggerEvent("media/video.uploaded")
.retries(2)
.concurrency(10)

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

public class ZeroRetriesFunction extends InngestFunction {
@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("zero-retries-function")
.name("Zero Retries Function")
.triggerEvent("test/zero.retries")
.retries(0);
}

@Override
public String execute(FunctionContext ctx, Step step) {
step.run("fail-step", () -> {
throw new RetryAfterError("This is a retriable exception but retries are set to 0", 50);
}, String.class);

// This is unreachable because the step above will always throw an exception, and it will never be retried.
return "Success";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected HashMap<String, InngestFunction> functions() {
addInngestFunction(functions, new SendEventFunction());
addInngestFunction(functions, new NonRetriableErrorFunction());
addInngestFunction(functions, new RetriableErrorFunction());
addInngestFunction(functions, new ZeroRetriesFunction());

return functions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ void testNonRetriableShouldFail() throws Exception {
assertEquals(output.get("message"), "something fatally went wrong");
}

@Test
void testFunctionSetToZeroRetriesShouldFail() throws Exception {
String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/zero.retries").first();

Thread.sleep(sleepTime);

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("RetryAfterError");
assert output.get("stack").contains("ZeroRetriesFunction.lambda$execute");
}

@Test
void testRetriableShouldSucceedAfterFirstAttempt() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TranscodeVideo : InngestFunction() {
.name("Process video upload")
.triggerEvent("media/video.uploaded")
.concurrency(10)
.retries(2)

override fun execute(
ctx: FunctionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class InngestFunctionConfigBuilder {
private var name: String? = null
private var triggers: MutableList<InngestFunctionTrigger> = mutableListOf()
private var concurrency: MutableList<Concurrency>? = null
private var retries = 3
private var batchEvents: BatchEvents? = null

/**
Expand Down Expand Up @@ -119,18 +120,21 @@ class InngestFunctionConfigBuilder {
return this
}

/**
* Specifies the maximum number of retries for all steps across this function.
*
* @param attempts The number of times to retry a step before failing, defaults to 3.
*/
fun retries(attempts: Int): InngestFunctionConfigBuilder = apply { this.retries = attempts }

private fun buildSteps(serveUrl: String): Map<String, StepConfig> {
val scheme = serveUrl.split("://")[0]
return mapOf(
"step" to
StepConfig(
id = "step",
name = "step",
retries =
mapOf(
// TODO - Pull from conf option
"attempts" to 3,
),
retries = mapOf("attempts" to this.retries),
runtime =
hashMapOf(
"type" to scheme,
Expand Down

0 comments on commit be831e0

Please sign in to comment.