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 1 commit
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
23 changes: 19 additions & 4 deletions lib/openid_connect/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ defmodule OpenIDConnect.Worker do

require Logger

def start_link(provider_configs, name \\ :openid_connect) do
GenServer.start_link(__MODULE__, provider_configs, name: name)
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} ->
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 @@ -44,6 +54,11 @@ defmodule OpenIDConnect.Worker do
{:reply, config, state}
end

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

def handle_info({:update_documents, provider}, state) do
with config <- get_in(state, [provider, :config]),
{:ok, documents} <- update_documents(provider, config) do
Expand Down
4 changes: 2 additions & 2 deletions test/openid_connect/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ defmodule OpenIDConnect.WorkerTest do
end

defp start_worker(config) do
{:ok, pid} = start_supervised({OpenIDConnect.Worker, config})
Process.sleep(10) # allow :update_documents to run
{:ok, pid} = start_supervised({OpenIDConnect.Worker, {config, self()}})
assert_receive :ready
{:ok, pid}
end
end