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 4 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
43 changes: 28 additions & 15 deletions lib/openid_connect/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ defmodule OpenIDConnect.Worker do

@refresh_time 60 * 60 * 1000

require Logger

def start_link(provider_configs, name \\ :openid_connect) do
GenServer.start_link(__MODULE__, provider_configs, name: name)
end
Expand All @@ -20,8 +22,8 @@ defmodule OpenIDConnect.Worker do
def init(provider_configs) do
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)

{:ok, state}
Expand All @@ -43,23 +45,34 @@ defmodule OpenIDConnect.Worker do
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)

{:noreply, state}
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

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)

Process.send_after(self(), {:update_documents, provider}, refresh_time)
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})
Process.sleep(10) # allow :update_documents to run
Copy link
Member

Choose a reason for hiding this comment

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

Doing sleep during a test isn't ideal. It would be better if the pid of the test could be added as an option and if present the worker would notify the pid when the document was updated. Then the test would assert_received for that message and timeout/fail if it doesn't happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. The test doesn't really need to be notified when update_documents completes (it's mocked), but the :update_documents should be in the worker's queue before the test continues.

Copy link
Member

Choose a reason for hiding this comment

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

as it stands, I think the PR's architecture implementation is wrong. The update_documents function should be blocking. I'd like to see failing test cases and work back from there to a solution

Copy link
Contributor Author

Choose a reason for hiding this comment

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

update_documents is still blocking. It was already a handle_info before this PR, I haven't changed that, and it schedules itself periodically, as before. I deferred it's invocation from init/1 to allow the worker (and it's supervision tree) to remain alive in the case of a temporary network error (domain resolution failure, for example).

Feel free to look at alternatives (#30).

{:ok, pid}
end
end