Skip to content

Commit

Permalink
Register failure handler
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Nov 24, 2023
1 parent 13fc246 commit 4f5f681
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
70 changes: 44 additions & 26 deletions lib/inngest/function.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ defmodule Inngest.Function do
"""
@callback exec(Context.t(), Input.t()) :: {:ok, any()} | {:error, any()}

@doc """
The method to be called when the Inngest function fails
"""
@callback handle_failure(Context.t(), Input.t()) :: {:ok, any()} | {:error, any()}

defmacro __using__(_opts) do
quote location: :keep do
alias Inngest.{Client, Trigger}
Expand Down Expand Up @@ -120,34 +115,53 @@ defmodule Inngest.Function do
|> List.first()
end

@impl true
def handle_failure(_ctx, _input), do: {:ok, "noop"}
defoverridable handle_failure: 2

def step(path),
do: %{
step: %Step{
id: :step,
name: "step",
runtime: %Step.RunTime{
url: "#{Config.app_host() <> path}?fnId=#{slug()}&step=step"
},
retries: %Step.Retry{
attempts: retries()
}
}
}

def serve(path) do
handler =
if failure_handler_defined?(__MODULE__) do
id = "#{slug()}-failure"

[
%{
id: id,
name: "#{name()} (failure)",
triggers: [%Trigger{event: "inngest/function.failed"}],
steps: %{
step: %Step{
id: :step,
name: "step",
runtime: %Step.RunTime{
url: "#{Config.app_host() <> path}?fnId=#{id}&step=step"
},
retries: %Step.Retry{
attempts: 0
}
}
}
}
]
else
[]
end

[
%{
id: slug(),
name: name(),
triggers: [trigger()],
steps: step(path),
mod: __MODULE__
steps: %{
step: %Step{
id: :step,
name: "step",
runtime: %Step.RunTime{
url: "#{Config.app_host() <> path}?fnId=#{slug()}&step=step"
},
retries: %Step.Retry{
attempts: retries()
}
}
}
}
]
] ++ handler
end

defp retries() do
Expand All @@ -159,6 +173,10 @@ defmodule Inngest.Function do
retry -> retry
end
end

defp failure_handler_defined?(mod) do
mod.__info__(:functions) |> Keyword.get(:handle_failure) == 2
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/support/cases/retriable_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ defmodule Inngest.Test.Case.RetriableError do

{:ok, "completed"}
end

def handle_failure(_ctx, _input) do
{:ok, "noop"}
end
end

0 comments on commit 4f5f681

Please sign in to comment.