-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create module for SDK response and simplify invoke
- Loading branch information
Showing
4 changed files
with
127 additions
and
58 deletions.
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
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 |
---|---|---|
@@ -1,3 +1,97 @@ | ||
defmodule Inngest.NonRetriableError do | ||
defexception message: "Not retrying error. Exiting." | ||
end | ||
|
||
defmodule Inngest.SdkResponse do | ||
@moduledoc """ | ||
Represents an SDK response to the executor when ran | ||
""" | ||
|
||
defstruct [ | ||
:status, | ||
:body, | ||
:retry | ||
] | ||
|
||
@type t() :: %__MODULE__{ | ||
status: number(), | ||
body: binary(), | ||
retry: nil | :noretry | binary() | boolean() | ||
} | ||
|
||
alias Inngest.Headers | ||
|
||
# NOTES: | ||
# ********* RESPONSE *********** | ||
# Each results has a specific meaning to it. | ||
# status, data | ||
# 206, generatorcode -> store result and continue execution | ||
# 200, resp -> execution completed (including steps) of function | ||
# 400, error -> non retriable error | ||
# 500, error -> retriable error | ||
def from_result({:ok, value}) do | ||
case Jason.encode(value) do | ||
{:ok, encoded} -> | ||
%__MODULE__{ | ||
status: 200, | ||
body: encoded | ||
} | ||
|
||
{:error, _error} -> | ||
%__MODULE__{ | ||
status: 500, | ||
body: "Failed to encode result into JSON: #{value}" | ||
} | ||
end | ||
end | ||
|
||
def from_result({:ok, opcodes, :continue}) do | ||
%__MODULE__{ | ||
status: 206, | ||
body: Jason.encode!(opcodes) | ||
} | ||
end | ||
|
||
# No retry error response | ||
def from_result({:error, error, :noretry}) do | ||
encoded = | ||
case Jason.encode(error) do | ||
{:ok, encoded} -> encoded | ||
{:error, _} -> "Failed to encode error: #{error}" | ||
end | ||
|
||
%__MODULE__{ | ||
status: 400, | ||
body: encoded, | ||
retry: :noretry | ||
} | ||
end | ||
|
||
def from_result({:error, error, _}) do | ||
encoded = | ||
case Jason.encode(error) do | ||
{:ok, encoded} -> encoded | ||
{:error, _} -> "Failed to encode error: #{error}" | ||
end | ||
|
||
%__MODULE__{ | ||
status: 500, | ||
body: encoded, | ||
retry: true | ||
} | ||
end | ||
|
||
@doc """ | ||
Set the retry header depending on response | ||
""" | ||
@spec maybe_retry_header(Plug.Conn.t(), t()) :: Plug.Conn.t() | ||
def maybe_retry_header(conn, %{retry: :noretry} = _resp) do | ||
Plug.Conn.put_resp_header(conn, Headers.no_retry(), "true") | ||
end | ||
|
||
def maybe_retry_header(conn, %{retry: dur} = _resp) when is_binary(dur) do | ||
Plug.Conn.put_resp_header(conn, Headers.retry_after(), dur) | ||
end | ||
|
||
def maybe_retry_header(conn, _resp), do: conn | ||
end |
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