2020-07-14 15:05:00 -07:00
|
|
|
defmodule Omnibot.Plugin.Supervisor do
|
2020-08-11 15:55:36 -07:00
|
|
|
@moduledoc """
|
|
|
|
|
Wraps around a plugin and supervises the plugin's specified children.
|
|
|
|
|
"""
|
|
|
|
|
|
2020-07-14 15:05:00 -07:00
|
|
|
use Supervisor
|
|
|
|
|
|
|
|
|
|
def start_link(opts) do
|
|
|
|
|
{plugin, opts} = Keyword.pop(opts, :plugin)
|
|
|
|
|
{cfg, opts} = Keyword.pop(opts, :cfg)
|
|
|
|
|
start_link(plugin, cfg, opts)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def start_link(plugin, cfg, opts) when is_atom(plugin) do
|
|
|
|
|
Supervisor.start_link(__MODULE__, {plugin, cfg}, opts)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def child_spec(arg) do
|
|
|
|
|
id = Module.concat(arg[:plugin], Plugin.Supervisor)
|
|
|
|
|
%{
|
|
|
|
|
id: id,
|
|
|
|
|
start: {__MODULE__, :start_link, [arg]},
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def init({plugin, cfg}) when is_atom(plugin) do
|
2020-07-14 19:13:43 -07:00
|
|
|
children = plugin.plugin_children(cfg)
|
2020-07-17 17:37:55 -07:00
|
|
|
Supervisor.init(children, strategy: :one_for_all)
|
2020-07-14 15:05:00 -07:00
|
|
|
end
|
|
|
|
|
end
|