Skip to content

Commit

Permalink
Add retries
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r committed Oct 4, 2024
1 parent 37f40af commit 00d7da3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
34 changes: 27 additions & 7 deletions inngest/experimental/mocked/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def trigger(
stack: list[str] = []
steps: dict[str, object] = {}
planned = set[str]()
attempt = 0

max_attempt = 4
if fn._opts.retries is not None:
max_attempt = fn._opts.retries

while True:
step_id: typing.Optional[str] = None
Expand All @@ -57,9 +62,9 @@ def trigger(
logger = unittest.mock.Mock()
request = server_lib.ServerRequest(
ctx=server_lib.ServerRequestCtx(
attempt=0,
attempt=attempt,
disable_immediate_execution=True,
run_id="abc123",
run_id="test",
stack=server_lib.ServerRequestCtxStack(stack=stack),
),
event=event[0],
Expand Down Expand Up @@ -110,18 +115,33 @@ def trigger(
)

if res.error:
return _Result(
error=res.error,
output=None,
status=Status.FAILED,
)
if attempt >= max_attempt:
return _Result(
error=res.error,
output=None,
status=Status.FAILED,
)

attempt += 1
continue

if res.multi:
for step in res.multi:
if not step.step:
# Unreachable
continue

if step.error:
if attempt >= max_attempt:
return _Result(
error=step.error,
output=None,
status=Status.FAILED,
)

attempt += 1
continue

if step.step.display_name in step_stubs:
stub = step_stubs[step.step.display_name]
if stub is Timeout:
Expand Down
84 changes: 84 additions & 0 deletions inngest/experimental/mocked/trigger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,87 @@ def fn(

with pytest.raises(UnstubbedStepError):
trigger(fn, inngest.Event(name="test"), client)

def test_retry_step(self) -> None:
counter = 0

@client.create_function(
fn_id="test",
trigger=inngest.TriggerEvent(event="test"),
)
def fn(
ctx: inngest.Context,
step: inngest.StepSync,
) -> str:
def a() -> str:
nonlocal counter
counter += 1
if counter < 2:
raise Exception("oh no")
return "hi"

return step.run("a", a)

res = trigger(fn, inngest.Event(name="test"), client)
assert res.status is Status.COMPLETED
assert res.output == "hi"

def test_fail_step(self) -> None:
@client.create_function(
fn_id="test",
retries=0,
trigger=inngest.TriggerEvent(event="test"),
)
def fn(
ctx: inngest.Context,
step: inngest.StepSync,
) -> None:
def a() -> None:
raise Exception("oh no")

step.run("a", a)

res = trigger(fn, inngest.Event(name="test"), client)
assert res.status is Status.FAILED
assert res.output is None
assert isinstance(res.error, Exception)
assert str(res.error) == "oh no"

def test_retry_fn(self) -> None:
counter = 0

@client.create_function(
fn_id="test",
trigger=inngest.TriggerEvent(event="test"),
)
def fn(
ctx: inngest.Context,
step: inngest.StepSync,
) -> str:
nonlocal counter
counter += 1
if counter < 2:
raise Exception("oh no")
return "hi"

res = trigger(fn, inngest.Event(name="test"), client)
assert res.status is Status.COMPLETED
assert res.output == "hi"

def test_fail_fn(self) -> None:
@client.create_function(
fn_id="test",
retries=0,
trigger=inngest.TriggerEvent(event="test"),
)
def fn(
ctx: inngest.Context,
step: inngest.StepSync,
) -> None:
raise Exception("oh no")

res = trigger(fn, inngest.Event(name="test"), client)
assert res.status is Status.FAILED
assert res.output is None
assert isinstance(res.error, Exception)
assert str(res.error) == "oh no"

0 comments on commit 00d7da3

Please sign in to comment.