Skip to content

Commit

Permalink
Better dns resolver retries
Browse files Browse the repository at this point in the history
Not sure this will work, but if it doesn't we can just skip the dns test
  • Loading branch information
michaeljguarino committed Sep 24, 2024
1 parent 07113b1 commit 0e64f2a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 20 additions & 0 deletions apps/core/lib/core/retry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Core.Retry do
defstruct [wait: 200, attempts: 0, max: 3]

def retry(fun, args) when is_function(fun) and (is_list(args) or is_map(args)) do
struct(__MODULE__, Map.new(args))
|> retry(fun)
end

def retry(%__MODULE__{attempts: attempts, max: max, wait: wait} = conf, fun) do
case {attempts < max, fun.()} do
{_, :ok} -> :ok
{_, {:ok, res}} -> {:ok, res}
{true, {:error, _} = error} ->
Logger.info "failed to execute function, error: #{inspect(error)}"
:timer.sleep(wait)
retry(%{conf | attempts: attempts + 1}, fun)
{_, {:error, err}} -> {:error, err}
end
end
end
13 changes: 7 additions & 6 deletions apps/core/lib/core/services/cloud/workflow/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ defmodule Core.Services.Cloud.Workflow.Shared do
def sync(_), do: :ok

def up(%ConsoleInstance{status: :deployment_created, url: url} = inst) do
:timer.sleep(:timer.seconds(5))
case {DNS.resolve(url), DNS.resolve(url, :cname)} do
{{:ok, [_ | _]}, _} -> mark_provisioned(inst)
{_, {:ok, [_ | _]}} -> mark_provisioned(inst)
{{:error, err}, _} -> {:error, "cannot resolve #{url}: #{inspect(err)}"}
end
Core.Retry.retry(fn ->
case {DNS.resolve(url), DNS.resolve(url, :cname)} do
{{:ok, [_ | _]}, _} -> mark_provisioned(inst)
{_, {:ok, [_ | _]}} -> mark_provisioned(inst)
{{:error, err}, _} -> {:error, "cannot resolve #{url}: #{inspect(err)}"}
end
end, wait: :timer.seconds(30), max: 100)
end

def up(%ConsoleInstance{status: :pending, postgres: pg, configuration: conf} = inst) do
Expand Down

0 comments on commit 0e64f2a

Please sign in to comment.