Skip to content

Commit

Permalink
Add some tests and entries worker queue for start all plugins of an e…
Browse files Browse the repository at this point in the history
…vent
  • Loading branch information
shahryarjb committed May 27, 2024
1 parent 4b91ae9 commit 833a499
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
12 changes: 9 additions & 3 deletions lib/plugins_management/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ defmodule MishkaInstaller.PluginsManagement.Event do
def handle_info({ref, _answer}, %{ref: ref} = state) do
# We don't care about the DOWN message now, so let's demonitor and flush it
Process.demonitor(ref, [:flush])

Queue.new(%{worker: MishkaInstaller.PluginsManagement.PluginWorker})

:persistent_term.put(:event_status, "ready")
# Do something with the result and then return
{:noreply, %{state | ref: nil}}
Expand Down Expand Up @@ -134,8 +137,7 @@ defmodule MishkaInstaller.PluginsManagement.Event do
output =
with :ok <- plugin_status(pl_item.status),
:ok <- hold_statuses?(pl_item.depends),
{:ok, db_plg} <- update(:status, :started, pl_item.id),
:ok <- MishkaInstaller.broadcast("event", :start, pl_item, true) do
{:ok, db_plg} <- update(:status, :started, pl_item.id) do
db_plg
end

Expand All @@ -144,7 +146,11 @@ defmodule MishkaInstaller.PluginsManagement.Event do
|> Enum.reject(&(is_tuple(&1) and elem(&1, 0) == :error))
|> Enum.sort_by(&{&1.priority, &1.name})

PluginModuleStateEvent.purge_create(sorted_plugins, event)
# send to queue
[elem(Queue.builder(%{entries: [plugins: sorted_plugins, event: event]}), 1)]
|> Queue.put_r(worker: MishkaInstaller.PluginsManagement.PluginWorker)

:ok
end
end

Expand Down
9 changes: 9 additions & 0 deletions lib/plugins_management/plugin_worker.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule MishkaInstaller.PluginsManagement.PluginWorker do
use MishkaInstaller.ProcessingPipelines.Queue.Worker, concurrency: 1
alias alias MishkaInstaller.Helper.PluginModuleStateEvent

def perform(plugins: plugins, event: event) do
:ok = PluginModuleStateEvent.purge_create(plugins, event)
MishkaInstaller.broadcast("event", :start_event, plugins, true)
end
end
28 changes: 23 additions & 5 deletions test/plugins_management/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ defmodule MishkaInstallerTest.PluginsManagement.EventTest do
use ExUnit.Case, async: false
alias MishkaInstaller.PluginsManagement.Event
alias MishkaInstallerTest.Support.MishkaPlugin.RegisterEmailSender
alias MishkaInstaller.ProcessingPipelines.Queue.Queue
alias MishkaInstaller.Helper.PluginModuleStateEvent
###################################################################################
########################## (▰˘◡˘▰) QueryTest (▰˘◡˘▰) ########################
###################################################################################
describe "Event Table CRUD QueryTest ===>" do
test "Create a Plugin record", _context do
Event.drop()
{:error, %{message: _msg, fields: [:extension, :event, :name]}} = assert Event.create(%{})

create = fn ->
Expand Down Expand Up @@ -168,6 +170,8 @@ defmodule MishkaInstallerTest.PluginsManagement.EventTest do

test "Start all plugins of an event" do
Event.drop()
MnesiaAssistant.subscribe({:table, Queue, :detailed})
MishkaInstaller.subscribe("event")

create = fn name, status, deps, event, priority ->
%{name: name, extension: MishkaTest, depends: deps, priority: priority}
Expand All @@ -176,14 +180,28 @@ defmodule MishkaInstallerTest.PluginsManagement.EventTest do
|> Event.create()
end

deps = [MishkaTest.Email1, MishkaTest.Email2, MishkaTest.Email3]
# TODO: The other tests can drop all records, it is reseter
if is_nil(Queue.read(worker: MishkaInstaller.PluginsManagement.PluginWorker)) do
Queue.create(%{worker: MishkaInstaller.PluginsManagement.PluginWorker})

assert_receive {:mnesia_table_event,
{:write, Queue,
{Queue, _id, MishkaInstaller.PluginsManagement.PluginWorker, _, _, _, _,
_, _, _}, _, _}},
5000
end

create.(MishkaTest.Email, :registered, deps, "after_login_test", 100)
create.(MishkaTest.Email1, :started, [], "after_login_test", 60)
create.(MishkaTest.Email2, :started, [], "after_login_test", 50)
create.(MishkaTest.Email3, :started, [], "before_login_test", 10)
deps = [MishkaPluginTest.Email1, MishkaPluginTest.Email2, MishkaPluginTest.Email3]

create.(MishkaPluginTest.Email, :registered, deps, "after_login_test", 100)
create.(MishkaPluginTest.Email1, :started, [], "after_login_test", 60)
create.(MishkaPluginTest.Email2, :started, [], "after_login_test", 50)
create.(MishkaPluginTest.Email3, :started, [], "before_login_test", 10)

Event.start("after_login_test")

assert_receive %{status: :start_event, data: _data}, 6000

module = PluginModuleStateEvent.module_event_name("after_login_test")
assert module.initialize?()

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end
MishkaInstallerTest.Support.MishkaQueue.WorkerOne.worker?()
MishkaInstallerTest.Support.MishkaQueue.WorkerTwo.worker?()
MishkaInstallerTest.Support.MishkaQueue.WorkerThree.worker?()
MishkaInstaller.PluginsManagement.PluginWorker.worker?()

{0, [Queue], :timer.seconds(15), 3}
|> Table.start_table(
Expand Down

0 comments on commit 833a499

Please sign in to comment.