[erlang-questions] How to upgrade running applications repeatedly

Bo <>
Thu Jun 7 22:06:59 CEST 2007


Hello All.
I have tried without success to be able to update my running applications.   The code below is a very simple example in which there is a loop, that will always call itself with ?MODULE:loop(L) which according to the documentation should always call the latest version of itself, but 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

Erlang seems to have upgraded one version and retains still one old version in step 2), but in step 3) it kicks out the old code and terminates my program.   What I do not understand is why does the running application NOT upgrade itself, as far as I get it, the ?MODULE:loop(L) should cause the program to load its latest version, but it does not, the running application stays in the old version and given that Erlang retains 2 versions in memory whenever a 3rd pops up, it terminates the first version.   The only way around it seems to be that when you REALLY want to upgrade your code, you are required to spawn a new version and pass the current parameters to that loop.   

Question:  Is there a better way to do it ??   Why does ?MODULE:loop(L) not update the running loop, if it doesn't, than this functionality is useless or isn't it (or maybe I miss its working entirely) ??  The only other way seems to be doing it OTP (which at this I would like to avoid) and write all data, that the loop would hold  (this code has been removed from example) to mnesia, but this scheme would complicate everything quite a bit, when I require to process data to/from client sockets.

I want to be able to continously update my application as my development advances on multiple nodes without ever stopping it or disconnecting neither services, processes or clients.

Best regards,
Bo
-------------------------------------------------------------------------------
-module(test1).
-define(VERSION, "0.0.11A").
-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 received Msg=~p~n",[Msg]),
            ?MODULE:server_loop(L)
    end.


       
---------------------------------
Be a better Heartthrob. Get better relationship answers from someone who knows.
Yahoo! Answers - Check it out. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070607/c82421a9/attachment.html>


More information about the erlang-questions mailing list