0

I am trying the hot code feature of erlang following the guide from LYAE but i do not understand how to make the update message to get triggered.

I have a module which runs a method that is upgradeable:

Module

-module(upgrade).
-export([main/1,upgrade/1,init/1,init_link/1]).
-record(state,{ version=0,comments=""}).


init(State)->
    spawn(?MODULE,main,[State]).
main(State)->
    receive 
        update->
            NewState=?MODULE:upgrade(State),
            if NewState#state.version>3 -> exit("Max Version Reached") end,

            ?MODULE:main(NewState);
        SomeMessage->
            main(State)
    end.



upgrade(State=#state{version=Version,comments=Comments})->
    Comm=case Version rem 2 of
            0 -> "Even version";
            _ -> "Uneven version"
         end,
    #state{version=Version+1,comments=Comm}.

Shell

>c(upgrade).
>rr(upgrade,state).
>U=upgrade:init(#state{version=0,comments="initial"}).
>Monitor=monitor(process,U).
> ......to something to trigger the update message
> flush().  % see the exit message reason

I do not understand how can i perform a hot code reload in order to trigger the update message. I want when i use flush to get the exit reason from my main method.

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 1
    You are going to have to show the `......to something to trigger the update message` part of your code. But based on how you have things setup, it looks like if you send that process an `update` message, it will update itself. So in your specific example, if you `U ! update`, it will call the upgrade function. – Justin Wood Oct 10 '19 at 22:17

1 Answers1

3

The process expects to get the atom update as a message. Since you have the pid of the process in the variable U, you can send the message like this:

U ! update.

Note that the strings Even version and Uneven version are only kept in the state, never printed, so you won't see those. The only thing you'll see is the exit message, after sending update four times and calling flush().

legoscia
  • 39,593
  • 22
  • 116
  • 167