application(3)
π NAME π
application β generic OTP application functions. yep that's the whole name, she's simple and she's powerful π
β¨ DESCRIPTION β¨
okay so bestie, listen up: in OTP, an application is basically a whole component that does one specific thing, and you can start it and stop it as a unit, and reuse it in other systems. iconic behavior honestly π
This module is your group chat with the application controller β that's a process that gets started at every single Erlang runtime system, no cap, every one.
So what's in here? Two whole vibes:
- functions for controlling applications β like, starting them and stopping them
- functions for getting info about applications β like reading configuration parameters
An application is defined by an application specification. That spec normally lives in an application resource file named Application.app, where Application is the application name. For all the deets on what's actually inside the specification, go look at app.
Also lowkey? This module can be viewed as a behaviour for an application implemented according to the OTP design principles as a supervision tree. The definition of how to start and stop the tree goes in an application callback module, which exports a predefined set of functions. kbyeee structure π¦
For details about applications and behaviours, see OTP Design Principles.
π See Also
OTP Design Principles, kernel, app
π§ DATA TYPES π§
application_opt()
application_opt() ::
{description, Description :: string()} |
{vsn, Vsn :: string()} |
{id, Id :: string()} |
{modules, [Module :: module()]} |
{registered, Names :: [Name :: atom()]} |
{applications, [Application :: atom()]} |
{included_applications, [Application :: atom()]} |
{env, [{Par :: atom(), Val :: term()}]} |
{start_phases,
[{Phase :: atom(), PhaseArgs :: term()}] | undefined} |
{maxT, MaxT :: timeout()} |
{maxP, MaxP :: pos_integer() | infinity} |
{mod, Start :: {Module :: module(), StartArgs :: term()}}.
These are the built-in options you can hand to an application. the whole little menu of them bestie π
For descriptions of all the options, see app.
application_spec()
application_spec() ::
{application,
Application :: atom(),
AppSpecKeys :: [application_opt()]}.
An application specification. that's it, that's the shape πΈ
restart_type()
restart_type() :: permanent | transient | temporary.
The type of restart behaviour an application should have β like, how dramatic she is about coming back π
start_type()
start_type() ::
normal |
{takeover, Node :: node()} |
{failover, Node :: node()}.
The reason the application is being started on the current node. she has motives π
tuple_of(_T)
tuple_of(_T) :: tuple().
A tuple where the elements are of type T. so like, a tuple but make it β¨themedβ¨
π₯ FUNCTIONS π₯
ensure_all_started(Applications, Type, Mode) -> {ok, Started} | {error, AppReason}
ensure_all_started(Applications, Type, Mode) ->
{ok, Started} | {error, AppReason}
when
Applications :: atom() | [atom()],
Type :: restart_type(),
Mode :: serial | concurrent,
Started :: [atom()],
AppReason :: {atom(), term()}.
Applications is either an atom() or a list of atom() representing multiple applications. multitasking queen π«Ά
This function is equivalent to calling start/1,2 repeatedly on all the dependencies that aren't started yet, for each application. Optional dependencies will also be loaded and started if they are available. no forcing anyone's hand β¨
The Mode argument controls if the applications should be started in serial mode (one at a time) or concurrent mode. In concurrent mode, a dependency graph is built and the leaves of the graph are started concurrently and recursively. In both modes, no assertion can be made about the order the applications are started. literally cannot promise you a sequence bestie. If not supplied, it defaults to serial.
Returns {ok, AppNames} for a successful start or for an already started application (which is, however, omitted from the AppNames list). she's already here, no need to announce her twice π
The function reports {error, {AppName,Reason}} for errors, where Reason is any possible reason returned by start/1,2 when starting a specific dependency.
β οΈ If an error occurs, the applications started by the function are stopped to bring the set of running applications back to its initial state. it's giving "we're undoing this whole thing, pretend it never happened" π
ensure_started(Application) -> ok | {error, Reason}
ensure_started(Application) -> ok | {error, Reason}
when Application :: atom(), Reason :: term().
Equivalent to start(Application) except it returns ok for already started applications. no complaining, just ok π
ensure_started(Application, Type) -> ok | {error, Reason}
ensure_started(Application, Type) -> ok | {error, Reason}
when
Application :: atom(),
Type :: restart_type(),
Reason :: term().
Equivalent to start(Application, Type) except it returns ok for already started applications. same energy, but now with a Type π
get_all_env(Application) -> Env
get_all_env(Application) -> Env
when
Application :: atom(),
Env :: [{Par :: atom(), Val :: term()}].
Returns the configuration parameters and their values for Application. the whole env, dump it on the table bestie π
If the specified application is not loaded, or if the process executing the call does not belong to any application, the function returns []. empty. nothing. void. πΈ
get_all_key(Application) -> undefined | Keys
get_all_key(Application) -> undefined | Keys
when
Application :: atom(),
Keys ::
{ok, [{Key :: atom(), Val :: term()},
...]}.
Returns the application specification keys and their values for Application. If the argument is omitted, it defaults to the application of the calling process. she figures out where she belongs π§
If the specified application is not loaded, the function returns undefined. If the process executing the call does not belong to any application, the function returns [].
get_application(PidOrModule) -> undefined | {ok, Application}
get_application(PidOrModule) -> undefined | {ok, Application}
when
PidOrModule ::
(Pid :: pid()) | (Module :: module()),
Application :: atom().
Returns the name of the application to which the process Pid or the module Module belongs. it's a "who's your mom" query fr fr π
If the specified process does not belong to any application, or if the specified process or module does not exist, the function returns undefined. orphan energy π
get_env(Application, Par) -> undefined | {ok, Val}
get_env(Application, Par) -> undefined | {ok, Val}
when
Application :: atom(), Par :: atom(), Val ::
term().
Returns the value of configuration parameter Par for Application.
Returns undefined if any of the following applies:
- The specified application is not loaded.
- The configuration parameter does not exist.
- The process executing the call does not belong to any application.
three strikes and she gives you undefined π«Ά
get_env(Application, Par, Def) -> Val
get_env(Application, Par, Def) -> Val
when
Application :: atom(),
Par :: atom(),
Def :: term(),
Val :: term().
Works like get_env/2 but returns the value Def when configuration parameter Par does not exist. a fallback bestie, everyone needs one π
get_key(Application, Key) -> undefined | {ok, Val}
get_key(Application, Key) -> undefined | {ok, Val}
when
Application :: atom(), Key :: atom(), Val ::
term().
Returns the value of the application specification key Key for Application.
Returns undefined if any of the following applies:
- The specified application is not loaded.
- The specification key does not exist.
- The process executing the call does not belong to any application.
same three-strike situation, but now it's about spec keys not env params π
get_supervisor(Application) -> undefined | {ok, Pid}
get_supervisor(Application) -> undefined | {ok, Pid}
when Pid :: pid(), Application :: atom().
Returns the Pid of the supervisor running at the root of Application. the boss at the top of the tree, main character energy π
If the specified application does not exist or does not define a callback module, the function returns undefined. no supervisor, no Pid, we move on π
load(AppDescr, Distributed) -> ok | {error, Reason}
load(AppDescr, Distributed) -> ok | {error, Reason}
when
AppDescr ::
Application | (AppSpec :: application_spec()),
Application :: atom(),
Distributed ::
{Application, Nodes} |
{Application, Time, Nodes} |
default,
Nodes :: [node() | tuple_of(node())],
Time :: pos_integer(),
Reason :: term().
Loads the application specification for an application into the application controller. It also loads the application specifications for any included applications. Notice that the function does not load the Erlang object code β it's giving specs only, no actual code, don't get it twisted π
The application can be specified by its name Application. In this case, the application controller searches the code path for the application resource file Application.app and loads the specification it contains. she goes looking for it herself π
The application specification can also be specified directly as a tuple AppSpec, having the format and contents as described in app.
If Distributed == {Application,[Time,]Nodes}, the application becomes distributed. The argument overrides the value for the application in the Kernel configuration parameter distributed. Application must be the application name (same as in the first argument). If a node crashes and Time is specified, the application controller waits for Time milliseconds before attempting to restart the application on another node. If Time is not specified, it defaults to 0 and the application is restarted immediately. zero patience, she's on it instantly π₯
Nodes is a list of node names where the application can run, in priority from left to right. Node names can be grouped using tuples to indicate that they have the same priority.
Example:
Nodes = [cp1@cave, {cp2@cave, cp3@cave}]
This means that the application is preferably to be started at cp1@cave. If cp1@cave is down, the application is to be started at cp2@cave or cp3@cave. a whole backup plan, we love a girl with options π
If Distributed == default, the value for the application in the Kernel configuration parameter distributed is used.
loaded_applications() -> [{Application, Description, Vsn}]
when Application :: atom(), Description :: string(), Vsn :: string().
Okay so this one hands you a lil list of info about all the applications that are loaded via load/1,2, included applications included π. Application is literally just the application name. Description and Vsn? Those are the values of their description and vsn application specification keys respectively. Cute, right? β¨
permit(Application, Permission) -> ok | {error, Reason}
when Application :: atom(), Permission :: boolean(), Reason :: term().
Bestie, this changes the permission for Application to run at the current node π. Heads up tho β the application must be loaded using load/1,2 or this function just... doesn't do anything. No effect. Tragic.
Here's the tea β:
- If the permission of a loaded but not started application is set to
false,startreturnsokbut the application is not started until the permission is set totrue. - If the permission of a running application is set to
false, the application is stopped. Set it back totruelater and it gets restarted. Iconic. - If the application is distributed, setting the permission to
falsemeans it'll be started at, or moved to, another node β depending on how its distribution is configured (peepload/2for that π).
The function does not return until the application is started, stopped, or successfully moved to another node. However! In some cases where permission is set to true, the function returns ok even though the application is not started. When does that happen? When an application can't start because of dependencies on other applications that aren't started yet. When those finally start, Application starts as well. It's giving β¨patience is a virtueβ¨.
By default, all applications are loaded with permission true on all nodes π
. You can configure the permission using the Kernel configuration parameter permissions.
set_env(Config, Opts) -> ok
when Config :: [{Application, Env}], Application :: atom(), Env :: [{Par :: atom(), Val :: term()}], Opts :: [{timeout, timeout()} | {persistent,<br> boolean()}].
Sets the configuration Config for multiple applications at once. Efficiency queen π.
It's literally equivalent to calling set_env/4 on each application individually, except it's more efficient. The given Config is validated before the configuration is set β so no half-done messes.
set_env/2 uses the standard gen_server time-out value (5000 ms). The option timeout can be specified if another time-out value is useful β like, say, in situations where the application controller is heavily loaded π.
Option persistent can be set to true to guarantee that parameters set with set_env/2 are not overridden by those defined in the application resource file on load. This means persistent values will stick after the application is loaded and also on application reload. Fr fr, they stay.
If an application is given more than once, or if an application has the same key given more than once, the behaviour is undefined and a warning message will be logged. In future releases, an error will be raised. So like... don't do that π.
β οΈ WARNING: Use this function only if you know what you are doing β that is, on your own applications. It is very application-dependent and configuration parameter-dependent when and how often the value is read by the application. Careless use of this function can put the application in a weird, inconsistent, and malfunctioning state. π Not the malfunctioning state bestie.
set_env(Application, Par, Val, Opts) -> ok
when Application :: atom(), Par :: atom(), Val :: term(), Opts :: [{timeout, timeout()} | {persistent,<br> boolean()}].
Sets the value of configuration parameter Par for Application π¦.
set_env/4 uses the standard gen_server time-out value (5000 ms). Option timeout can be specified if another time-out value is useful β for example, in situations where the application controller is heavily loaded πΈ.
If set_env/4 is called before the application is loaded, the application environment values specified in file Application.app override the ones previously set π. This is also true for application reloads. So yeah, your pre-load sets can get bodied.
Option persistent can be set to true to guarantee that parameters set with set_env/4 are not overridden by those defined in the application resource file on load. This means persistent values will stick after the application is loaded and also on application reload. Periodt π
.
β οΈ WARNING: Use this function only if you know what you are doing β that is, on your own applications. It is very application-dependent and configuration parameter-dependent when and how often the value is read by the application. Careless use of this function can put the application in a weird, inconsistent, and malfunctioning state. (Yes, same warning, same energy β this is serious bestie π)
start(Application, Type) -> ok | {error, Reason}
when Application :: atom(), Type :: restart_type(), Reason :: term().
Starts Application β¨. If it's not loaded, the application controller first loads it using load/1. It ensures that any included applications are loaded, but does not start them β that's assumed to be taken care of in the code for Application.
The application controller checks the value of the application specification key applications to make sure all applications needed to be started before this one are running. If an application is missing and the application is not marked as optional, {error,{not_started,App}} is returned, where App is the name of the missing application. Note: this function makes no attempt to start any of the applications listed in applications, not even optional ones π€. Peep ensure_all_started/1,2 for recursively starting the current application and its dependencies.
Once validated, the application controller then creates an application master for the application π. The application master becomes the group leader of all the processes in the application. I/O is forwarded to the previous group leader though β this is just a way to identify processes that belong to the application. Used for example to find itself from any process, or, reciprocally, to kill them all when it terminates π.
The application master starts the application by calling the application callback function Module:start/2 as defined by the application specification key mod.
Argument Type specifies the type of the application. If omitted, it defaults to temporary.
- If a
permanentapplication terminates, all other applications and the entire Erlang node are also terminated π₯. - If a
transientapplication terminates:- with
Reason == normal, this is reported but no other applications are terminated. - abnormally, all other applications and the entire Erlang node are also terminated π₯.
- with
- If a
temporaryapplication terminates, this is reported but no other applications are terminated.
Notice that an application can always be stopped explicitly by calling stop/1. Regardless of the type of the application, no other applications are affected π«Ά.
Notice also that the transient type is of little practical use, because when a supervision tree terminates, the reason is set to shutdown, not normal. So like... it's giving false promises ngl π.
start_type() -> StartType | undefined | local
when StartType :: start_type().
This function is intended to be called by a process belonging to an application, when the application is started, to determine the start type, which is StartType or local π.
For a description of StartType, see Module:start/2.
local is returned if only parts of the application are restarted (by a supervisor), or if the function is called outside a startup β¨.
If the process executing the call does not belong to any application, the function returns undefined. Mystery vibe π΅οΈ.
stop(Application) -> ok | {error, Reason}
when Application :: atom(), Reason :: term().
Stops Application π. The application master calls Module:prep_stop/1, if such a function is defined, and then tells the top supervisor of the application to shut down (see supervisor).
This means that the entire supervision tree, including included applications, is terminated in reversed start order βͺ. After the shutdown, the application master calls Module:stop/1. Module is the callback module as defined by the application specification key mod.
Last, the application master terminates. Notice that all processes with the application master as group leader β that is, processes spawned from a process belonging to the application β are also terminated π.
When stopped, the application is still loaded β¨.
To stop a distributed application, stop/1 must be called on all nodes where it can execute (that is, on all nodes where it has been started). The call to stop/1 on the node where the application currently executes stops its execution. The application is not moved between nodes, as stop/1 is called on the node where the application currently executes before stop/1 is called on the other nodes. Team effort bestie π«Ά.
takeover(Application, Type) -> ok | {error, Reason}
when Application :: atom(), Type :: restart_type(), Reason :: term().
Takes over the distributed application Application, which executes at another node Node π.
At the current node, the application gets restarted by calling Module:start({takeover,Node},StartArgs) β yes, bestie, exactly that call. Module and StartArgs are retrieved from the loaded application specification. The application at the other node is not stopped until the startup is completed, that is, when Module:start/2 and any calls to Module:start_phase/3 have returned. iykyk β¨
Thus, two instances of the application run simultaneously during the takeover, so that data can be transferred from the old to the new instance. It's giving parallel universe energy. π If this is not an acceptable behavior, parts of the old instance can be shut down when the new instance is started. However, the application cannot be stopped entirely β at least the top supervisor must remain alive. Periodt. π
For a description of Type, see start/1,2.
unload/1
unload(Application) -> ok | {error, Reason}
when Application :: atom(), Reason :: term().
Unloads the application specification for Application from the application controller. It also unloads the application specifications for any included applications. But heads up: it does not purge the Erlang object code. So the code stays, the spec dips. π«Ά
unset_env/3
unset_env(Application, Par, Opts) -> ok
when
Application :: atom(),
Par :: atom(),
Opts ::
[{timeout, timeout()} |
{persistent, boolean()}].
Removes the configuration parameter Par and its value for Application.
unset_env/3 uses the standard gen_server time-out value (5000 ms). Option timeout can be specified if another time-out value is useful, for example, in situations where the application controller is heavily loaded.
unset_env/3 also allows the persistent option to be passed (see set_env/4).
WARNING: Use this function only if you know what you are doing, that is, on your own applications. It is very application-dependent and configuration parameter-dependent when and how often the value is read by the application. Careless use of this function can put the application in a weird, inconsistent, and malfunctioning state. Not cute. π
which_applications/1
which_applications(Timeout) -> [{Application, Description, Vsn}]
when
Timeout :: timeout(),
Application :: atom(),
Description :: string(),
Vsn :: string().
Returns a list with information about the applications that are currently running.
Application is the application name. Description and Vsn are the values of their description and vsn application specification keys, respectively.
A Timeout argument can be specified in situations where the application controller is heavily loaded. Slay. β¨
π CALLBACKS π
config_change(Changed, New, Removed)
This function is called by an application after a code replacement, if the configuration parameters have changed.
Changed is a list of parameter-value tuples including all configuration parameters with changed values.
New is a list of parameter-value tuples including all added configuration parameters.
Removed is a list of all removed parameters. Bye felicia. π
prep_stop(State)
This function is called when an application is about to be stopped, before shutting down the processes of the application.
State is the state returned from Module:start/2, or [] if no state was returned. NewState is any term and is passed to Module:stop/1.
The function is optional. If it is not defined, the processes are terminated and then Module:stop(State) is called. No stress, it's flexible. π«Ά
start(Application, Type)
This function is called whenever an application is started using start/1,2, and is to start the processes of the application. If the application is structured according to the OTP design principles as a supervision tree, this means starting the top supervisor of the tree. Main character energy. π
StartType defines the type of start:
normalif it is a normal startup.normalalso if the application is distributed and started at the current node because of a failover from another node, and the application specification keystart_phases == undefined.{takeover,Node}if the application is distributed and started at the current node because of a takeover fromNode, either becausetakeover/2has been called or because the current node has higher priority thanNode.{failover,Node}if the application is distributed and started at the current node because of a failover fromNode, and the application specification keystart_phases /= undefined.
StartArgs is the StartArgs argument defined by the application specification key mod.
The function is to return {ok,Pid} or {ok,Pid,State}, where Pid is the pid of the top supervisor and State is any term. If omitted, State defaults to []. If the application is stopped later, State is passed to Module:prep_stop/1. Iconic. π
start_phase(Phase, StartType, PhaseArgs)
Starts an application with included applications, when synchronization is needed between processes in the different applications during startup.
The start phases are defined by the application specification key start_phases == [{Phase,PhaseArgs}]. For included applications, the set of phases must be a subset of the set of phases defined for the including application.
The function is called for each start phase (as defined for the primary application) for the primary application and all included applications, for which the start phase is defined.
For a description of StartType, see Module:start/2. Sync up, besties. π¦
stop(Application)
This function is called whenever an application has stopped. It is intended to be the opposite of Module:start/2 and is to do any necessary cleaning up. The return value is ignored.
State is the return value of Module:prep_stop/1, if such a function exists. Otherwise State is taken from the return value of Module:start/2. Clean up on aisle you. π§Ήβ¨
And that's the whole page, bestie! This was application(3) from Ericsson AB, version kernel 11.0.3. No cap, you're now lowkey an OTP application wizard. Periodt. ππ
π¦ where this page comes from
- ubuntu stonking: erlang-doc 1:29.0.6+dfsg-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms