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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Appup Cookbook</TITLE>
<SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="12"><!-- Empty --></A>
<H2>12 Appup Cookbook</H2>
<P>This chapter contains examples of <CODE>.appup</CODE> files for
typical cases of upgrades/downgrades done in run-time.<A NAME="12.1"><!-- Empty --></A>
<H3>12.1 Changing a Functional Module</H3>
<P>When a change has been made to a functional module, for example
if a new function has been added or a bug has been corrected,
simple code replacement is sufficient.
<P>Example:
<PRE>
{"2",
[{"1", [{load_module, m}]}],
[{"1", [{load_module, m}]}]
}.
</PRE>
<A NAME="12.2"><!-- Empty --></A>
<H3>12.2 Changing a Residence Module</H3>
<P>In a system implemented according to the OTP Design Principles,
all processes, except system processes and special processes,
reside in one of the behaviours <CODE>supervisor</CODE>,
<CODE>gen_server</CODE>, <CODE>gen_fsm</CODE> or <CODE>gen_event</CODE>. These
belong to the STDLIB application and upgrading/downgrading
normally requires an emulator restart.
<P>OTP thus provides no support for changing residence modules
except in the case of <A HREF="#spec">special
processes</A>.<A NAME="12.3"><!-- Empty --></A>
<H3>12.3 Changing a Callback Module</H3>
<P>A callback module is a functional module, and for code
extensions simple code replacement is sufficient.
<P>Example: When adding a function to <CODE>ch3</CODE> as described in
the example in <A HREF="release_handling.html#appup">Release
Handling</A>, <CODE>ch_app.appup</CODE> looks as follows:
<PRE>
{"2",
[{"1", [{load_module, ch3}]}],
[{"1", [{load_module, ch3}]}]
}.
</PRE>
<P>OTP also supports changing the internal state of behaviour
processes, see <A HREF="#int_state">Changing Internal
State</A> below.<A NAME="int_state"><!-- Empty --></A><A NAME="12.4"><!-- Empty --></A>
<H3>12.4 Changing Internal State</H3>
<P>In this case, simple code replacement is not sufficient.
The process must explicitly transform its state using the callback
function <CODE>code_change</CODE> before switching to the new version
of the callback module. Thus synchronized code replacement is
used.
<P>Example: Consider the gen_server <CODE>ch3</CODE> from the chapter
about the <A HREF="gen_server.html#ex">gen_server
behaviour</A>. The internal state is a term <CODE>Chs</CODE>
representing the available channels. Assume we want add a counter
<CODE>N</CODE> which keeps track of the number of <CODE>alloc</CODE> requests
so far. This means we need to change the format to
<CODE>{Chs,N}</CODE>.
<P>The <CODE>.appup</CODE> file could look as follows:
<PRE>
{"2",
[{"1", [{update, ch3, {advanced, []}}]}],
[{"1", [{update, ch3, {advanced, []}}]}]
}.
</PRE>
<P>The third element of the <CODE>update</CODE> instruction is a tuple
<CODE>{advanced,Extra}</CODE> which says that the affected processes
should do a state transformation before loading the new version
of the module. This is done by the processes calling the callback
function <CODE>code_change</CODE> (see <CODE>gen_server(3)</CODE>). The term
<CODE>Extra</CODE>, in this case [], is passed as-is to the function:
<A NAME="code_change"><!-- Empty --></A>
<PRE>
-module(ch3).
...
-export([code_change/3]).
...
code_change({down, _Vsn}, {Chs, N}, _Extra) ->
{ok, Chs};
code_change(_Vsn, Chs, _Extra) ->
{ok, {Chs, 0}}.
</PRE>
<P>The first argument is <CODE>{down,Vsn}</CODE> in case of a downgrade,
or <CODE>Vsn</CODE> in case of an upgrade. The term <CODE>Vsn</CODE> is
fetched from the 'original' version of the module, i.e.
the version we are upgrading from, or downgrading to.
<P>The version is defined by the module attribute <CODE>vsn</CODE>, if
any. There is no such attribute in <CODE>ch3</CODE>, so in this case
the version is the checksum (a huge integer) of the BEAM file, an
uninteresting value which is ignored.
<P>(The other callback functions of <CODE>ch3</CODE> need to be modified
as well and perhaps a new interface function added, this is not
shown here).<A NAME="12.5"><!-- Empty --></A>
<H3>12.5 Module Dependencies</H3>
<P>Assume we extend a module by adding a new interface function, as
in the example in <A HREF="release_handling.html#appup">Release
Handling</A>, where a function <CODE>available/0</CODE> is
added to <CODE>ch3</CODE>.
<P>If we also add a call to this function, say in the module
<CODE>m1</CODE>, a run-time error could occur during release upgrade if
the new version of <CODE>m1</CODE> is loaded first and calls
<CODE>ch3:available/0</CODE> before the new version of <CODE>ch3</CODE> is
loaded.
<P>Thus, <CODE>ch3</CODE> must be loaded before <CODE>m1</CODE> is, in
the upgrade case, and vice versa in the downgrade case. We say
that <CODE>m1</CODE> <STRONG>is dependent on</STRONG> <CODE>ch3</CODE>. In a release
handling instruction, this is expressed by the element
<CODE>DepMods</CODE>:
<PRE>
{load_module, Module, DepMods}
{update, Module, {advanced, Extra}, DepMods}
</PRE>
<P><CODE>DepMods</CODE> is a list of modules, on which <CODE>Module</CODE> is
dependent.
<P>Example: The module <CODE>m1</CODE> in the application <CODE>myapp</CODE> is
dependent on <CODE>ch3</CODE> when upgrading from "1" to "2", or
downgrading from "2" to "1":
<PRE>
myapp.appup:
{"2",
[{"1", [{load_module, m1, [ch3]}]}],
[{"1", [{load_module, m1, [ch3]}]}]
}.
ch_app.appup:
{"2",
[{"1", [{load_module, ch3}]}],
[{"1", [{load_module, ch3}]}]
}.
</PRE>
<P>If <CODE>m1</CODE> and <CODE>ch3</CODE> had belonged to the same application,
the <CODE>.appup</CODE> file could have looked like this:
<PRE>
{"2",
[{"1",
[{load_module, ch3},
{load_module, m1, [ch3]}]}],
[{"1",
[{load_module, ch3},
{load_module, m1, [ch3]}]}]
}.
</PRE>
<P>Note that it is <CODE>m1</CODE> that is dependent on <CODE>ch3</CODE> also
when downgrading. <CODE>systools</CODE> knows the difference between
up- and downgrading and will generate a correct <CODE>relup</CODE>,
where <CODE>ch3</CODE> is loaded before <CODE>m1</CODE> when upgrading but
<CODE>m1</CODE> is loaded before <CODE>ch3</CODE> when downgrading.<A NAME="spec"><!-- Empty --></A><A NAME="12.6"><!-- Empty --></A>
<H3>12.6 Changing Code For a Special Process</H3>
<P>In this case, simple code replacement is not sufficient.
When a new version of a residence module for a special process
is loaded, the process must make a fully qualified call to
its loop function to switch to the new code. Thus synchronized
code replacement must be used.
<P>
<TABLE CELLPADDING=4>
<TR>
<TD VALIGN=TOP><IMG ALT="Note!" SRC="note.gif"></TD>
<TD>
<P>The name(s) of the user-defined residence module(s) must be
listed in the <CODE>Modules</CODE> part of the child specification
for the special process, in order for the release handler to
find the process. </TD>
</TR>
</TABLE>
<P>Example. Consider the example <CODE>ch4</CODE> from the chapter about
<A HREF="spec_proc.html#ex">sys and proc_lib</A>.
When started by a supervisor, the child specification could look
like this:
<PRE>
{ch4, {ch4, start_link, []},
permanent, brutal_kill, worker, [ch4]}
</PRE>
<P>If <CODE>ch4</CODE> is part of the application <CODE>sp_app</CODE> and a new
version of the module should be loaded when upgrading from
version "1" to "2" of this application, <CODE>sp_app.appup</CODE> could
look like this:
<PRE>
{"2",
[{"1", [{update, ch4, {advanced, []}}]}],
[{"1", [{update, ch4, {advanced, []}}]}]
}.
</PRE>
<P>The <CODE>update</CODE> instruction must contain the tuple
<CODE>{advanced,Extra}</CODE>. The instruction will make the special
process call the callback function <CODE>system_code_change/4</CODE>, a
function the user must implement. The term <CODE>Extra</CODE>, in this
case [], is passed as-is to <CODE>system_code_change/4</CODE>:
<PRE>
-module(ch4).
...
-export([system_code_change/4]).
...
system_code_change(Chs, _Module, _OldVsn, _Extra) ->
{ok, Chs}.
</PRE>
<P>The first argument is the internal state <CODE>State</CODE> passed from
the function <CODE>sys:handle_system_msg(Request, From, Parent,
Module, Deb, State)</CODE>, called by the special process when
a system message is received. In <CODE>ch4</CODE>, the internal state is
the set of available channels <CODE>Chs</CODE>.
<P>The second argument is the name of the module (<CODE>ch4</CODE>).
<P>The third argument is <CODE>Vsn</CODE> or <CODE>{down,Vsn}</CODE> as
described for
<A HREF="#code_change">gen_server:code_change/3</A>.
<P>In this case, all arguments but the first are ignored and
the function simply returns the internal state again. This is
enough if the code only has been extended. If we had wanted to
change the internal state (similar to the example in
<A HREF="#int_state">Changing Internal State</A>),
it would have been done in this function and
<CODE>{ok,Chs2}</CODE> returned.<A NAME="sup"><!-- Empty --></A><A NAME="12.7"><!-- Empty --></A>
<H3>12.7 Changing a Supervisor</H3>
<P>The supervisor behaviour supports changing the internal state,
i.e. changing restart strategy and maximum restart frequency
properties, as well as changing existing child specifications.
<P>Adding and deleting child processes are also possible, but not
handled automatically. Instructions must be given by in
the <CODE>.appup</CODE> file.<A NAME="12.7.1"><!-- Empty --></A>
<H4>12.7.1 Changing Properties</H4>
<P>Since the supervisor should change its internal state,
synchronized code replacement is required. However,
a special <CODE>update</CODE> instruction must be used.
<P>The new version of the callback module must be loaded first
both in the case of upgrade and downgrade. Then the new return
value of <CODE>init/1</CODE> can be checked and the internal state be
changed accordingly.
<P>The following <CODE>upgrade</CODE> instruction is used for
supervisors:
<PRE>
{update, Module, supervisor}
</PRE>
<P>Example: Assume we want to change the restart strategy of
<CODE>ch_sup</CODE> from the <A HREF="sup_princ.html#ex">Supervisor
Behaviour</A> chapter from one_for_one to one_for_all.
We change the callback function <CODE>init/1</CODE> in
<CODE>ch_sup.erl</CODE>:
<PRE>
-module(ch_sup).
...
init(_Args) ->
{ok, {{one_for_all, 1, 60}, ...}}.
</PRE>
<P>The file <CODE>ch_app.appup</CODE>:
<PRE>
{"2",
[{"1", [{update, ch_sup, supervisor}]}],
[{"1", [{update, ch_sup, supervisor}]}]
}.
</PRE>
<A NAME="12.7.2"><!-- Empty --></A>
<H4>12.7.2 Changing Child Specifications</H4>
<P>The instruction, and thus the <CODE>.appup</CODE> file, when
changing an existing child specification, is the same as when
changing properties as described above:
<PRE>
{"2",
[{"1", [{update, ch_sup, supervisor}]}],
[{"1", [{update, ch_sup, supervisor}]}]
}.
</PRE>
<P>The changes do not affect existing child processes. For
example, changing the start function only specifies how
the child process should be restarted, if needed later on.
<P>Note that the id of the child specification cannot be changed.
<P>Note also that changing the <CODE>Modules</CODE> field of the child
specification may affect the release handling process itself,
as this field is used to identify which processes are affected
when doing a synchronized code replacement.<A NAME="sup_add"><!-- Empty --></A><A NAME="12.7.3"><!-- Empty --></A>
<H4>12.7.3 Adding And Deleting Child Processes</H4>
<P>As stated above, changing child specifications does not affect
existing child processes. New child specifications are
automatically added, but not deleted. Also, child processes are
not automatically started or terminated. Instead, this must be
done explicitly using <CODE>apply</CODE> instructions.
<P>Example: Assume we want to add a new child process <CODE>m1</CODE> to
<CODE>ch_sup</CODE> when upgrading <CODE>ch_app</CODE> from "1" to "2".
This means <CODE>m1</CODE> should be deleted when downgrading from
"2" to "1":
<PRE>
{"2",
[{"1",
[{update, ch_sup, supervisor},
{apply, {supervisor, restart_child, [ch_sup, m1]}}
]}],
[{"1",
[{apply, {supervisor, terminate_child, [ch_sup, m1]}},
{apply, {supervisor, delete_child, [ch_sup, m1]}},
{update, ch_sup, supervisor}
]}]
}.
</PRE>
<P>Note that the order of the instructions is important.
<P>Note also that the supervisor must be registered as
<CODE>ch_sup</CODE> for the script to work. If the supervisor is not
registered, it cannot be accessed directly from the script.
Instead a help function that finds the pid of the supervisor
and calls <CODE>supervisor:restart_child</CODE> etc. must be written,
and it is this function that should be called from the script
using the <CODE>apply</CODE> instruction.
<P>If the module <CODE>m1</CODE> is introduced in version "2" of
<CODE>ch_app</CODE>, it must also be loaded when upgrading and
deleted when downgrading:
<PRE>
{"2",
[{"1",
[{add_module, m1},
{update, ch_sup, supervisor},
{apply, {supervisor, restart_child, [ch_sup, m1]}}
]}],
[{"1",
[{apply, {supervisor, terminate_child, [ch_sup, m1]}},
{apply, {supervisor, delete_child, [ch_sup, m1]}},
{update, ch_sup, supervisor},
{delete_module, m1}
]}]
}.
</PRE>
<P>Note again that the order of the instructions is important.
When upgrading, <CODE>m1</CODE> must be loaded and the supervisor's
child specification changed, before the new child process can
be started. When downgrading, the child process must be
terminated before child specification is changed and the module
is deleted.<A NAME="12.8"><!-- Empty --></A>
<H3>12.8 Adding or Deleting a Module</H3>
<P>Example: A new functional module <CODE>m</CODE> is added to
<CODE>ch_app</CODE>:
<PRE>
{"2",
[{"1", [{add_module, m}]}],
[{"1", [{delete_module, m}]}]
</PRE>
<A NAME="12.9"><!-- Empty --></A>
<H3>12.9 Starting or Terminating a Process</H3>
<P>In a system structured according to the OTP design principles,
any process would be a child process belonging to a supervisor,
see <A HREF="#sup_add">Adding and Deleting Child
Processes</A> above.<A NAME="12.10"><!-- Empty --></A>
<H3>12.10 Adding or Removing an Application</H3>
<P>When adding or removing an application, no <CODE>.appup</CODE> file
is needed. When generating <CODE>relup</CODE>, the <CODE>.rel</CODE> files
are compared and <CODE>add_application</CODE> and
<CODE>remove_application</CODE> instructions are added automatically.
<A NAME="12.11"><!-- Empty --></A>
<H3>12.11 Restarting an Application</H3>
<P>Restarting an application is useful when a change is too
complicated to be made without restarting the processes, for
example if the supervisor hierarchy has been restructured.
<P>Example: When adding a new child <CODE>m1</CODE> to <CODE>ch_sup</CODE>, as
in the <A HREF="#sup_add">example above</A>, an
alternative to updating the supervisor is to restart the entire
application:
<PRE>
{"2",
[{"1", [{restart_application, ch_app}]}],
[{"1", [{restart_application, ch_app}]}]
}.
</PRE>
<A NAME="app_spec"><!-- Empty --></A><A NAME="12.12"><!-- Empty --></A>
<H3>12.12 Changing an Application Specification</H3>
<P>When installing a release, the application specifications are
automatically updated before evaluating the <CODE>relup</CODE> script.
Hence, no instructions are needed in the <CODE>.appup</CODE> file:
<PRE>
{"2",
[{"1", []}],
[{"1", []}]
}.
</PRE>
<A NAME="12.13"><!-- Empty --></A>
<H3>12.13 Changing Application Configuration</H3>
<P>Changing an application configuration by updating the <CODE>env</CODE>
key in the <CODE>.app</CODE> file is an instance of changing an
application specification, <A HREF="#app_spec">see
above</A>.
<P>Alternatively, application configuration parameters can be
added or updated in <CODE>sys.config</CODE>.<A NAME="12.14"><!-- Empty --></A>
<H3>12.14 Changing Included Applications</H3>
<P>The release handling instructions for adding, removing and
restarting applications apply to primary applications only.
There are no corresponding instructions for included
applications. However, since an included application is really a
supervision tree with a topmost supervisor, started as a child
process to a supervisor in the including application, a
<CODE>relup</CODE> file can be manually created.
<P>Example: Assume we have a release containing an application
<CODE>prim_app</CODE> which have a supervisor <CODE>prim_sup</CODE> in its
supervision tree.
<P>In a new version of the release, our example application
<CODE>ch_app</CODE> should be included in <CODE>prim_app</CODE>. That is,
its topmost supervisor <CODE>ch_sup</CODE> should be started as a child
process to <CODE>prim_sup</CODE>.
<P>1) Edit the code for <CODE>prim_sup</CODE>:
<PRE>
init(...) ->
{ok, {...supervisor flags...,
[...,
{ch_sup, {ch_sup,start_link,[]},
permanent,infinity,supervisor,[ch_sup]},
...]}}.
</PRE>
<P>2) Edit the <CODE>.app</CODE> file for <CODE>prim_app</CODE>:
<PRE>
{application, prim_app,
[...,
{vsn, "2"},
...,
{included_applications, [ch_app]},
...
]}.
</PRE>
<P>3) Create a new <CODE>.rel</CODE> file, including <CODE>ch_app</CODE>:
<PRE>
{release,
...,
[...,
{prim_app, "2"},
{ch_app, "1"}]}.
</PRE>
<A NAME="12.14.1"><!-- Empty --></A>
<H4>12.14.1 Application Restart</H4>
<P>4a) One way to start the included application is to restart
the entire <CODE>prim_app</CODE> application. Normally, we would then
use the <CODE>restart_application</CODE> instruction in
the <CODE>.appup</CODE> file for <CODE>prim_app</CODE>.
<P>However, if we did this and then generated a <CODE>relup</CODE> file,
not only would it contain instructions for restarting (i.e.
removing and adding) <CODE>prim_app</CODE>, it would also contain
instructions for starting <CODE>ch_app</CODE> (and stopping it, in
the case of downgrade). This is due to the fact that
<CODE>ch_app</CODE> is included in the new <CODE>.rel</CODE> file, but not
in the old one.
<P>Instead, a correct <CODE>relup</CODE> file can be created manually,
either from scratch or by editing the generated version.
The instructions for starting/stopping <CODE>ch_app</CODE> are
replaced by instructions for loading/unloading the application:
<PRE>
{"B",
[{"A",
[],
[{load_object_code,{ch_app,"1",[ch_sup,ch3]}},
{load_object_code,{prim_app,"2",[prim_app,prim_sup]}},
point_of_no_return,
{apply,{application,stop,[prim_app]}},
{remove,{prim_app,brutal_purge,brutal_purge}},
{remove,{prim_sup,brutal_purge,brutal_purge}},
{purge,[prim_app,prim_sup]},
{load,{prim_app,brutal_purge,brutal_purge}},
{load,{prim_sup,brutal_purge,brutal_purge}},
{load,{ch_sup,brutal_purge,brutal_purge}},
{load,{ch3,brutal_purge,brutal_purge}},
{apply,{application,load,[ch_app]}},
{apply,{application,start,[prim_app,permanent]}}]}],
[{"A",
[],
[{load_object_code,{prim_app,"1",[prim_app,prim_sup]}},
point_of_no_return,
{apply,{application,stop,[prim_app]}},
{apply,{application,unload,[ch_app]}},
{remove,{ch_sup,brutal_purge,brutal_purge}},
{remove,{ch3,brutal_purge,brutal_purge}},
{purge,[ch_sup,ch3]},
{remove,{prim_app,brutal_purge,brutal_purge}},
{remove,{prim_sup,brutal_purge,brutal_purge}},
{purge,[prim_app,prim_sup]},
{load,{prim_app,brutal_purge,brutal_purge}},
{load,{prim_sup,brutal_purge,brutal_purge}},
{apply,{application,start,[prim_app,permanent]}}]}]
}.
</PRE>
<A NAME="12.14.2"><!-- Empty --></A>
<H4>12.14.2 Supervisor Change</H4>
<P>4b) Another way to start the included application (or stop it
in the case of downgrade) is by combining instructions for
adding and removing child processes to/from <CODE>prim_sup</CODE> with
instructions for loading/unloading all <CODE>ch_app</CODE> code and
its application specification.
<P>Again, the <CODE>relup</CODE> file is created manually. Either from
scratch or by editing a generated version. Load all code for
<CODE>ch_app</CODE> first, and also load the application
specification, before <CODE>prim_sup</CODE> is updated. When
downgrading, <CODE>prim_sup</CODE> should be updated first, before
the code for <CODE>ch_app</CODE> and its application specification
are unloaded.
<PRE>
{"B",
[{"A",
[],
[{load_object_code,{ch_app,"1",[ch_sup,ch3]}},
{load_object_code,{prim_app,"2",[prim_sup]}},
point_of_no_return,
{load,{ch_sup,brutal_purge,brutal_purge}},
{load,{ch3,brutal_purge,brutal_purge}},
{apply,{application,load,[ch_app]}},
{suspend,[prim_sup]},
{load,{prim_sup,brutal_purge,brutal_purge}},
{code_change,up,[{prim_sup,[]}]},
{resume,[prim_sup]},
{apply,{supervisor,restart_child,[prim_sup,ch_sup]}}]}],
[{"A",
[],
[{load_object_code,{prim_app,"1",[prim_sup]}},
point_of_no_return,
{apply,{supervisor,terminate_child,[prim_sup,ch_sup]}},
{apply,{supervisor,delete_child,[prim_sup,ch_sup]}},
{suspend,[prim_sup]},
{load,{prim_sup,brutal_purge,brutal_purge}},
{code_change,down,[{prim_sup,[]}]},
{resume,[prim_sup]},
{remove,{ch_sup,brutal_purge,brutal_purge}},
{remove,{ch3,brutal_purge,brutal_purge}},
{purge,[ch_sup,ch3]},
{apply,{application,unload,[ch_app]}}]}]
}.
</PRE>
<A NAME="12.15"><!-- Empty --></A>
<H3>12.15 Changing Non-Erlang Code</H3>
<P>Changing code for a program written in another programming
language than Erlang, for example a port program, is very
application dependent and OTP provides no special support for it.
<P>Example, changing code for a port program: Assume that
the Erlang process controlling the port is a gen_server
<CODE>portc</CODE> and that the port is opened in the callback function
<CODE>init/1</CODE>:
<PRE>
init(...) ->
...,
PortPrg = filename:join(code:priv_dir(App), "portc"),
Port = open_port({spawn,PortPrg}, [...]),
...,
{ok, #state{port=Port, ...}}.
</PRE>
<P>If the port program should be updated, we can extend the code for
the gen_server with a <CODE>code_change</CODE> function which closes
the old port and opens a new port. (If necessary, the gen_server
may first request data that needs to be saved from the port
program and pass this data to the new port):
<PRE>
code_change(_OldVsn, State, port) ->
State#state.port ! close,
receive
{Port,close} ->
true
end,
PortPrg = filename:join(code:priv_dir(App), "portc"),
Port = open_port({spawn,PortPrg}, [...]),
{ok, #state{port=Port, ...}}.
</PRE>
<P>Update the application version number in the <CODE>.app</CODE> file
and write an <CODE>.appup</CODE> file:
<PRE>
["2",
[{"1", [{update, portc, {advanced,port}}]}],
[{"1", [{update, portc, {advanced,port}}]}]
].
</PRE>
<P>Make sure the <CODE>priv</CODE> directory where the C program is
located is included in the new release package:
<PRE>
1> <STRONG>systools:make_tar("my_release", [{dirs,[priv]}]).</STRONG>
...
</PRE>
<A NAME="12.16"><!-- Empty --></A>
<H3>12.16 Emulator Restart</H3>
<P>If the emulator can or should be restarted, the very simple
<CODE>.relup</CODE> file can be created manually:
<PRE>
{"B",
[{"A",
[],
[restart_new_emulator]}],
[{"A",
[],
[restart_new_emulator]}]
}.
</PRE>
<P>This way, the release handler framework with automatic packing
and unpacking of release packages, automatic path updates etc. can
be used without having to specify <CODE>.appup</CODE> files.
<P>If some transformation of persistent data, for example database
contents, needs to be done before installing the new release
version, instructions for this can be added to the <CODE>.relup</CODE>
file as well.<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|