From 6be676c5fef91bd642adc1897da8a0b301693d37 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Wed, 6 Nov 2024 16:13:31 -0500 Subject: [PATCH 1/2] add Goth.start and cleanup error handling to avoid unnecessary crashes --- lib/goth.ex | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/goth.ex b/lib/goth.ex index 6e41f22..20f6948 100644 --- a/lib/goth.ex +++ b/lib/goth.ex @@ -109,6 +109,18 @@ defmodule Goth do GenServer.start_link(__MODULE__, opts, name: registry_name(name)) end + def start(opts) do + opts = + opts + |> Keyword.put_new(:refresh_before, @refresh_before_minutes * 60) + |> Keyword.put_new(:http_client, {:finch, []}) + |> Keyword.put_new(:source, {:default, []}) + |> Keyword.put_new(:retry_delay, &exp_backoff/1) + + name = Keyword.fetch!(opts, :name) + GenServer.start(__MODULE__, opts, name: registry_name(name)) + end + def __finch__(options) do {method, options} = Keyword.pop!(options, :method) {url, options} = Keyword.pop!(options, :url) @@ -246,8 +258,9 @@ defmodule Goth do end end - defp handle_retry(exception, %{retries: retries, max_retries: max_retries}) when retries >= max_retries - 1 do - raise "too many failed attempts to refresh, last error: #{inspect(exception)}" + defp handle_retry(exception, %{retries: retries, max_retries: max_retries} = state) when retries >= max_retries - 1 do + Logger.error("too many failed attempts to refresh, last error: #{inspect(exception)}") + {:noreply, %{state | retries: 0}} end defp handle_retry(_, state) do From 4958159d1e9acec2154590ecacc732ecd58f8312 Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 7 Nov 2024 13:52:58 -0500 Subject: [PATCH 2/2] shutdown process if cannot retry --- lib/goth.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/goth.ex b/lib/goth.ex index 20f6948..817d459 100644 --- a/lib/goth.ex +++ b/lib/goth.ex @@ -260,7 +260,7 @@ defmodule Goth do defp handle_retry(exception, %{retries: retries, max_retries: max_retries} = state) when retries >= max_retries - 1 do Logger.error("too many failed attempts to refresh, last error: #{inspect(exception)}") - {:noreply, %{state | retries: 0}} + {:stop, {:shutdown, exception}, %{state | retries: 0}} end defp handle_retry(_, state) do