Reputation: 9360
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.
Upvotes: 0
Views: 107
Reputation: 41568
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()
.
Upvotes: 3