diff --git a/lib/application.ex b/lib/application.ex index 3745a0c..3433d2c 100644 --- a/lib/application.ex +++ b/lib/application.ex @@ -6,12 +6,12 @@ defmodule MishkaInstaller.Application do def start(_type, _args) do children = [ {Registry, keys: :unique, name: MishkaJobWorkerRegistry}, + {Phoenix.PubSub, name: MishkaInstaller.PubSub}, {PartitionSupervisor, child_spec: DynamicSupervisor, name: MishkaJobWorkerSupervisor}, {PartitionSupervisor, child_spec: Task.Supervisor, name: MishkaInstaller.MnesiaTaskSupervisors}, {PartitionSupervisor, child_spec: Task.Supervisor, name: MishkaInstaller.JobTaskSupervisors}, - {Phoenix.PubSub, name: MishkaInstaller.PubSub, id: "queue"}, MishkaInstaller.ProcessingPipelines.Queue.Queue, MishkaInstaller.PluginsManagement.Event ] diff --git a/lib/plugins_management/hook.ex b/lib/plugins_management/hook.ex index d4de2ed..408110a 100644 --- a/lib/plugins_management/hook.ex +++ b/lib/plugins_management/hook.ex @@ -1,22 +1,28 @@ defmodule MishkaInstaller.PluginsManagement.Hook do + alias MishkaInstaller.PluginsManagement.Event + defmacro __using__(opts \\ []) do quote bind_quoted: [opts: opts] do + alias MishkaInstaller.PluginsManagement.{Event, Hook} # Based on https://elixirforum.com/t/59168/5 @app_config Mix.Project.config() + @plugin_event Keyword.get(opts, :event) + @plugin_name __MODULE__ @after_compile __MODULE__ - alias MishkaInstaller.PluginsManagement.{Event, Hook} - # TODO: we need genserver - # TODO: we need handle_call to get info from genserver state - # |__ Like: dependent modules, status of this plug - def config(), do: @app_config + def config(), + do: Keyword.merge(@app_config, __plugin__: @plugin_name, __event__: @plugin_event) - def config(key), do: Keyword.get(@app_config, key) + def config(key), do: Keyword.get(config(), key) - def register(plugin) do - with {:ok, plg} <- Event.builder(plugin), + def register(plugin \\ %{}) do + event = config(:__event__) + plugin_name = config(:__plugin__) + base = if is_nil(event), do: plugin, else: Map.merge(plugin, %{event: event}) + + with merged <- Map.merge(base, %{name: plugin_name, extension: config(:app)}), + {:ok, plg} <- Event.builder(merged), {:ok, module} <- Hook.ensure_loaded(plg.name), - {:ok, {_ev, _pr}} <- Hook.extension_loaded(config(:app), plg.extension), deps_list <- Event.hold_statuses(plg.depends), {:ok, db_plg} <- Event.create({:ok, Map.merge(plg, Hook.depends_status(deps_list, plg.status))}), @@ -65,6 +71,9 @@ defmodule MishkaInstaller.PluginsManagement.Hook do # TODO: Send stop request to this plugin Genserver end + # TODO: we need genserver + # TODO: we need handle_call to get info from genserver state + # |__ Like: dependent modules, status of this plug # TODO: Send a request to enable all held plugin, when this plugin is their dependency defoverridable register: 1, @@ -95,6 +104,7 @@ defmodule MishkaInstaller.PluginsManagement.Hook do end end + @doc false def extension_loaded(event, project) do if event == project do {:ok, {event, project}}