Skip to content

Instantly share code, notes, and snippets.

@axelson
Created May 11, 2023 19:35
Show Gist options
  • Save axelson/85bce4196936f2bc194284096bdbfa72 to your computer and use it in GitHub Desktop.
Save axelson/85bce4196936f2bc194284096bdbfa72 to your computer and use it in GitHub Desktop.
defmodule MyApp.ObanUtils do
@doc """
Wait for our Phoenix Endpoint to come online
Without waiting for the Phoenix Endpoint to start up we have a race condition
where a job may execute that broadcasts a message (which requires the Endpoint
to be running). And additionally our Phoenix controller's depend on Oban to be
running so that `Oban.insert/3` can be used (which gives us guarantees about
uniqueness).
So by delaying these jobs until the Endpoint is up we avoid the circular race
condition.
"""
def ensure_endpoint_alive(timeout \\ 10_000)
def ensure_endpoint_alive(timeout) when timeout > 0 do
if Process.whereis(FeltServerWeb.Endpoint) do
:ok
else
Process.sleep(100)
ensure_endpoint_alive(timeout - 100)
end
end
def ensure_endpoint_alive(_timeout) do
raise "Timeout exceeded while waiting for endpoint to be available"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment