Skip to content

Commit

Permalink
Replace some essential data inside register hook
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed May 23, 2024
1 parent ca81f97 commit 80c99d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
28 changes: 19 additions & 9 deletions lib/plugins_management/hook.ex
Original file line number Diff line number Diff line change
@@ -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))}),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}}
Expand Down

0 comments on commit 80c99d6

Please sign in to comment.