View Source MishkaInstaller.Hook (Mishka Installer v0.0.4)
In addition to being one of the most significant modules of the MishkaInstaller library, the Hook module gives you the ability to make the library as a whole, as well as the projects that make use of this library, more modular.
It is essential to comprehend that you may treat any action performed independently as an event and register an unlimited number of plugins for that event.
You can do this by considering that action to be done separately. Each plugin has the potential to have its own individual inputs and outputs, and depending on the architecture of the area that you want to use Hook in, you may even be able to change the output and send it to other plugins.
Throughout all of the many parts that have been built for this module, it has been attempted to have a flexible approach to dealing with errors and to provide the programmer with a wide variety of options.
With the additional functions at her disposal, the programmer can actually create a gateway in their projects, where the data flow must pass through several gates, whether it changes or remains unchanged, and a series of operations are performed.
For illustration's sake, let's suppose you want the registration system to allow users to sign up for social networks on Twitter and Google.
When you use this library, it is conceivable that you can quickly display HTML or even operate in the background before registering after registering.
This is a significant improvement over the standard practice, in which you are required to modify the primary codes of your project. Create a separate plugin.
It is interesting to notice that these facilities are quite basic and convenient for the admin user.
If this opportunity is provided, the management can manage its own plugins in different ways if it has a dashboard.
It is crucial to highlight that each plugin is its own GenServer
; in addition,
it is dynamically supervised, and the information is stored in the database as well as in ETS.
Furthermore, this section is very useful even if the programmer wants to perform many tasks that are not associated with Perform defined functions.
The fact that the programmers have to introduce each plugin to the system based on a specific behavior is one of the exciting aspects of using this section. Additionally, the system has prepared some default behaviors to force the programmers to introduce the plugins in the order specified by the system.
The use of custom behaviors on the part of the programmer and MishkaInstaller itself makes debugging easier; however, this library does not leave the programmer to fend for themselves in this significant matter; rather, a straightforward error storage system is prepared based on the particular activities being performed.
Should prevent any unpredictable behavior at any costs.
build-purpose
Build purpose
Imagine you are going to make an application that will have many plugins built for it in the future. But the fact that many manipulations will be made on your source code makes it difficult to maintain the application. For example, you present a content management system for your users, and now they need to activate a section for registration and SMS; the system allows you to present your desired input/output absolutely plugin oriented to your users and makes it possible for the developers to write their required applications beyond the core source code.
We have used this library in the Mishka content management system.
plugin-management-system-implementation-theory
Plugin management system implementation theory
The library categorizes your whole software design structure into many parts;
and has an appropriate dependency that is optional with GenServer
;
it considers a monitoring branch for each of your plugins, which results in fewer errors and downtime
. The considered part:
- Behaviors and events
- Recalling or
Hook
with priority State
management and links to the database (PostgreSQL
support)
Except from the 1st item, which can be redefined based on the developer's needs in his/her personal systems, the remaining items are almost constant, and a lot of functions will be handed to the developer to manage each plugin.
behaviors-and-events
Behaviors and events
In this section, you can define a series of events for each event
,
for example: after successful registration
or unsuccessful purchase
from “the store”,
and for each event
, put a set of callbacks
in one module. After completing this step,
when the user wants to create his own plugin, the @behaviour
module will call you in its action module.
This helps you have a regular and error-free system, and the library uses an almost integrated structure in all of its events.
hook-with-priority
Hook
with priority
In Mishka Elixir Plugin Management Library, a series of action or hook
functions are given to the developer of the main plugin or software,
which helps build plugins outside the system and convert software sections into separate events
.
Some of the functions of this module include the following:
- Registering a plugin outside of the system in database and ram
state
- Removing plugin from database and
state
- Restoring plugin
- Successful pause of plugin
Hook
plugin- Search among the
events
And other functions that help both the mother software become an event-driven system and the developer
can build the desired plugin or extension for different parts of the software and install it on the system as a separate package.
This package can also be published in hex
.
state-management-and-links-to-the-database-supporting-postgresql
State management and links to the database supporting PostgreSQL
The Hook
module manages a large part of this part, and the developer of the external plugin usually does not need it much.
Still, this part creates a state
on RAM for each plugin that is activated in a specific event and a dynamic supervisor for it.
This allows us in case of an error in each plugin;
the other plugins in the different events face no errors, and the system will try to restart with various strategies.
It should be noted for more stability and data storage after registering a plugin in the system;
This section also maintains a backup copy of the database and strategies for recall in the event in case of an error.
But to speed up the calling of each plugin, the website always uses state
.
using-the-library
Using the library:
After installing this library, you must first install the required database of this package on your website,
for which a mix task
has been created, which is enough to load it once in your terminal, in the project path before the start.
mix mishka_installer.db.gen.migration
After implementing the above sections, you must first implement events in your main software and place
the call
function from the Hook
module there to call all the plugins activated in the event you want based on priority.
And give the state
you want, to these plugins in order, and the output you expect will eventually be generated.
For example, you can see the mentioned description in a function controller in phoenix after a successful registration as the following:
def login(conn, %{"user" => %{"email" => email, "password" => password}} = _params) do
# If your conditions are passed we call an event and pass it a struct of entries
# which our developers need to create plugin with this information
state = %MishkaInstaller.Reference.OnUserAfterLogin{
conn: conn,
endpoint: :html,
ip: user_ip, type: :email,
user_info: user_info
}
hook = MishkaInstaller.Hook.call(event: "on_user_after_login", state: state)
hook.conn
|> renew_session()
|> put_session(:user_id, user_info.id)
|> put_flash(:info, "You entered to our world, well played.")
|> redirect(to: "/home")
end
Now the event is ready in the part where you need to allow the developer to make his own plugins for it. And it's time to write a plugin for this section. This is very simple. Consider the following example:
defmodule MishkaUser.SuccessLogin do
alias MishkaInstaller.Reference.OnUserAfterLogin
use MishkaInstaller.Hook,
module: __MODULE__,
behaviour: OnUserAfterLogin,
event: :on_user_after_login,
initial: []
@spec initial(list()) :: {:ok, OnUserAfterLogin.ref(), list()}
def initial(args) do
event = %PluginState{name: "MishkaUser.SuccessLogin", event: Atom.to_string(@ref), priority: 1}
Hook.register(event: event)
{:ok, @ref, args}
end
@spec call(OnUserAfterLogin.t()) :: {:reply, OnUserAfterLogin.t()}
def call(%OnUserAfterLogin{} = state) do
new_state = Your_Code_Or_Function
{:reply, new_state}
end
end
As you can see in the above, we used
MishkaInstaller.Reference.OnUserAfterLogin
in order to activatebehavior
which has a fewcallback
in it, and you can see here.
There should be two main functions in each plugin, namely
initial
and alsocall
. In the first function, we introduce our plugin, and in the second function, whenever the action function calls this special event for which the plugin is written, based on priority. This plugin is also called. But what is important is the final output of thecall
function. This output may be the input of other plugins with higher priorities. The order of the plugins is from small to large, and if several plugins are registered for a number, it is sorted by name in the second parameter. And it should be noted that in any case, if you did not want thisstate
to go to other plugins and the last output is returned in the same plugin, and you can replace{:reply, :halt, new_state}
with{:reply, new_state}
.
Subsequent plugins with higher priorities are not counted, and the loop ends here.
Notice that a GenServer
will be made based on each plugin name without a supervisor,
which can be used for temporary memory in the case when the __using__
function is used as above,
which results in the following option:
use MishkaInstaller.Hook,
module: __MODULE__,
behaviour: OnUserAfterLogin,
event: :on_user_after_login,
initial: []
The last two step to use the plugin you have to put it in your Application
module so that whenever the server is turned off and on,
the plugin is run again and if it is not registered, a copy of its support will be provided once in the database.
children = [
%{id: YOUR_PLUGIN_MODULE, start: {YOUR_PLUGIN_MODULE, :start_link, [[]]}}
]
And add these config in your project like /config/config.exs
config :mishka_installer, :basic,
repo: YOUR_Repo,
pubsub: YOUR_PUBSUB or nil,
html_router: YOUR_WEBSITE_ROUTER_MODULE
module-communication-process-of-mishkainstaller-hook
Module communication process of MishkaInstaller.Hook
1-call-plugins
1. call plugins
+--------------+
| Application |
+------+-------+
|
+------v------+
| Supervisor |
+------+------+
|
|
+---------------------------+ +------------v----------------+
| | | |
| MishkaInstaller.PluginETS | | MishkaInstaller.PluginState |
| | | |
+---------------+-----------+ +-------------+---------------+
| |
| |
+---v---------------------------v-----+
| |
| MishkaInstaller.Hook.call |
| |
+-------------------------------------+
2-register-a-plugin
2. Register a plugin
+--------------+
| Application |
+------+-------+
|
+------v------+
| Supervisor |
+-------+-----+
|
|
+---------------------------+ +-------------v---------------+
| | | |
| MishkaInstaller.PluginETS | | MishkaInstaller.PluginState |
| | | |
+-----------------^---------+ +-------------^---------------+
| |
| |
+-----+-------------------------+-----+
| |
| MishkaInstaller.Hook.register |
| |
+-------------------^-----------------+
|
|
+-------------+----------------+
| Developer's plugin GenServer |
+-------------^----------------+
|
|
+-------------+----------------+
| |
|Developer's plugin Application|
| |
+------------------------------+
Link to this section Summary
Types
This type can be used when you want to introduce an event
This type can be used when you want to introduce an plugin
Functions
Your software will be able to call each of the active and registered plugins in the list in the order that they appear
depending on the priority that the function assigns to each plugin, and it will do so with a particular event.
The program generates a State
and sends it to the first plugin in the list. This plugin, in turn,
sends the desired output changes to the remaining plugins in the list, and this process is repeated all the way down
to the plugin that is at the end of the list.
This function will delete the specified plugin from the State
that you are contemplating, but there will be no change to the database due to this action.
It is important to remember that stopping a batch based on a particular occurrence is also possible.
The operation performed in this function is the same as ensure_event?/1
, except that the output is a pattern with a special message.
You will have the opportunity to select the type of dependency, which can either be :soft
or :hard
,
when you are in the process of registering each plugin.
It is not necessary to check related plugins if this item is configured to use the first available option,
and a plugin can be installed in the system on its own accord if this option is selected.
Registering a plugin is the first step in modularizing and event-oriented programming for your project. You should be aware, prior to registering a custom plugin to the project and activating it in a particular section, that the plugin is designed by a programmer and is required to adhere to a series of fundamental structures before it can be used.
The only difference between this method and the start/1
function is that
this function deletes from RAM any states that are associated with the plugin that you want to use.
You are able to activate all the plugins that have been installed by using this feature. It is important to point out that it is possible to activate numerous plugins at the same time as a single plugin or even a batch based on a particular event. This can be done in several different ways.
This function will stop the specified plugin from the State
that you are contemplating, but there will be no change to the database due to this action.
It is important to remember that stopping a batch based on a particular occurrence is also possible.
This function is very similar to the delete/1
command; however, in addition to removing the plugin from the system,
it also removes the plugin from the database.
Link to this section Types
Link to this section Functions
Your software will be able to call each of the active and registered plugins in the list in the order that they appear
depending on the priority that the function assigns to each plugin, and it will do so with a particular event.
The program generates a State
and sends it to the first plugin in the list. This plugin, in turn,
sends the desired output changes to the remaining plugins in the list, and this process is repeated all the way down
to the plugin that is at the end of the list.
It is important to note that you are free to make use of the available options in accordance with the logic of the component of your program that is responsible for loading the event you want.
You can transmit a
State
to an unlimited number of plugins and perform various operations on it, but the output of theState
will not be taken into consideration, and the startingState
will be used as the one that determines the final output.You have the ability to include a portion of the
State
's information in the private map. This flag ensures that just reading is open to the public, while authoring is obviously restricted in terms of the final product.At the time of registration, each plugin is assigned a priority; the lower this number, the higher the likelihood that this plugin will be called before others on the list; if it shares the same priority as multiple other plugins, the order in which it appears in the list will be determined by its name.
examples
Examples
state = %MishkaInstaller.Reference.OnUserAfterLogin{conn: conn, endpoint: :html, user_info: user_info}
# or load with private flag
state = %TestEvent{user_info: %{name: "shahryar"}, private: %{acl: 0, ip: "127.0.1.1", endpoint: :admin}}
MishkaInstaller.Hook.call(event: "on_user_after_login", state: state)
# or
MishkaInstaller.Hook.call(event: "on_user_after_login", state: state, operation: :no_return)
# Example call in a controller
def login(conn, %{"user" => %{"email" => email, "password" => password}} = _params) do
# If your conditions are passed we call an event and pass it a struct of entries
# which our developers need to create plugin with this information
hook = MishkaInstaller.Hook.call(event: "on_user_after_login", state: state)
hook.conn
|> renew_session()
|> put_session(:user_id, user_info.id)
|> put_flash(:info, "You entered to our world, well played.")
|> redirect(to: "/home")
end
@spec delete([{:event, event()} | {:extension, atom()} | {:module, plugin()}, ...]) :: list() | {:error, :delete, String.t()} | {:ok, :delete, String.t()}
This function will delete the specified plugin from the State
that you are contemplating, but there will be no change to the database due to this action.
It is important to remember that stopping a batch based on a particular occurrence is also possible.
examples
Examples
MishkaInstaller.Hook.delete(module: "ensure_event_plugin")
# or
MishkaInstaller.Hook.delete(event: "on_user_after_login")
@spec ensure_event(MishkaInstaller.PluginState.t(), :debug) :: {:error, :ensure_event, %{errors: list()}} | {:ok, :ensure_event, String.t()}
The operation performed in this function is the same as ensure_event?/1
, except that the output is a pattern with a special message.
examples
Examples
test_plug =
%MishkaInstaller.PluginState{
name: "MishkaInstaller.Hook",
event: "event_one",
depend_type: :hard,
depends: ["MishkaInstaller.PluginState"]
}
MishkaInstaller.Hook.ensure_event(test_plug, :debug)
@spec ensure_event?(MishkaInstaller.PluginState.t()) :: boolean()
You will have the opportunity to select the type of dependency, which can either be :soft
or :hard
,
when you are in the process of registering each plugin.
It is not necessary to check related plugins if this item is configured to use the first available option,
and a plugin can be installed in the system on its own accord if this option is selected.
If the second choice for the desired plugin is chosen, which is hard
,
then this method will be of assistance to you. It will verify each of the plugins listed in the dependent parameter,
and if there are no issues, it will return true
. If there are issues, however, it will return false
.
examples
Examples
test_plug =
%MishkaInstaller.PluginState{
name: "MishkaInstaller.Hook",
event: "event_one",
depend_type: :hard,
depends: ["MishkaInstaller.PluginState"]
}
MishkaInstaller.Hook.ensure_event?(test_plug)
@spec register(depends: :force, event: MishkaInstaller.PluginState.t()) :: {:error, :register, any()} | {:ok, :register, :activated | :force}
Registering a plugin is the first step in modularizing and event-oriented programming for your project. You should be aware, prior to registering a custom plugin to the project and activating it in a particular section, that the plugin is designed by a programmer and is required to adhere to a series of fundamental structures before it can be used.
each-plugin-must-meet-the-following-conditions-when-registering
Each plugin must meet the following conditions when registering:
- Because the process of utilizing a plugin is dependent on beginning
GenServer
by the name of that plugin, you need to think of it as aGenServer
and begin using it. - If you do not wish to handle this process manually, the
__using__
module of the Hook package has already prepared it for you; all you need to do is call it. The aforementioned explanation can be found in its entirety in the code snippet. - The definition of each plugin is determined by a certain behavior carried out by a module. You need to have some fundamental instances of this behavior, in addition to the requirements set forth by the plugin's developer.
Some fundamental behaviors are as follows:
- initial
- call
- stop
- delete
- restart
- start
For more clarity, you can see
MishkaInstaller.Reference.OnUserLoginFailure
behavior.
- All plugins are stored in ETS.
- All plugins are stored in the database.
- All plugins are stored in one supervisor and another
GenServer
.
In order to register a plugin, you need to call it in the
initial
function of the module in which you used theMishkaInstaller.Hook
directive and bind it with theuse
directive. Take, for instance:
use MishkaInstaller.Hook,
module: __MODULE__,
behaviour: OnUserAfterLogin,
event: :on_user_after_login,
initial: []
@spec initial(list()) :: {:ok, OnUserAfterLogin.ref(), list()}
def initial(args) do
event = %PluginState{name: "MishkaUser.SuccessLogin", event: Atom.to_string(@ref), priority: 1}
Hook.register(event: event)
{:ok, @ref, args}
end
After being added to the MishkaInstaller.PluginState
module struct, each plug-in is required to be registered in the system,
as demonstrated by the preceding line of code.
It is important to know that each plugin in your project can be registered in one of two different ways.
- By diagnosing problems and ensuring that the requested plugin is operational.
- By requiring the test stages to be completed or by ignoring the requirements of the plug-in itself.
examples
Examples
event = %PluginState{name: "MishkaUser.SuccessLogin", event: Atom.to_string(@ref), priority: 1}
MishkaInstaller.Hook.register(event: event)
# or
MishkaInstaller.Hook.register(event: event, depends: :force)
Based on the function restart/1
, restart all installed plugins.
examples
Examples
MishkaInstaller.Hook.restart()
@spec restart(depends: :force, event: event(), module: plugin()) :: list() | {:error, :restart, any()} | {:ok, :restart, String.t()}
The only difference between this method and the start/1
function is that
this function deletes from RAM any states that are associated with the plugin that you want to use.
examples
Examples
MishkaInstaller.Hook.restart(module: "ensure_event_plugin")
# or
MishkaInstaller.Hook.restart(module: "ensure_event_plugin", depends: :force)
# or
MishkaInstaller.Hook.restart(event: "on_user_after_login")
# or
MishkaInstaller.Hook.restart(event: "on_user_after_login", depends: :force)
@spec start([ {:depends, :force} | {:event, event()} | {:extension, atom()} | {:module, plugin()}, ... ]) :: list() | {:error, :start, binary() | [...]} | {:ok, :start, :force | binary()}
You are able to activate all the plugins that have been installed by using this feature. It is important to point out that it is possible to activate numerous plugins at the same time as a single plugin or even a batch based on a particular event. This can be done in several different ways.
Much like the register/1
function, this function has two modes,
which can be altered by looking at the needs of a plugin and the forced mode, respectively.
Note: When running in debug mode, the referred-to plugin in the project must
be set to Application.load/1
, and Application.unload/1
cannot be started.
Note: If your plugin has other dependencies, all of them must be activated before restarting.
For more information, see
ensure_event?/1
andensure_event/2
functions.
examples
Examples
MishkaInstaller.Hook.start(module: "ensure_event_plugin")
# or
MishkaInstaller.Hook.start(module: "ensure_event_plugin", depends: :force)
# or
MishkaInstaller.Hook.start(event: "on_user_after_login")
# or
MishkaInstaller.Hook.start(event: "on_user_after_login", depends: :force)
@spec stop(event: event(), module: plugin(), extension: atom()) :: list() | {:error, :stop, String.t()} | {:ok, :stop, String.t()}
This function will stop the specified plugin from the State
that you are contemplating, but there will be no change to the database due to this action.
It is important to remember that stopping a batch based on a particular occurrence is also possible.
examples
Examples
MishkaInstaller.Hook.stop(module: "ensure_event_plugin")
# or
MishkaInstaller.Hook.stop(event: "on_user_after_login")
@spec unregister(event: event(), module: plugin(), extension: atom()) :: list() | {:error, :unregister, any()} | {:ok, :unregister, Stream.timer()}
This function is very similar to the delete/1
command; however, in addition to removing the plugin from the system,
it also removes the plugin from the database.
examples
Examples
MishkaInstaller.Hook.unregister(module: "ensure_event_plugin")
# or
MishkaInstaller.Hook.unregister(event: "on_user_after_login")