1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
9
<holder>Ericsson AB, All Rights Reserved</holder>
12
The contents of this file are subject to the Erlang Public License,
13
Version 1.1, (the "License"); you may not use this file except in
14
compliance with the License. You should have received a copy of the
15
Erlang Public License along with this software. If not, it can be
16
retrieved online at http://www.erlang.org/.
18
Software distributed under the License is distributed on an "AS IS"
19
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
the License for the specific language governing rights and limitations
23
The Initial Developer of the Original Code is Ericsson AB.
26
<title>application</title>
32
<module>application</module>
33
<modulesummary>Generic OTP application functions</modulesummary>
35
<p>In OTP, <em>application</em> denotes a component implementing
36
some specific functionality, that can be started and stopped as a
37
unit, and which can be re-used in other systems as well. This
38
module interfaces the <em>application controller</em>, a process
39
started at every Erlang runtime system, and contains functions
40
for controlling applications (for example starting and stopping
41
applications), and functions to access information about
42
applications (for example configuration parameters).</p>
43
<p>An application is defined by an <em>application specification</em>. The specification is normally located in an
44
<em>application resource file</em> called <c>Application.app</c>,
45
where <c>Application</c> is the name of the application. Refer to
46
<seealso marker="app">app(4)</seealso> for more information about
47
the application specification.</p>
48
<p>This module can also be viewed as a behaviour for an application
49
implemented according to the OTP design principles as a
50
supervision tree. The definition of how to start and stop
51
the tree should be located in an <em>application callback module</em> exporting a pre-defined set of functions.</p>
52
<p>Refer to <seealso marker="doc/design_principles:des_princ">OTP Design Principles</seealso> for more information about
53
applications and behaviours.</p>
57
<name>get_all_env() -> Env</name>
58
<name>get_all_env(Application) -> Env</name>
59
<fsummary>Get the configuration parameters for an application</fsummary>
61
<v>Application = atom()</v>
62
<v>Env = [{Par,Val}]</v>
63
<v> Par = atom()</v>
64
<v> Val = term()</v>
67
<p>Returns the configuration parameters and their values for
68
<c>Application</c>. If the argument is omitted, it defaults to
69
the application of the calling process.</p>
70
<p>If the specified application is not loaded, or if the process
71
executing the call does not belong to any application,
72
the function returns <c>[]</c>.</p>
76
<name>get_all_key() -> {ok, Keys} | []</name>
77
<name>get_all_key(Application) -> {ok, Keys} | undefined </name>
78
<fsummary>Get the application specification keys</fsummary>
80
<v>Application = atom()</v>
81
<v>Keys = [{Key,Val}]</v>
82
<v> Key = atom()</v>
83
<v> Val = term()</v>
86
<p>Returns the application specification keys and their values
87
for <c>Application</c>. If the argument is omitted, it
88
defaults to the application of the calling process.</p>
89
<p>If the specified application is not loaded, the function
90
returns <c>undefined</c>. If the process executing the call
91
does not belong to any application, the function returns
96
<name>get_application() -> {ok, Application} | undefined</name>
97
<name>get_application(Pid | Module) -> {ok, Application} | undefined</name>
98
<fsummary>Get the name of an application containing a certain process or module</fsummary>
101
<v>Module = atom()</v>
102
<v>Application = atom()</v>
105
<p>Returns the name of the application to which the process
106
<c>Pid</c> or the module <c>Module</c> belongs. Providing no
107
argument is the same as calling
108
<c>get_application(self())</c>.</p>
109
<p>If the specified process does not belong to any application,
110
or if the specified process or module does not exist,
111
the function returns <c>undefined</c>.</p>
115
<name>get_env(Par) -> {ok, Val} | undefined</name>
116
<name>get_env(Application, Par) -> {ok, Val} | undefined</name>
117
<fsummary>Get the value of a configuration parameter</fsummary>
119
<v>Application = atom()</v>
124
<p>Returns the value of the configuration parameter <c>Par</c>
125
for <c>Application</c>. If the application argument is
126
omitted, it defaults to the application of the calling
128
<p>If the specified application is not loaded, or
129
the configuration parameter does not exist, or if the process
130
executing the call does not belong to any application,
131
the function returns <c>undefined</c>.</p>
135
<name>get_key(Key) -> {ok, Val} | undefined</name>
136
<name>get_key(Application, Key) -> {ok, Val} | undefined</name>
137
<fsummary>Get the value of an application specification key</fsummary>
139
<v>Application = atom()</v>
144
<p>Returns the value of the application specification key
145
<c>Key</c> for <c>Application</c>. If the application
146
argument is omitted, it defaults to the application of
147
the calling process.</p>
148
<p>If the specified application is not loaded, or
149
the specification key does not exist, or if the process
150
executing the call does not belong to any application,
151
the function returns <c>undefined</c>.</p>
155
<name>load(AppDescr) -> ok | {error, Reason}</name>
156
<name>load(AppDescr, Distributed) -> ok | {error, Reason}</name>
157
<fsummary>Load an application</fsummary>
159
<v>AppDescr = Application | AppSpec</v>
160
<v> Application = atom()</v>
161
<v> AppSpec = {application,Application,AppSpecKeys}</v>
162
<v> AppSpec = [{Key,Val}]</v>
163
<v> Key = atom()</v>
164
<v> Val = term()</v>
165
<v>Distributed = {Application,Nodes} | {Application,Time,Nodes} | default</v>
166
<v> Nodes = [node() | {node(),..,node()}]</v>
167
<v> Time = integer() > 0</v>
168
<v>Reason = term()</v>
171
<p>Loads the application specification for an application into
172
the application controller. It will also load the application
173
specifications for any included applications. Note that
174
the function does not load the actual Erlang object code.</p>
175
<p>The application can be given by its name <c>Application</c>.
176
In this case the application controller will search the code
177
path for the application resource file <c>Application.app</c>
178
and load the specification it contains.</p>
179
<p>The application specification can also be given directly as a
180
tuple <c>AppSpec</c>. This tuple should have the format and
181
contents as described in <c>app(4)</c>.</p>
182
<p>If <c>Distributed == {Application,[Time,]Nodes}</c>,
183
the application will be distributed. The argument overrides
184
the value for the application in the Kernel configuration
185
parameter <c>distributed</c>. <c>Application</c> must be
186
the name of the application (same as in the first argument).
187
If a node crashes and <c>Time</c> has been specified, then
188
the application controller will wait for <c>Time</c>
189
milliseconds before attempting to restart the application on
190
another node. If <c>Time</c> is not specified, it will
191
default to 0 and the application will be restarted
193
<p><c>Nodes</c> is a list of node names where the application
194
may run, in priority from left to right. Node names can be
195
grouped using tuples to indicate that they have the same
196
priority. Example:</p>
198
Nodes = [cp1@cave, {cp2@cave, cp3@cave}]</code>
199
<p>This means that the application should preferably be started
200
at <c>cp1@cave</c>. If <c>cp1@cave</c> is down,
201
the application should be started at either <c>cp2@cave</c>
202
or <c>cp3@cave</c>.</p>
203
<p>If <c>Distributed == default</c>, the value for
204
the application in the Kernel configuration parameter
205
<c>distributed</c> will be used.</p>
209
<name>loaded_applications() -> [{Application, Description, Vsn}]</name>
210
<fsummary>Get the currently loaded applications</fsummary>
212
<v>Application = atom()</v>
213
<v>Description = string()</v>
214
<v>Vsn = string()</v>
217
<p>Returns a list with information about the applications which
218
have been loaded using <c>load/1,2</c>, also included
219
applications. <c>Application</c> is the application name.
220
<c>Description</c> and <c>Vsn</c> are the values of its
221
<c>description</c> and <c>vsn</c> application specification
222
keys, respectively.</p>
226
<name>permit(Application, Bool) -> ok | {error, Reason}</name>
227
<fsummary>Change an application's permission to run on a node.</fsummary>
229
<v>Application = atom()</v>
231
<v>Reason = term()</v>
234
<p>Changes the permission for <c>Application</c> to run at
235
the current node. The application must have been loaded using
236
<c>load/1,2</c> for the function to have effect.</p>
237
<p>If the permission of a loaded, but not started, application
238
is set to <c>false</c>, <c>start</c> will return <c>ok</c> but
239
the application will not be started until the permission is
240
set to <c>true</c>.</p>
241
<p>If the permission of a running application is set to
242
<c>false</c>, the application will be stopped. If
243
the permission later is set to <c>true</c>, it will be
245
<p>If the application is distributed, setting the permission to
246
<c>false</c> means that the application will be started at, or
247
moved to, another node according to how its distribution is
248
configured (see <c>load/2</c> above).</p>
249
<p>The function does not return until the application is
250
started, stopped or successfully moved to another node.
251
However, in some cases where permission is set to <c>true</c>
252
the function may return <c>ok</c> even though the application
253
itself has not started. This is true when an application
254
cannot start because it has dependencies to other
255
applications which have not yet been started. When they have
256
been started, <c>Application</c> will be started as well.</p>
257
<p>By default, all applications are loaded with permission
258
<c>true</c> on all nodes. The permission is configurable by
259
using the Kernel configuration parameter <c>permissions</c>.</p>
263
<name>set_env(Application, Par, Val) -> ok</name>
264
<name>set_env(Application, Par, Val, Timeout) -> ok</name>
265
<fsummary>Set the value of a configuration parameter</fsummary>
267
<v>Application = atom()</v>
270
<v>Timeout = int() | infinity</v>
273
<p>Sets the value of the configuration parameter <c>Par</c> for
274
<c>Application</c>.</p>
275
<p><c>set_env/3</c> uses the standard <c>gen_server</c> timeout
276
value (5000 ms). A <c>Timeout</c> argument can be provided
277
if another timeout value is useful, for example, in situations
278
where the application controller is heavily loaded.</p>
280
<p>Use this function only if you know what you are doing,
281
that is, on your own applications. It is very application
282
and configuration parameter dependent when and how often
283
the value is read by the application, and careless use
284
of this function may put the application in a
285
weird, inconsistent, and malfunctioning state. </p>
290
<name>start(Application) -> ok | {error, Reason}</name>
291
<name>start(Application, Type) -> ok | {error, Reason}</name>
292
<fsummary>Load and start an application</fsummary>
294
<v>Application = atom()</v>
295
<v>Type = permanent | transient | temporary</v>
296
<v>Reason = term()</v>
299
<p>Starts <c>Application</c>. If it is not loaded,
300
the application controller will first load it using
301
<c>load/1</c>. It will make sure any included applications
302
are loaded, but will not start them. That is assumed to be
303
taken care of in the code for <c>Application</c>.</p>
304
<p>The application controller checks the value of
305
the application specification key <c>applications</c>, to
306
ensure that all applications that should be started before
307
this application are running. If not,
308
<c>{error,{not_started,App}}</c> is returned, where <c>App</c>
309
is the name of the missing application.</p>
310
<p>The application controller then creates an <em>application master</em> for the application. The application master is
311
the group leader of all the processes in the application.
312
The application master starts the application by calling
313
the application callback function <c>Module:start/2</c> as
314
defined by the application specification key <c>mod</c>.</p>
315
<p>The <c>Type</c> argument specifies the type of
316
the application. If omitted, it defaults to <c>temporary</c>.</p>
317
<list type="bulleted">
318
<item>If a permanent application terminates, all other
319
applications and the entire Erlang node are also terminated.</item>
320
<item>If a transient application terminates with <c>Reason == normal</c>, this is reported but no other applications are
321
terminated. If a transient application terminates
322
abnormally, all other applications and the entire Erlang
323
node are also terminated.</item>
324
<item>If a temporary application terminates, this is reported
325
but no other applications are terminated.</item>
327
<p>Note that it is always possible to stop an application
328
explicitly by calling <c>stop/1</c>. Regardless of the type of
329
the application, no other applications will be affected.</p>
330
<p>Note also that the transient type is of little practical use,
331
since when a supervision tree terminates, the reason is set to
332
<c>shutdown</c>, not <c>normal</c>.</p>
336
<name>start_type() -> StartType | local | undefined</name>
337
<fsummary>Get the start type of an ongoing application startup.</fsummary>
339
<v>StartType = normal | {takeover,Node} | {failover,Node}</v>
340
<v> Node = node()</v>
343
<p>This function is intended to be called by a process belonging
344
to an application, when the application is being started, to
345
determine the start type which is either <c>StartType</c> or
347
<p>See <c>Module:start/2</c> for a description of
348
<c>StartType</c>.</p>
349
<p><c>local</c> is returned if only parts of the application is
350
being restarted (by a supervisor), or if the function is
351
called outside a startup.</p>
352
<p>If the process executing the call does not belong to any
353
application, the function returns <c>undefined</c>.</p>
357
<name>stop(Application) -> ok | {error, Reason}</name>
358
<fsummary>Stop an application</fsummary>
360
<v>Application = atom()</v>
361
<v>Reason = term()</v>
364
<p>Stops <c>Application</c>. The application master calls
365
<c>Module:prep_stop/1</c>, if such a function is defined, and
366
then tells the top supervisor of the application to shutdown
367
(see <c>supervisor(3)</c>). This means that the entire
368
supervision tree, including included applications, is
369
terminated in reversed start order. After the shutdown,
370
the application master calls <c>Module:stop/1</c>.
371
<c>Module</c> is the callback module as defined by
372
the application specification key <c>mod</c>.</p>
373
<p>Last, the application master itself terminates. Note that all
374
processes with the application master as group leader, i.e.
375
processes spawned from a process belonging to the application,
376
thus are terminated as well.</p>
377
<p>When stopped, the application is still loaded.</p>
378
<p>In order to stop a distributed application, <c>stop/1</c>
379
has to be called on all nodes where it can execute (that is,
380
on all nodes where it has been started). The call to
381
<c>stop/1</c> on the node where the application currently
382
executes will stop its execution. The application will not be
383
moved between nodes due to <c>stop/1</c> being called on
384
the node where the application currently executes before
385
<c>stop/1</c> is called on the other nodes.</p>
389
<name>takeover(Application, Type) -> ok | {error, Reason}</name>
390
<fsummary>Take over a distributed application</fsummary>
392
<v>Application = atom()</v>
393
<v>Type = permanent | transient | temporary</v>
394
<v>Reason = term()</v>
397
<p>Performs a takeover of the distributed application
398
<c>Application</c>, which executes at another node
399
<c>Node</c>. At the current node, the application is
401
<c>Module:start({takeover,Node},StartArgs)</c>. <c>Module</c>
402
and <c>StartArgs</c> are retrieved from the loaded application
403
specification. The application at the other node is not
404
stopped until the startup is completed, i.e. when
405
<c>Module:start/2</c> and any calls to
406
<c>Module:start_phase/3</c> have returned.</p>
407
<p>Thus two instances of the application will run simultaneously
408
during the takeover, which makes it possible to transfer data
409
from the old to the new instance. If this is not acceptable
410
behavior, parts of the old instance may be shut down when
411
the new instance is started. Note that the application may
412
not be stopped entirely however, at least the top supervisor
413
must remain alive.</p>
414
<p>See <c>start/1,2</c> for a description of <c>Type</c>.</p>
418
<name>unload(Application) -> ok | {error, Reason}</name>
419
<fsummary>Unload an application</fsummary>
421
<v>Application = atom()</v>
422
<v>Reason = term()</v>
425
<p>Unloads the application specification for <c>Application</c>
426
from the application controller. It will also unload
427
the application specifications for any included applications.
428
Note that the function does not purge the actual Erlang
433
<name>unset_env(Application, Par) -> ok</name>
434
<name>unset_env(Application, Par, Timeout) -> ok</name>
435
<fsummary>Unset the value of a configuration parameter</fsummary>
437
<v>Application = atom()</v>
439
<v>Timeout = int() | infinity</v>
442
<p>Removes the configuration parameter <c>Par</c> and its value
443
for <c>Application</c>.</p>
444
<p><c>unset_env/2</c> uses the standard <c>gen_server</c>
445
timeout value (5000 ms). A <c>Timeout</c> argument can be
446
provided if another timeout value is useful, for example, in
447
situations where the application controller is heavily loaded.</p>
449
<p>Use this function only if you know what you are doing,
450
that is, on your own applications. It is very application
451
and configuration parameter dependent when and how often
452
the value is read by the application, and careless use
453
of this function may put the application in a
454
weird, inconsistent, and malfunctioning state. </p>
459
<name>which_applications() -> [{Application, Description, Vsn}]</name>
460
<name>which_applications(Timeout) -> [{Application, Description, Vsn}]</name>
461
<fsummary>Get the currently running applications</fsummary>
463
<v>Application = atom()</v>
464
<v>Description = string()</v>
465
<v>Vsn = string()</v>
466
<v>Timeout = int() | infinity</v>
469
<p>Returns a list with information about the applications which
470
are currently running. <c>Application</c> is the application
471
name. <c>Description</c> and <c>Vsn</c> are the values of its
472
<c>description</c> and <c>vsn</c> application specfication
473
keys, respectively.</p>
474
<p><c>which_applications/0</c> uses the standard
475
<c>gen_server</c> timeout value (5000 ms). A <c>Timeout</c>
476
argument can be provided if another timeout value is useful,
477
for example, in situations where the application controller
478
is heavily loaded.</p>
484
<title>CALLBACK MODULE</title>
485
<p>The following functions should be exported from an
486
<c>application</c> callback module.</p>
490
<name>Module:start(StartType, StartArgs) -> {ok, Pid} | {ok, Pid, State} | {error, Reason}</name>
491
<fsummary>Start an application</fsummary>
493
<v>StartType = normal | {takeover,Node} | {failover,Node}</v>
494
<v> Node = node()</v>
495
<v>StartArgs = term()</v>
497
<v>State = term()</v>
500
<p>This function is called whenever an application is started
501
using <c>application:start/1,2</c>, and should start
502
the processes of the application. If the application is
503
structured according to the OTP design principles as a
504
supervision tree, this means starting the top supervisor of
506
<p><c>StartType</c> defines the type of start:</p>
507
<list type="bulleted">
508
<item><c>normal</c> if its a normal startup.</item>
509
<item><c>normal</c> also if the application is distributed and
510
started at the current node due to a failover from another
511
node, and the application specification key <c>start_phases == undefined</c>.</item>
512
<item><c>{takeover,Node}</c> if the application is
513
distributed and started at the current node due to a
514
takeover from <c>Node</c>, either because
515
<c>application:takeover/2</c> has been called or because
516
the current node has higher priority than <c>Node</c>.</item>
517
<item><c>{failover,Node}</c> if the application is
518
distributed and started at the current node due to a
519
failover from <c>Node</c>, and the application
520
specification key <c>start_phases /= undefined</c>.</item>
522
<p><c>StartArgs</c> is the <c>StartArgs</c> argument defined by
523
the application specification key <c>mod</c>.</p>
524
<p>The function should return <c>{ok,Pid}</c> or
525
<c>{ok,Pid,State}</c> where <c>Pid</c> is the pid of the top
526
supervisor and <c>State</c> is any term. If omitted,
527
<c>State</c> defaults to <c>[]</c>. If later the application
528
is stopped, <c>State</c> is passed to
529
<c>Module:prep_stop/1</c>.</p>
533
<name>Module:start_phase(Phase, StartType, PhaseArgs) -> ok | {error, Reason}</name>
534
<fsummary>Extended start of an application</fsummary>
536
<v>Phase = atom()</v>
537
<v>StartType = normal | {takeover,Node} | {failover,Node}</v>
538
<v> Node = node()</v>
539
<v>PhaseArgs = term()</v>
541
<v>State = state()</v>
544
<p>This function is used to start an application with included
545
applications, when there is a need for synchronization between
546
processes in the different applications during startup.</p>
547
<p>The start phases is defined by the application specification
548
key <c>start_phases == [{Phase,PhaseArgs}]</c>. For included
549
applications, the set of phases must be a subset of the set of
550
phases defined for the including application.</p>
551
<p>The function is called for each start phase (as defined for
552
the primary appliction) for the primary application and all
553
included applications, for which the start phase is defined.</p>
554
<p>See <c>Module:start/2</c> for a description of
555
<c>StartType</c>.</p>
559
<name>Module:prep_stop(State) -> NewState</name>
560
<fsummary>Prepare an application for termination</fsummary>
562
<v>State = NewState = term()</v>
565
<p>This function is called when an application is about to be
566
stopped, before shutting down the processes of
568
<p><c>State</c> is the state returned from
569
<c>Module:start/2</c>, or <c>[]</c> if no state was returned.
570
<c>NewState</c> is any term and will be passed to
571
<c>Module:stop/1</c>.</p>
572
<p>The function is optional. If it is not defined, the processes
573
will be terminated and then <c>Module:stop(State)</c> is
578
<name>Module:stop(State)</name>
579
<fsummary>Clean up after termination of an application</fsummary>
581
<v>State = term()</v>
584
<p>This function is called whenever an application has stopped.
585
It is intended to be the opposite of <c>Module:start/2</c>
586
and should do any necessary cleaning up. The return value is
588
<p><c>State</c> is the return value of
589
<c>Module:prep_stop/1</c>, if such a function exists.
590
Otherwise <c>State</c> is taken from the return value of
591
<c>Module:start/2</c>.</p>
595
<name>Module:config_change(Changed, New, Removed) -> ok</name>
596
<fsummary>Update the configuration parameters for an application.</fsummary>
598
<v>Changed = [{Par,Val}]</v>
599
<v>New = [{Par,Val}]</v>
600
<v>Removed = [Par]</v>
601
<v> Par = atom()</v>
602
<v> Val = term()</v>
605
<p>This function is called by an application after a code
606
replacement, if there are any changes to the configuration
608
<p><c>Changed</c> is a list of parameter-value tuples with all
609
configuration parameters with changed values, <c>New</c> is
610
a list of parameter-value tuples with all configuration
611
parameters that have been added, and <c>Removed</c> is a list
612
of all parameters that have been removed.</p>
618
<title>SEE ALSO</title>
619
<p><seealso marker="doc/design_principles:des_princ">OTP Design Principles</seealso>,
620
<seealso marker="kernel_app">kernel(6)</seealso>,
621
<seealso marker="app">app(4)</seealso></p>