Skip to content

Commit

Permalink
Add unregister an event and all events
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed May 25, 2024
1 parent e95f619 commit ad7ad61
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
55 changes: 37 additions & 18 deletions lib/plugins_management/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule MishkaInstaller.PluginsManagement.Event do
alias MnesiaAssistant.{Transaction, Query, Table}
alias MnesiaAssistant.Error, as: MError
alias MishkaInstaller.Helper.PluginModuleStateEvent
import MnesiaAssistant, only: [er: 1, erl_fields: 4]
####################################################################################
########################## (▰˘◡˘▰) Schema (▰˘◡˘▰) ############################
####################################################################################
Expand Down Expand Up @@ -176,35 +177,40 @@ defmodule MishkaInstaller.PluginsManagement.Event do
mod_plugins <- Enum.reject(plugins, &(&1.status in [:stopped, :held])),
sorted_plugins <- Enum.sort_by(mod_plugins, &{&1.priority, &1.name}),
:ok <- PluginModuleStateEvent.purge_create(sorted_plugins, event),
:ok <- MishkaInstaller.broadcast("event", :stop, db_plg, broadcast),
:ok <- MishkaInstaller.unsubscribe("event"),
:ok <- MishkaInstaller.broadcast("event", :unregister, db_plg, broadcast),
:ok <- GenServer.call(db_plg.name, :unsubscribe),
:ok <- GenServer.stop(name, :normal) do
{:ok, :unregister, db_plg}
end
end

def unregister(event) do
event
# TODO: unregister all plugins of an event
end

def unregister() do
# TODO: unregister all plugins of all event
end
case read(event: event) do
{:error, error} ->
{:error, error}

def purge(name, _broadcast) do
with {:ok, :unregister, _db_plg} <- name.unregister(false) do
# TODO: After creating `installation_management` we can purge its project
data ->
data
|> Enum.map(fn pl_item ->
delete(name: pl_item.name)
MishkaInstaller.broadcast("event", :unregister, pl_item, true)
GenServer.call(pl_item.name, :unsubscribe)
GenServer.stop(pl_item.name, :normal)
end)

PluginModuleStateEvent.purge(event)
end
end

def purge(event) do
event
# TODO: purge all plugins of an event
end
def unregister() do
case group_events() do
{:ok, events} ->
Enum.each(events, &unregister(&1))
:ok

def purge() do
# TODO: purge all plugins of all event
error ->
error
end
end

def get(key) do
Expand Down Expand Up @@ -417,6 +423,19 @@ defmodule MishkaInstaller.PluginsManagement.Event do
end
end

def group_events(key \\ [:event]) do
Transaction.transaction(fn ->
Query.select(__MODULE__, [{erl_fields({__MODULE__}, keys(), key, 1), [], er(:all)}])
end)
|> case do
{:atomic, result} ->
{:ok, Enum.uniq(result)}

{:aborted, reason} ->
Transaction.transaction_error(reason, __MODULE__, "deleting", :global, :database)
end
end

####################################################################################
########################## (▰˘◡˘▰) Helper (▰˘◡˘▰) ############################
####################################################################################
Expand Down
9 changes: 4 additions & 5 deletions lib/plugins_management/hook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ defmodule MishkaInstaller.PluginsManagement.Hook do
Event.unregister(config(:__plugin__), config(:__event__), broadcast)
end

def purge(broadcast \\ true) do
Event.purge(config(:__plugin__), broadcast)
end

def get() do
GenServer.call(__MODULE__, :get)
end
Expand All @@ -54,7 +50,6 @@ defmodule MishkaInstaller.PluginsManagement.Hook do
start: 1,
restart: 1,
stop: 1,
purge: 1,
unregister: 1

def __after_compile__(_env, _bytecode) do
Expand Down Expand Up @@ -85,6 +80,10 @@ defmodule MishkaInstaller.PluginsManagement.Hook do
{:reply, state, state}
end

def handle_call(:unsubscribe, _from, state) do
{:reply, MishkaInstaller.unsubscribe("event"), state}
end

@impl true
def handle_info(_reason, state) do
{:noreply, state}
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.0", "6f0eff9c9c489f26b69b61440bf1b238d95badae49adac77973cbacae87e3c2e", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "ea7a9307de9d1548d2a72d299058d1fd2339e3d398560a0e46c27dab4891e4d2"},
"mishka_developer_tools": {:git, "https://github.com/mishka-group/mishka_developer_tools.git", "b9d9d0cdc19bc4ae3a9cfe61a5843d4b1219472c", []},
"mishka_developer_tools": {:git, "https://github.com/mishka-group/mishka_developer_tools.git", "271d4ebaa5275a9f2f592490d40bfb3cb27b7db6", []},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
}
3 changes: 0 additions & 3 deletions test/plugins_management/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ defmodule MishkaInstallerTest.PluginsManagement.EventTest do
{:error, _error} = assert Event.get(name: RegisterEmailSender)
end

test "Purge a plugin" do
end

test "Backup all plugins" do
end

Expand Down

0 comments on commit ad7ad61

Please sign in to comment.