diff --git a/lib/helper/plugin_module_state_event.ex b/lib/helper/plugin_module_state_event.ex index 80d091f..a04f51d 100644 --- a/lib/helper/plugin_module_state_event.ex +++ b/lib/helper/plugin_module_state_event.ex @@ -49,6 +49,10 @@ defmodule MishkaInstaller.Helper.PluginModuleStateEvent do def is_changed?(new_plugins) do !Enum.all?(new_plugins, &(&1 in unquote(escaped_plugins))) end + + def is_initialized?(new_plugin) do + Enum.member?(unquote(escaped_plugins), new_plugin) + end end end diff --git a/lib/plugins_management/hook.ex b/lib/plugins_management/hook.ex index 2adc9e7..0497e6b 100644 --- a/lib/plugins_management/hook.ex +++ b/lib/plugins_management/hook.ex @@ -5,7 +5,7 @@ defmodule MishkaInstaller.PluginsManagement.Hook do quote bind_quoted: [opts: opts] do use GenServer, restart: :transient alias MishkaInstaller.PluginsManagement.{Event, Hook} - alias MishkaInstaller.Helper.ModuleStateEvent + alias MishkaInstaller.Helper.PluginModuleStateEvent, as: MSE alias MishkaInstaller.ProcessingPipelines.Queue.Queue # Based on https://elixirforum.com/t/59168/5 @app_config Mix.Project.config() @@ -78,7 +78,7 @@ defmodule MishkaInstaller.PluginsManagement.Hook do event_status = :persistent_term.get(:event_status, nil) - state = + new_state = if event_status == "ready" do Hook.register_start_helper(__MODULE__, state) else @@ -86,7 +86,7 @@ defmodule MishkaInstaller.PluginsManagement.Hook do state end - {:noreply, state} + {:noreply, new_state} end @impl true @@ -100,8 +100,17 @@ defmodule MishkaInstaller.PluginsManagement.Hook do @impl true def handle_info(:start_again, state) do + event = Keyword.get(state, :event) db_plg = Event.read(name: Keyword.get(state, :name)) - new_state = Hook.start_helper(__MODULE__, state, db_plg) + module = MSE.module_event_name(event) + + new_state = + if MSE.safe_initialize?(event) and module.is_initialized?(db_plg) do + Keyword.merge(state, status: db_plg.status, depends: db_plg.depends) + else + Hook.start_helper(__MODULE__, state, db_plg) + end + {:noreply, new_state} end diff --git a/lib/plugins_management/plugin_worker.ex b/lib/plugins_management/plugin_worker.ex index 39c0ae0..71cb3d9 100644 --- a/lib/plugins_management/plugin_worker.ex +++ b/lib/plugins_management/plugin_worker.ex @@ -3,6 +3,10 @@ defmodule MishkaInstaller.PluginsManagement.PluginWorker do alias MishkaInstaller.PluginsManagement.Event alias MishkaInstaller.Helper.PluginModuleStateEvent + def perform(event: event, type: :start, purge: :force) do + Event.start(event) + end + def perform(event: event, type: :start) do case Event.start(event, false) do {:error, error} ->