Skip to content

Commit

Permalink
VIP - Add some helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed May 30, 2024
1 parent f01783d commit 3abaa9e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/helper/plugin_module_state_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 13 additions & 4 deletions lib/plugins_management/hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -78,15 +78,15 @@ 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
Process.send_after(__MODULE__, :register_start_again, 1000)
state
end

{:noreply, state}
{:noreply, new_state}
end

@impl true
Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/plugins_management/plugin_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down

0 comments on commit 3abaa9e

Please sign in to comment.