Skip to content

Commit

Permalink
add simple hot swap
Browse files Browse the repository at this point in the history
  • Loading branch information
hoterran committed Aug 8, 2012
1 parent a61b90d commit 774747f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
64 changes: 61 additions & 3 deletions README.md
@@ -1,11 +1,12 @@
#summary
one example, show how to swap erlang code online include inner state record
#Example1

show how to swap erlang code online include inner state record

first, we have gen server swap_test
this is counter program, each call will show inner state counter, than incrment this counter.
now, we need change code, can set random step, but gen process cant stop, so need online swap code

#step
##step

>os:cmd("cp swap_test1.erl swap_test.erl").
Expand Down Expand Up @@ -37,3 +38,60 @@ now, we need change code, can set random step, but gen process cant stop, so nee
now we set step = 3


##code


you can see this blog for [detail](http://www.hoterran.info/erlang-otp-sys-sourcecode)


#Example2


how to kill old code process

1> c(e3).
{ok,e3}
2> e3:start().
true
3> whereis(e3).
<0.41.0>
4> c(e3).
{ok,e3}
5> whereis(e3).
<0.41.0>
6> c(e3).
{ok,e3}
7> whereis(e3).
undefined

the second c(e3) kill old code process

#Example3

##upgrade

>> c(e3).
{ok,e3}
>> e3:start().
true
>> e3:msg().
"aa"
hello

modify code ``aa`` to ``bb``

>> e3:compile().
{module,e3}
>> e3:msg().
hello
"aa"

yes cant change, still old code, now switch

>> e3:switch().
code_switch
8> e3:msg().
"bb"
hello

25 changes: 25 additions & 0 deletions e3.erl
@@ -0,0 +1,25 @@
-module(e3).
-compile(export_all).

start() ->
register(?MODULE, spawn(?MODULE, loop, [])).

switch() ->
?MODULE ! code_switch.

compile() ->
compile:file(?MODULE),
code:purge(?MODULE),
code:load_file(?MODULE).

msg() ->
?MODULE ! hello.

loop() ->
receive
code_switch ->
?MODULE:loop();
_ ->
io:format("~p~n", ["bb"]),
loop()
end.

0 comments on commit 774747f

Please sign in to comment.