z_module_manager (zotonic_core v1.0.0-rc.17)

Module manager, starts/restarts a site's modules.

Summary

Functions

Activate a module. The module is marked as active and started as a child of the module supervisor. The module manager can be checked later to see if the module started or not.

Before activating a module, check if it depends on non-activated other modules. This checks the complete graph of all modules that are currently activated plus the new modules. All missing dependencies will be listed. This also shows the modules that have missing dependencies and can't run because of those missing dependencies.

Return the list of active modules.

Return whether a specific module is active.

Return the list of all active modules and their directories. Exclude modules that are missing from the system.

Check which modules are active and installed but currently not in running state. The site is hardcoded as it must be part of the running list of modules, if not then it is added to the list of not running modules irrespective if it is activated or not.

Return the list of all modules in the database.

Convert process state when code is changed

Deactivate a module. The module is marked as deactivated and stopped when it was running.

Before deactivating a module, check if active modules depend on the deactivated module. This checks the complete graph of all modules that are currently activated minus the deactivated module. All missing dependencies will be listed. This also shows the modules that have missing dependencies and can't run because of those missing dependencies.

Return a module's dependencies as a tuple usable for z_toposort:sort/1.

Sort all modules on their dependencies (with sub sort the module's priority)

List of modules enabled for backup sites.

Return the list of all modules running.

Return the status of all enabled modules.

Return the list of all provided functionalities in running modules.

Return the list of all modules that are activated but not present on the file system.

Return the status of any ongoing upgrade

Return a list of all modules

Sync enabled modules with loaded modules

Handle down messages from modules.

Initiates the server.

Check if a service is provided by any module.

Get the author of a module.

Get the configurations of a module.

Get the description of a module.

Fetch information about a module or site.

Get the schema version of a module.

Get the title of a module.

Check if the code of a module exists. The database can hold module references to non-existing modules.

Check all observers of a module, ensure that they are all active. Used after a module has been reloaded

Return the priority of a module. Default priority is 500, lower is higher priority. Never crash on a missing module.

Sort the results of a scan on module priority first, module name next. The list is made up of {module, Values} tuples

Reinstall the given module's schema, calling Module:manage_schema(install, Context); if that function exists.

Restart a module, activates the module if it was not activated.

Scan for a list of modules. A module is always an OTP application, the name of the application is similar to the name of the module.

Scan for a list of modules and the current site. A module is always an OTP application,

Return a table with per dependeny which modules depend on it.

Return a table with per provision which modules provide it.

Run after a site has been started up. Notify to the site that all modules are ready and load the translations.

Starts the module manager

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Reload the list of all modules, add processes if necessary.

Wait till all modules are started, used when starting up a new or test site.

Return the pid of a running module

Types

manage_schema/0

-type manage_schema() :: install | {upgrade, ToVersion :: integer()}.

module_status/0

-type module_status() ::
          new | starting | running | stopping | restarting | retrying | failed | stopped | removing.

Functions

activate(Module, Context)

-spec activate(atom(), z:context()) -> ok | {error, not_found}.

Activate a module. The module is marked as active and started as a child of the module supervisor. The module manager can be checked later to see if the module started or not.

activate_await(Module, Context)

-spec activate_await(atom(), z:context()) -> ok | {error, not_active | not_found}.

activate_precheck(Module, Context)

-spec activate_precheck(atom() | [atom()], z:context()) ->
                           ok | {error, #{atom() => [atom()]}} | {error, {cyclic, z_toposort:cycles()}}.

Before activating a module, check if it depends on non-activated other modules. This checks the complete graph of all modules that are currently activated plus the new modules. All missing dependencies will be listed. This also shows the modules that have missing dependencies and can't run because of those missing dependencies.

active(Context)

-spec active(#context{cowreq :: cowboy_req:req() | undefined,
                      cowenv :: cowboy_middleware:env() | undefined,
                      site :: atom(),
                      controller_module :: atom() | undefined,
                      client_id :: binary() | undefined,
                      client_topic :: mqtt_sessions:topic() | undefined,
                      routing_id :: binary() | undefined,
                      acl :: term() | admin | undefined,
                      acl_is_read_only :: boolean(),
                      user_id :: integer() | authenticated | undefined,
                      render_state :: undefined | z_render:render_state(),
                      db :: {atom(), atom()} | undefined,
                      dbc :: pid() | undefined,
                      language :: [atom()],
                      tz :: binary(),
                      props :: map(),
                      depcache :: pid() | atom(),
                      dispatcher :: pid() | atom(),
                      template_server :: pid() | atom(),
                      scomp_server :: pid() | atom(),
                      dropbox_server :: pid() | atom(),
                      pivot_server :: pid() | atom(),
                      module_indexer :: pid() | atom(),
                      translation_table :: atom()}) ->
                [Module :: atom()].

Return the list of active modules.

active(Module, Context)

-spec active(Module :: atom(),
             #context{cowreq :: cowboy_req:req() | undefined,
                      cowenv :: cowboy_middleware:env() | undefined,
                      site :: atom(),
                      controller_module :: atom() | undefined,
                      client_id :: binary() | undefined,
                      client_topic :: mqtt_sessions:topic() | undefined,
                      routing_id :: binary() | undefined,
                      acl :: term() | admin | undefined,
                      acl_is_read_only :: boolean(),
                      user_id :: integer() | authenticated | undefined,
                      render_state :: undefined | z_render:render_state(),
                      db :: {atom(), atom()} | undefined,
                      dbc :: pid() | undefined,
                      language :: [atom()],
                      tz :: binary(),
                      props :: map(),
                      depcache :: pid() | atom(),
                      dispatcher :: pid() | atom(),
                      template_server :: pid() | atom(),
                      scomp_server :: pid() | atom(),
                      dropbox_server :: pid() | atom(),
                      pivot_server :: pid() | atom(),
                      module_indexer :: pid() | atom(),
                      translation_table :: atom()}) ->
                boolean().

Return whether a specific module is active.

active_dir(Context)

-spec active_dir(z:context()) -> [{Module :: atom(), Dir :: file:filename_all()}].

Return the list of all active modules and their directories. Exclude modules that are missing from the system.

active_not_running(Context)

-spec active_not_running(Context) -> [Module] when Context :: z:context(), Module :: atom().

Check which modules are active and installed but currently not in running state. The site is hardcoded as it must be part of the running list of modules, if not then it is added to the list of not running modules irrespective if it is activated or not.

all(Context)

-spec all(z:context()) -> [atom()].

Return the list of all modules in the database.

code_change(OldVsn, State, Extra)

Convert process state when code is changed

deactivate(Module, Context)

-spec deactivate(atom(), z:context()) -> ok.

Deactivate a module. The module is marked as deactivated and stopped when it was running.

deactivate_precheck(Module, Context)

-spec deactivate_precheck(atom(), z:context()) ->
                             ok |
                             {error, #{atom() => [atom()]}} |
                             {error, {cyclic, z_toposort:cycles()}}.

Before deactivating a module, check if active modules depend on the deactivated module. This checks the complete graph of all modules that are currently activated minus the deactivated module. All missing dependencies will be listed. This also shows the modules that have missing dependencies and can't run because of those missing dependencies.

dependencies(M)

-spec dependencies({atom(), term()} | atom()) ->
                      {atom() | {atom(), term()}, Depends :: [atom()], Provides :: [atom()]}.

Return a module's dependencies as a tuple usable for z_toposort:sort/1.

dependency_sort(Context)

-spec dependency_sort(z:context() | [atom()]) -> {ok, [atom()]} | {error, {cyclic, z_toposort:cycles()}}.

Sort all modules on their dependencies (with sub sort the module's priority)

env_backup_modules()

-spec env_backup_modules() -> [atom()].

List of modules enabled for backup sites.

get_modules(Context)

Return the list of all modules running.

get_modules_status(Context)

-spec get_modules_status(Context) -> ModulesStatus
                            when Context :: z:context(), ModulesStatus :: [{atom(), module_status()}].

Return the status of all enabled modules.

get_provided(Context)

-spec get_provided(z:context()) -> [atom()].

Return the list of all provided functionalities in running modules.

get_uninstalled(Context)

-spec get_uninstalled(z:context()) -> [atom()].

Return the list of all modules that are activated but not present on the file system.

get_upgrade_status(Context)

Return the status of any ongoing upgrade

handle_call(Message, From, State)

Return a list of all modules

handle_cast(Message, State)

Sync enabled modules with loaded modules

handle_info(Info, State)

Handle down messages from modules.

init(Site)

Initiates the server.

is_provided(Service, Context)

-spec is_provided(atom(), z:context()) -> boolean().

Check if a service is provided by any module.

lib_dir(Module)

-spec lib_dir(atom()) -> {error, bad_name} | file:filename().

mod_author(Module)

-spec mod_author(Module) -> Author
                    when Module :: atom() | binary() | string(), Author :: binary() | undefined.

Get the author of a module.

mod_config(Module)

-spec mod_config(Module) -> ConfigList
                    when Module :: atom() | binary() | string(), ConfigList :: [map()].

Get the configurations of a module.

mod_description(Module)

-spec mod_description(Module) -> Desc
                         when Module :: atom() | binary() | string(), Desc :: binary() | undefined.

Get the description of a module.

mod_info(Module)

-spec mod_info(Module) -> Info
                  when
                      Module :: atom() | binary() | string(),
                      Info ::
                          #{app := atom(),
                            prio := integer(),
                            version := binary() | undefined,
                            schema := integer() | undefined,
                            title := binary() | undefined,
                            description := binary() | undefined,
                            app_dir := file:filename_all() | undefined}.

Fetch information about a module or site.

mod_schema(Module)

-spec mod_schema(Module) -> SchemaVersion
                    when Module :: atom() | binary() | string(), SchemaVersion :: integer() | undefined.

Get the schema version of a module.

mod_title(Module)

-spec mod_title(Module) -> Title
                   when Module :: atom() | binary() | string(), Title :: binary() | undefined.

Get the title of a module.

mod_version(Module)

-spec mod_version(Module) -> Version
                     when Module :: atom() | binary() | string(), Version :: binary() | undefined.

module_exists(M)

Check if the code of a module exists. The database can hold module references to non-existing modules.

module_reloaded(Module, Context)

-spec module_reloaded(Module :: atom(),
                      #context{cowreq :: cowboy_req:req() | undefined,
                               cowenv :: cowboy_middleware:env() | undefined,
                               site :: atom(),
                               controller_module :: atom() | undefined,
                               client_id :: binary() | undefined,
                               client_topic :: mqtt_sessions:topic() | undefined,
                               routing_id :: binary() | undefined,
                               acl :: term() | admin | undefined,
                               acl_is_read_only :: boolean(),
                               user_id :: integer() | authenticated | undefined,
                               render_state :: undefined | z_render:render_state(),
                               db :: {atom(), atom()} | undefined,
                               dbc :: pid() | undefined,
                               language :: [atom()],
                               tz :: binary(),
                               props :: map(),
                               depcache :: pid() | atom(),
                               dispatcher :: pid() | atom(),
                               template_server :: pid() | atom(),
                               scomp_server :: pid() | atom(),
                               dropbox_server :: pid() | atom(),
                               pivot_server :: pid() | atom(),
                               module_indexer :: pid() | atom(),
                               translation_table :: atom()}) ->
                         ok.

Check all observers of a module, ensure that they are all active. Used after a module has been reloaded

module_to_app(ModuleName)

-spec module_to_app(ModuleName) -> Module
                       when ModuleName :: binary() | atom() | string(), Module :: module().

prio(Module)

-spec prio(atom()) -> integer().

Return the priority of a module. Default priority is 500, lower is higher priority. Never crash on a missing module.

prio_sort(ModuleProps)

-spec prio_sort([atom() | {atom(), term()}]) -> [atom() | {atom(), term()}].

Sort the results of a scan on module priority first, module name next. The list is made up of {module, Values} tuples

reinstall(Module, Context)

Reinstall the given module's schema, calling Module:manage_schema(install, Context); if that function exists.

restart(Module, Context)

-spec restart(Module :: atom(),
              #context{cowreq :: cowboy_req:req() | undefined,
                       cowenv :: cowboy_middleware:env() | undefined,
                       site :: atom(),
                       controller_module :: atom() | undefined,
                       client_id :: binary() | undefined,
                       client_topic :: mqtt_sessions:topic() | undefined,
                       routing_id :: binary() | undefined,
                       acl :: term() | admin | undefined,
                       acl_is_read_only :: boolean(),
                       user_id :: integer() | authenticated | undefined,
                       render_state :: undefined | z_render:render_state(),
                       db :: {atom(), atom()} | undefined,
                       dbc :: pid() | undefined,
                       language :: [atom()],
                       tz :: binary(),
                       props :: map(),
                       depcache :: pid() | atom(),
                       dispatcher :: pid() | atom(),
                       template_server :: pid() | atom(),
                       scomp_server :: pid() | atom(),
                       dropbox_server :: pid() | atom(),
                       pivot_server :: pid() | atom(),
                       module_indexer :: pid() | atom(),
                       translation_table :: atom()}) ->
                 ok | {error, not_found}.

Restart a module, activates the module if it was not activated.

scan()

-spec scan() -> [{Module :: atom(), Application :: atom(), Dir :: file:filename_all()}].

Scan for a list of modules. A module is always an OTP application, the name of the application is similar to the name of the module.

scan(Context)

-spec scan(z:context()) -> [{Module :: atom(), Application :: atom(), Dir :: file:filename_all()}].

Scan for a list of modules and the current site. A module is always an OTP application,

scan_depending(Context)

-spec scan_depending(z:context()) -> #{atom() := [atom()]}.

Return a table with per dependeny which modules depend on it.

scan_provided(Context)

-spec scan_provided(z:context()) -> #{atom() := [atom()]}.

Return a table with per provision which modules provide it.

sidejob_finish_start(Site)

-spec sidejob_finish_start(Site) -> ok when Site :: atom().

Run after a site has been started up. Notify to the site that all modules are ready and load the translations.

start_link(Site)

-spec start_link(Site) -> {ok, Pid} | ignore | {error, Error}
                    when Site :: atom(), Pid :: pid(), Error :: term().

Starts the module manager

startable(M, Context)

terminate(Reason, State)

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

upgrade(Context)

-spec upgrade(z:context()) -> ok.

Reload the list of all modules, add processes if necessary.

upgrade_await(Context)

-spec upgrade_await(z:context()) -> ok | {error, timeout}.

Wait till all modules are started, used when starting up a new or test site.

whereis(Module, Context)

-spec whereis(atom(), z:context()) -> {ok, pid()} | {error, not_running | not_found}.

Return the pid of a running module