Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep worker alive if update_documents fails #37

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions lib/openid_connect/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@ defmodule OpenIDConnect.Worker do

@refresh_time 60 * 60 * 1000

def start_link(provider_configs, name \\ :openid_connect) do
GenServer.start_link(__MODULE__, provider_configs, name: name)
require Logger

def start_link(options, name \\ :openid_connect)

def start_link({provider_configs, notify_pid}, name) do
GenServer.start_link(__MODULE__, {provider_configs, notify_pid}, name: name)
end

def start_link(provider_configs, name) do
start_link({provider_configs, nil}, name)
end

def init(:ignore) do
def init({:ignore, _pid}) do
:ignore
end

def init(provider_configs) do
def init({provider_configs, notify_pid}) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead this should be a separate functional call that is something like:

def init({provider_configs, pid}) is_id(pid) do
   {:ok, state} = init(provider_configs)
   Process.send(pid, :ready)
   {:ok, state}
end

you can drop the handle_info function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Third time lucky. I'm using Process.send_after rather than Process.send to ensure that update_documents will be in the workers inbox before the test proceeds.

state =
Enum.into(provider_configs, %{}, fn {provider, config} ->
documents = update_documents(provider, config)
{provider, %{config: config, documents: documents}}
Process.send_after(self(), {:update_documents, provider}, 0)
{provider, %{config: config, documents: nil}}
end)

unless is_nil(notify_pid) do
Process.send_after(self(), {:notify, notify_pid}, 1)
end

{:ok, state}
end

Expand All @@ -42,24 +54,40 @@ defmodule OpenIDConnect.Worker do
{:reply, config, state}
end

def handle_info({:update_documents, provider}, state) do
config = get_in(state, [provider, :config])
documents = update_documents(provider, config)

state = put_in(state, [provider, :documents], documents)

def handle_info({:notify, pid}, state) do
Process.send(pid, :ready, [])
{:noreply, state}
end

defp update_documents(provider, config) do
{:ok, %{remaining_lifetime: remaining_lifetime}} =
{:ok, documents} = OpenIDConnect.update_documents(config)

refresh_time = time_until_next_refresh(remaining_lifetime)
def handle_info({:update_documents, provider}, state) do
with config <- get_in(state, [provider, :config]),
{:ok, documents} <- update_documents(provider, config) do
{:noreply, put_in(state, [provider, :documents], documents)}
else
_ -> {:noreply, state}
end
end

Process.send_after(self(), {:update_documents, provider}, refresh_time)
defp update_documents(provider, config) do
with {:ok, documents} <- OpenIDConnect.update_documents(config),
remaining_lifetime <- Map.get(documents, :remaining_lifetime),
refresh_time <- time_until_next_refresh(remaining_lifetime) do
Process.send_after(self(), {:update_documents, provider}, refresh_time)
{:ok, documents}
else
{:error, :update_documents, reason} = error ->
Logger.warn("Failed to update documents for provider #{provider}: #{message(reason)}")
Process.send_after(self(), {:update_documents, provider}, @refresh_time)
error
end
end

documents
defp message(reason) do
if Exception.exception?(reason) do
Exception.message(reason)
else
"#{inspect(reason)}"
end
end

defp time_until_next_refresh(nil), do: @refresh_time
Expand Down
16 changes: 10 additions & 6 deletions test/openid_connect/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule OpenIDConnect.WorkerTest do

config = Application.get_env(:openid_connect, :providers)

{:ok, pid} = start_supervised({OpenIDConnect.Worker, config})
{:ok, pid} = start_worker(config)

state = :sys.get_state(pid)

Expand All @@ -43,10 +43,9 @@ defmodule OpenIDConnect.WorkerTest do

test "worker can respond to a call for the config" do
mock_http_requests()

config = Application.get_env(:openid_connect, :providers)

{:ok, pid} = start_supervised({OpenIDConnect.Worker, config})
{:ok, pid} = start_worker(config)

google_config = GenServer.call(pid, {:config, :google})

Expand All @@ -58,8 +57,7 @@ defmodule OpenIDConnect.WorkerTest do

config = Application.get_env(:openid_connect, :providers)

{:ok, pid} = start_supervised({OpenIDConnect.Worker, config})

{:ok, pid} = start_worker(config)
discovery_document = GenServer.call(pid, {:discovery_document, :google})

expected_document =
Expand All @@ -77,7 +75,7 @@ defmodule OpenIDConnect.WorkerTest do

config = Application.get_env(:openid_connect, :providers)

{:ok, pid} = start_supervised({OpenIDConnect.Worker, config})
{:ok, pid} = start_worker(config)

jwk = GenServer.call(pid, {:jwk, :google})

Expand All @@ -98,4 +96,10 @@ defmodule OpenIDConnect.WorkerTest do
end)
|> expect(:get, fn "https://www.googleapis.com/oauth2/v3/certs", _headers, _opts -> @google_certs end)
end

defp start_worker(config) do
{:ok, pid} = start_supervised({OpenIDConnect.Worker, {config, self()}})
assert_receive :ready
{:ok, pid}
end
end