1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>1996</year><year>2013</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
The contents of this file are subject to the Erlang Public License,
Version 1.1, (the "License"); you may not use this file except in
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
</legalnotice>
<title>application</title>
<prepared></prepared>
<docno></docno>
<date></date>
<rev></rev>
</header>
<module>application</module>
<modulesummary>Generic OTP application functions</modulesummary>
<description>
<p>In OTP, <em>application</em> denotes a component implementing
some specific functionality, that can be started and stopped as a
unit, and which can be re-used in other systems as well. This
module interfaces the <em>application controller</em>, a process
started at every Erlang runtime system, and contains functions
for controlling applications (for example starting and stopping
applications), and functions to access information about
applications (for example configuration parameters).</p>
<p>An application is defined by an <em>application specification</em>. The specification is normally located in an
<em>application resource file</em> called <c>Application.app</c>,
where <c>Application</c> is the name of the application. Refer to
<seealso marker="app">app(4)</seealso> for more information about
the application specification.</p>
<p>This module can also 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 should be located in an <em>application callback module</em> exporting a pre-defined set of functions.</p>
<p>Refer to <seealso marker="doc/design_principles:des_princ">OTP Design Principles</seealso> for more information about
applications and behaviours.</p>
</description>
<datatypes>
<datatype>
<name name="start_type"/>
</datatype>
<datatype>
<name name="restart_type"/>
</datatype>
<datatype>
<!-- Parameterized opaque types are NYI: -->
<name><marker id="type-tuple_of">tuple_of(T)</marker></name>
<desc><p>A tuple where the elements are of type <c>T</c>.</p></desc>
</datatype>
</datatypes>
<funcs>
<func>
<name name="get_all_env" arity="0"/>
<name name="get_all_env" arity="1"/>
<fsummary>Get the configuration parameters for an application</fsummary>
<desc>
<p>Returns the configuration parameters and their values for
<c><anno>Application</anno></c>. If the argument is omitted, it defaults to
the application of the calling process.</p>
<p>If the specified application is not loaded, or if the process
executing the call does not belong to any application,
the function returns <c>[]</c>.</p>
</desc>
</func>
<func>
<name name="get_all_key" arity="0"/>
<name name="get_all_key" arity="1"/>
<fsummary>Get the application specification keys</fsummary>
<desc>
<p>Returns the application specification keys and their values
for <c><anno>Application</anno></c>. If the argument is omitted, it
defaults to the application of the calling process.</p>
<p>If the specified application is not loaded, the function
returns <c>undefined</c>. If the process executing the call
does not belong to any application, the function returns
<c>[]</c>.</p>
</desc>
</func>
<func>
<name name="get_application" arity="0"/>
<name name="get_application" arity="1"/>
<fsummary>Get the name of an application containing a certain process or module</fsummary>
<desc>
<p>Returns the name of the application to which the process
<c><anno>Pid</anno></c> or the module <c><anno>Module</anno></c> belongs. Providing no
argument is the same as calling
<c>get_application(self())</c>.</p>
<p>If the specified process does not belong to any application,
or if the specified process or module does not exist,
the function returns <c>undefined</c>.</p>
</desc>
</func>
<func>
<name name="get_env" arity="1"/>
<name name="get_env" arity="2"/>
<fsummary>Get the value of a configuration parameter</fsummary>
<desc>
<p>Returns the value of the configuration parameter <c><anno>Par</anno></c>
for <c><anno>Application</anno></c>. If the application argument is
omitted, it defaults to the application of the calling
process.</p>
<p>If the specified application is not loaded, or
the configuration parameter does not exist, or if the process
executing the call does not belong to any application,
the function returns <c>undefined</c>.</p>
</desc>
</func>
<func>
<name name="get_env" arity="3"/>
<fsummary>Get the value of a configuration parameter using a default</fsummary>
<desc>
<p>Works like <seealso marker="#get_env/2">get_env/2</seealso> but returns
<c><anno>Def</anno></c> value when configuration parameter
<c><anno>Par</anno></c> does not exist.</p>
</desc>
</func>
<func>
<name name="get_key" arity="1"/>
<name name="get_key" arity="2"/>
<fsummary>Get the value of an application specification key</fsummary>
<desc>
<p>Returns the value of the application specification key
<c><anno>Key</anno></c> for <c><anno>Application</anno></c>. If the application
argument is omitted, it defaults to the application of
the calling process.</p>
<p>If the specified application is not loaded, or
the specification key does not exist, or if the process
executing the call does not belong to any application,
the function returns <c>undefined</c>.</p>
</desc>
</func>
<func>
<name name="load" arity="1"/>
<name name="load" arity="2"/>
<fsummary>Load an application</fsummary>
<type name="application_spec"/>
<type name="application_opt"/>
<desc>
<p>Loads the application specification for an application into
the application controller. It will also load the application
specifications for any included applications. Note that
the function does not load the actual Erlang object code.</p>
<p>The application can be given by its name <c><anno>Application</anno></c>.
In this case the application controller will search the code
path for the application resource file <c><anno>Application</anno>.app</c>
and load the specification it contains.</p>
<p>The application specification can also be given directly as a
tuple <c><anno>AppSpec</anno></c>. This tuple should have the format and
contents as described in <c>app(4)</c>.</p>
<p>If <c><anno>Distributed</anno> == {<anno>Application</anno>,[<anno>Time</anno>,]<anno>Nodes</anno>}</c>,
the application will be distributed. The argument overrides
the value for the application in the Kernel configuration
parameter <c>distributed</c>. <c><anno>Application</anno></c> must be
the name of the application (same as in the first argument).
If a node crashes and <c><anno>Time</anno></c> has been specified, then
the application controller will wait for <c><anno>Time</anno></c>
milliseconds before attempting to restart the application on
another node. If <c><anno>Time</anno></c> is not specified, it will
default to 0 and the application will be restarted
immediately.</p>
<p><c><anno>Nodes</anno></c> is a list of node names where the application
may run, in priority from left to right. Node names can be
grouped using tuples to indicate that they have the same
priority. Example:</p>
<code type="none">
Nodes = [cp1@cave, {cp2@cave, cp3@cave}]</code>
<p>This means that the application should preferably be started
at <c>cp1@cave</c>. If <c>cp1@cave</c> is down,
the application should be started at either <c>cp2@cave</c>
or <c>cp3@cave</c>.</p>
<p>If <c>Distributed == default</c>, the value for
the application in the Kernel configuration parameter
<c>distributed</c> will be used.</p>
</desc>
</func>
<func>
<name name="loaded_applications" arity="0"/>
<fsummary>Get the currently loaded applications</fsummary>
<desc>
<p>Returns a list with information about the applications which
have been loaded using <c>load/1,2</c>, also included
applications. <c><anno>Application</anno></c> is the application name.
<c><anno>Description</anno></c> and <c><anno>Vsn</anno></c> are the values of its
<c>description</c> and <c>vsn</c> application specification
keys, respectively.</p>
</desc>
</func>
<func>
<name name="permit" arity="2"/>
<fsummary>Change an application's permission to run on a node.</fsummary>
<desc>
<p>Changes the permission for <c><anno>Application</anno></c> to run at
the current node. The application must have been loaded using
<c>load/1,2</c> for the function to have effect.</p>
<p>If the permission of a loaded, but not started, application
is set to <c>false</c>, <c>start</c> will return <c>ok</c> but
the application will not be started until the permission is
set to <c>true</c>.</p>
<p>If the permission of a running application is set to
<c>false</c>, the application will be stopped. If
the permission later is set to <c>true</c>, it will be
restarted.</p>
<p>If the application is distributed, setting the permission to
<c>false</c> means that the application will be started at, or
moved to, another node according to how its distribution is
configured (see <c>load/2</c> above).</p>
<p>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 <c>true</c>
the function may return <c>ok</c> even though the application
itself has not started. This is true when an application
cannot start because it has dependencies to other
applications which have not yet been started. When they have
been started, <c>Application</c> will be started as well.</p>
<p>By default, all applications are loaded with permission
<c>true</c> on all nodes. The permission is configurable by
using the Kernel configuration parameter <c>permissions</c>.</p>
</desc>
</func>
<func>
<name name="set_env" arity="3"/>
<name name="set_env" arity="4"/>
<fsummary>Set the value of a configuration parameter</fsummary>
<desc>
<p>Sets the value of the configuration parameter <c><anno>Par</anno></c> for
<c><anno>Application</anno></c>.</p>
<p><c>set_env/3</c> uses the standard <c>gen_server</c> timeout
value (5000 ms). A <c><anno>Timeout</anno></c> argument can be provided
if another timeout value is useful, for example, in situations
where the application controller is heavily loaded.</p>
<warning>
<p>Use this function only if you know what you are doing,
that is, on your own applications. It is very application
and configuration parameter dependent when and how often
the value is read by the application, and careless use
of this function may put the application in a
weird, inconsistent, and malfunctioning state. </p>
</warning>
</desc>
</func>
<func>
<name name="ensure_started" arity="1"/>
<name name="ensure_started" arity="2"/>
<fsummary>Load and start an application</fsummary>
<desc>
<p>Equivalent to <seealso marker="#start/2"><c>application:start/1,2</c></seealso> except
it returns <c>ok</c> for already started applications.</p>
</desc>
</func>
<func>
<name name="ensure_all_started" arity="1"/>
<name name="ensure_all_started" arity="2"/>
<fsummary>Load and start an application and its dependencies, recursively</fsummary>
<desc>
<p>Equivalent to calling <seealso marker="#start/2"><c>application:start/1,2</c></seealso>
repeatedly on all dependencies that have not yet been started for an application.
The function returns <c>{ok, AppNames}</c> for a successful start or for an already started
application (which are however omitted from the <c>AppNames</c> list), and reports
<c>{error, {AppName,Reason}}</c> for errors, where <c>Reason</c> is any possible reason
returned by <seealso marker="#start/2"><c>application:start/1,2</c> when starting a
specific dependency. In case of an error, the applications that were started by the
function are stopped to bring the set of running applications back to its initial state.</p>
</desc>
</func>
<func>
<name name="start" arity="1"/>
<name name="start" arity="2"/>
<fsummary>Load and start an application</fsummary>
<desc>
<p>Starts <c><anno>Application</anno></c>. If it is not loaded,
the application controller will first load it using
<c>load/1</c>. It will make sure any included applications
are loaded, but will not start them. That is assumed to be
taken care of in the code for <c><anno>Application</anno></c>.</p>
<p>The application controller checks the value of
the application specification key <c>applications</c>, to
ensure that all applications that should be started before
this application are running. If not,
<c>{error,{not_started,App}}</c> is returned, where <c>App</c>
is the name of the missing application.</p>
<p>The application controller then creates an <em>application master</em> for the application. The application master is
the group leader of all the processes in the application.
The application master starts the application by calling
the application callback function <c>Module:start/2</c> as
defined by the application specification key <c>mod</c>.</p>
<p>The <c><anno>Type</anno></c> argument specifies the type of
the application. If omitted, it defaults to <c>temporary</c>.</p>
<list type="bulleted">
<item>If a permanent application terminates, all other
applications and the entire Erlang node are also terminated.</item>
<item>If a transient application terminates with <c>Reason == normal</c>, this is reported but no other applications are
terminated. If a transient application terminates
abnormally, all other applications and the entire Erlang
node are also terminated.</item>
<item>If a temporary application terminates, this is reported
but no other applications are terminated.</item>
</list>
<p>Note that it is always possible to stop an application
explicitly by calling <c>stop/1</c>. Regardless of the type of
the application, no other applications will be affected.</p>
<p>Note also that the transient type is of little practical use,
since when a supervision tree terminates, the reason is set to
<c>shutdown</c>, not <c>normal</c>.</p>
</desc>
</func>
<func>
<name name="start_type" arity="0"/>
<fsummary>Get the start type of an ongoing application startup.</fsummary>
<desc>
<p>This function is intended to be called by a process belonging
to an application, when the application is being started, to
determine the start type which is either <c><anno>StartType</anno></c> or
<c>local</c>.</p>
<p>See <seealso marker="#start_type"><c>Module:start/2</c></seealso> for a description of
<c><anno>StartType</anno></c>.</p>
<p><c>local</c> is returned if only parts of the application is
being restarted (by a supervisor), or if the function is
called outside a startup.</p>
<p>If the process executing the call does not belong to any
application, the function returns <c>undefined</c>.</p>
</desc>
</func>
<func>
<name name="stop" arity="1"/>
<fsummary>Stop an application</fsummary>
<desc>
<p>Stops <c><anno>Application</anno></c>. The application master calls
<c>Module:prep_stop/1</c>, if such a function is defined, and
then tells the top supervisor of the application to shutdown
(see <c>supervisor(3)</c>). This means that the entire
supervision tree, including included applications, is
terminated in reversed start order. After the shutdown,
the application master calls <c>Module:stop/1</c>.
<c>Module</c> is the callback module as defined by
the application specification key <c>mod</c>.</p>
<p>Last, the application master itself terminates. Note that all
processes with the application master as group leader, i.e.
processes spawned from a process belonging to the application,
thus are terminated as well.</p>
<p>When stopped, the application is still loaded.</p>
<p>In order to stop a distributed application, <c>stop/1</c>
has to be called on all nodes where it can execute (that is,
on all nodes where it has been started). The call to
<c>stop/1</c> on the node where the application currently
executes will stop its execution. The application will not be
moved between nodes due to <c>stop/1</c> being called on
the node where the application currently executes before
<c>stop/1</c> is called on the other nodes.</p>
</desc>
</func>
<func>
<name name="takeover" arity="2"/>
<fsummary>Take over a distributed application</fsummary>
<desc>
<p>Performs a takeover of the distributed application
<c><anno>Application</anno></c>, which executes at another node
<c>Node</c>. At the current node, the application is
restarted by calling
<c>Module:start({takeover,Node},StartArgs)</c>. <c>Module</c>
and <c>StartArgs</c> are retrieved from the loaded application
specification. The application at the other node is not
stopped until the startup is completed, i.e. when
<c>Module:start/2</c> and any calls to
<c>Module:start_phase/3</c> have returned.</p>
<p>Thus two instances of the application will run simultaneously
during the takeover, which makes it possible to transfer data
from the old to the new instance. If this is not acceptable
behavior, parts of the old instance may be shut down when
the new instance is started. Note that the application may
not be stopped entirely however, at least the top supervisor
must remain alive.</p>
<p>See <c>start/1,2</c> for a description of <c>Type</c>.</p>
</desc>
</func>
<func>
<name name="unload" arity="1"/>
<fsummary>Unload an application</fsummary>
<desc>
<p>Unloads the application specification for <c><anno>Application</anno></c>
from the application controller. It will also unload
the application specifications for any included applications.
Note that the function does not purge the actual Erlang
object code.</p>
</desc>
</func>
<func>
<name name="unset_env" arity="2"/>
<name name="unset_env" arity="3"/>
<fsummary>Unset the value of a configuration parameter</fsummary>
<desc>
<p>Removes the configuration parameter <c><anno>Par</anno></c> and its value
for <c><anno>Application</anno></c>.</p>
<p><c>unset_env/2</c> uses the standard <c>gen_server</c>
timeout value (5000 ms). A <c><anno>Timeout</anno></c> argument can be
provided if another timeout value is useful, for example, in
situations where the application controller is heavily loaded.</p>
<warning>
<p>Use this function only if you know what you are doing,
that is, on your own applications. It is very application
and configuration parameter dependent when and how often
the value is read by the application, and careless use
of this function may put the application in a
weird, inconsistent, and malfunctioning state. </p>
</warning>
</desc>
</func>
<func>
<name name="which_applications" arity="0"/>
<name name="which_applications" arity="1"/>
<fsummary>Get the currently running applications</fsummary>
<desc>
<p>Returns a list with information about the applications which
are currently running. <c><anno>Application</anno></c> is the application
name. <c><anno>Description</anno></c> and <c><anno>Vsn</anno></c> are the values of its
<c>description</c> and <c>vsn</c> application specification
keys, respectively.</p>
<p><c>which_applications/0</c> uses the standard
<c>gen_server</c> timeout value (5000 ms). A <c><anno>Timeout</anno></c>
argument can be provided if another timeout value is useful,
for example, in situations where the application controller
is heavily loaded.</p>
</desc>
</func>
</funcs>
<section>
<title>CALLBACK MODULE</title>
<p>The following functions should be exported from an
<c>application</c> callback module.</p>
</section>
<funcs>
<func>
<name>Module:start(StartType, StartArgs) -> {ok, Pid} | {ok, Pid, State} | {error, Reason}</name>
<fsummary>Start an application</fsummary>
<type>
<v>StartType = normal | {takeover,Node} | {failover,Node}</v>
<v> Node = node()</v>
<v>StartArgs = term()</v>
<v>Pid = pid()</v>
<v>State = term()</v>
</type>
<desc>
<p>This function is called whenever an application is started
using <c>application:start/1,2</c>, and should 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.</p>
<p><marker id="start_type"/><c>StartType</c> defines the type of start:</p>
<list type="bulleted">
<item><c>normal</c> if it's a normal startup.</item>
<item><c>normal</c> also if the application is distributed and
started at the current node due to a failover from another
node, and the application specification key <c>start_phases == undefined</c>.</item>
<item><c>{takeover,Node}</c> if the application is
distributed and started at the current node due to a
takeover from <c>Node</c>, either because
<c>application:takeover/2</c> has been called or because
the current node has higher priority than <c>Node</c>.</item>
<item><c>{failover,Node}</c> if the application is
distributed and started at the current node due to a
failover from <c>Node</c>, and the application
specification key <c>start_phases /= undefined</c>.</item>
</list>
<p><c>StartArgs</c> is the <c>StartArgs</c> argument defined by
the application specification key <c>mod</c>.</p>
<p>The function should return <c>{ok,Pid}</c> or
<c>{ok,Pid,State}</c> where <c>Pid</c> is the pid of the top
supervisor and <c>State</c> is any term. If omitted,
<c>State</c> defaults to <c>[]</c>. If later the application
is stopped, <c>State</c> is passed to
<c>Module:prep_stop/1</c>.</p>
</desc>
</func>
<func>
<name>Module:start_phase(Phase, StartType, PhaseArgs) -> ok | {error, Reason}</name>
<fsummary>Extended start of an application</fsummary>
<type>
<v>Phase = atom()</v>
<v>StartType = <seealso marker="#type-start_type">start_type()</seealso></v>
<v>PhaseArgs = term()</v>
<v>Pid = pid()</v>
<v>State = state()</v>
</type>
<desc>
<p>This function is used to start an application with included
applications, when there is a need for synchronization between
processes in the different applications during startup.</p>
<p>The start phases is defined by the application specification
key <c>start_phases == [{Phase,PhaseArgs}]</c>. For included
applications, the set of phases must be a subset of the set of
phases defined for the including application.</p>
<p>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.</p>
<p>See <c>Module:start/2</c> for a description of
<c>StartType</c>.</p>
</desc>
</func>
<func>
<name>Module:prep_stop(State) -> NewState</name>
<fsummary>Prepare an application for termination</fsummary>
<type>
<v>State = NewState = term()</v>
</type>
<desc>
<p>This function is called when an application is about to be
stopped, before shutting down the processes of
the application.</p>
<p><c>State</c> is the state returned from
<c>Module:start/2</c>, or <c>[]</c> if no state was returned.
<c>NewState</c> is any term and will be passed to
<c>Module:stop/1</c>.</p>
<p>The function is optional. If it is not defined, the processes
will be terminated and then <c>Module:stop(State)</c> is
called.</p>
</desc>
</func>
<func>
<name>Module:stop(State)</name>
<fsummary>Clean up after termination of an application</fsummary>
<type>
<v>State = term()</v>
</type>
<desc>
<p>This function is called whenever an application has stopped.
It is intended to be the opposite of <c>Module:start/2</c>
and should do any necessary cleaning up. The return value is
ignored.</p>
<p><c>State</c> is the return value of
<c>Module:prep_stop/1</c>, if such a function exists.
Otherwise <c>State</c> is taken from the return value of
<c>Module:start/2</c>.</p>
</desc>
</func>
<func>
<name>Module:config_change(Changed, New, Removed) -> ok</name>
<fsummary>Update the configuration parameters for an application.</fsummary>
<type>
<v>Changed = [{Par,Val}]</v>
<v>New = [{Par,Val}]</v>
<v>Removed = [Par]</v>
<v> Par = atom()</v>
<v> Val = term()</v>
</type>
<desc>
<p>This function is called by an application after a code
replacement, if there are any changes to the configuration
parameters.</p>
<p><c>Changed</c> is a list of parameter-value tuples with all
configuration parameters with changed values, <c>New</c> is
a list of parameter-value tuples with all configuration
parameters that have been added, and <c>Removed</c> is a list
of all parameters that have been removed.</p>
</desc>
</func>
</funcs>
<section>
<title>SEE ALSO</title>
<p><seealso marker="doc/design_principles:des_princ">OTP Design Principles</seealso>,
<seealso marker="kernel_app">kernel(6)</seealso>,
<seealso marker="app">app(4)</seealso></p>
</section>
</erlref>
|