-
Notifications
You must be signed in to change notification settings - Fork 19
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
Re-architecture of Alice's supervision tree structure #85
base: a2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,19 @@ defmodule Alice do | |
end | ||
|
||
@doc """ | ||
Stops an Alice bot instance | ||
Stops an Alice bot instance along with its handlers and adapters | ||
""" | ||
def stop_bot(pid) do | ||
Supervisor.terminate_child(Alice.Bot.Supervisor, pid) | ||
if Process.alive?(pid) do | ||
%{adapters: adapters, handlers: handlers} = :sys.get_state(pid) | ||
for adapter <- adapters do | ||
Supervisor.terminate_child(Alice.Adapter.Supervisor, Process.whereis(adapter)) | ||
end | ||
for handler <- handlers do | ||
Supervisor.terminate_child(Alice.Handler.Supervisor, Process.whereis(handler)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar question here for the handler supervisor. |
||
end | ||
Supervisor.terminate_child(Alice.Bot.Supervisor, pid) | ||
end | ||
end | ||
|
||
@doc """ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Alice.Adapter.Supervisor do | ||
@moduledoc """ | ||
Supervises any Alice.Adapter process that are started. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
""" | ||
|
||
use Supervisor | ||
|
||
def start_link(_) do | ||
Supervisor.start_link([ | ||
worker(Alice.Adapter, [], restart: :transient) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may want to consider converting this to a module based supervisor, [per the docs here] (https://hexdocs.pm/elixir/Supervisor.html#module-module-based-supervisors) particularly since this is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since I wasn't doing anything in |
||
], strategy: :simple_one_for_one, name: __MODULE__) | ||
end | ||
|
||
def init(_), do: :ok | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
defmodule Alice.Bot.Supervisor do | ||
@moduledoc false | ||
@moduledoc """ | ||
Supervises any Alice.Bot process that are started. | ||
""" | ||
|
||
use Supervisor | ||
|
||
def start_link(opts \\ []) do | ||
Supervisor.start_link(__MODULE__, :ok, opts) | ||
def start_link(_) do | ||
Supervisor.start_link([ | ||
worker(Alice.Bot, [], restart: :transient) | ||
], strategy: :simple_one_for_one, name: __MODULE__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about going with a module based supervisor. I also don't think we need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left it SOFO because the bot process is started by the user's bot project after the entire Alice tree starts. Using SOFO, we don't have to double-supervise the bot process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, having said that, I'm not 100% happy with having to start the bot there kind of manually, so maybe that's something we still have to figure out. |
||
end | ||
|
||
def init(:ok) do | ||
[worker(Alice.Bot, [], restart: :transient)] | ||
|> supervise(strategy: :simple_one_for_one) | ||
end | ||
def init(_), do: :ok | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
defmodule Alice.Handler.Supervisor do | ||
def start_link do | ||
import Supervisor.Spec, warn: false | ||
@moduledoc """ | ||
Supervises any Alice.Handler process that are started. | ||
""" | ||
|
||
children = [ | ||
use Supervisor | ||
|
||
def start_link(_) do | ||
Supervisor.start_link([ | ||
worker(Alice.Handler, [], restart: :transient) | ||
] | ||
Supervisor.start_link(children, strategy: :simple_one_for_one) | ||
], strategy: :simple_one_for_one, name: __MODULE__) | ||
end | ||
|
||
def init(_), do: :ok | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
Supervisor.stop
(docs here) withAlice.Adapter.Supervisor
be used to avoid the loop?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into it, that would be much better. This is what I had to do to get rid of that error messages after the tests run, and I'm all for simplifying it.