Permalink
Browse files

add simple hot swap

  • Loading branch information...
1 parent a61b90d commit 774747f830ed23ddb4c20646c1d5753d13876177 @hoterran committed Aug 8, 2012
Showing with 86 additions and 3 deletions.
  1. +61 −3 README.md
  2. +25 −0 e3.erl
View
@@ -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").
@@ -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
+
View
25 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.