[erlang-questions] How to upgrade running applications repeatedly

Matthias Lang <>
Thu Jun 7 23:22:06 CEST 2007


Bo writes:
 > this is what happens:
 > 
 > 1) I start the app with test1:start().
 > 2) I compile it and send some message to it, everything works
 > 3) I repeat step 2 and the program fails

I tried your code with one trivial modification and it works as
expected. Here's the output:

  2> c(test1).
  {ok,test1}
  3> test1:start().
  true
  4> test ! hello.
  Server "0.0.11A" received Msg=hello
  hello
%% At this point, I edited the source code and changed the version id.
  5> c(test1).     
  {ok,test1}
  6> test ! hello.
  Server "0.0.11A" received Msg=hello
  hello
  7> test ! hello.
  Server "0.0.11B" received Msg=hello
  hello

This works exactly as I expected---note the new version running after
command #7--- and does not fail as you described above. What are you
doing differently?

The exact code I ran is appended below.

Matthias

----------------------------------------------------------------------
-module(test1).
-define(VERSION, "0.0.11B").
-compile(export_all).

stop() ->
    test ! quit.
version() ->
    test ! version.
start() ->
    start_server([]).
start(L) ->
    start_server(L).

start_server(L) ->
    process_flag(trap_exit, true),
    register(test, 
         spawn(fun() ->
            process_flag(trap_exit, true),
            Val= (catch ?MODULE:server_loop([L])),
            io:format("Server terminated with:~p~n",[Val])
           end)).

server_loop(L) ->
    receive
      version  ->
            io:format("Server Version: ~p~n",[?VERSION]),
        ?MODULE:server_loop(L);
      quit  ->
              io:format("Server shutting down=~p~n",[quit]),
              {ok, true}; 
      upgrade  ->
               io:format("Server upgrading ...~n"),
        unregister(test),
        ?MODULE:start(L), 
              {ok, true}; 
    Msg ->
            io:format("Server ~p received Msg=~p~n",[?VERSION, Msg]),
            ?MODULE:server_loop(L)
    end.



More information about the erlang-questions mailing list