Reputation: 940
I'm learning Erlang and I've learned about hot code loading, but I don't know how the gen_fst behavior's code_change function works. I also can't find any example of it.
Should I create an action like so:
upgrade() ->
gen_fsm:send_event(machine_name, upgrade).
And have a handler in the states like so:
some_state(upgrade, State) ->
code:purge(?MODULE),
compile:file(?MODULE),
code:load_file(?MODULE),
{next_state, some_state, State, 1000}.
I've tried this, but the code_change/4
function doesn't execute. How should I correctly implement hot code loading in my FSM?
Upvotes: 0
Views: 61
Reputation: 8372
The code_change function calls are orchestrated by the supervisors when doing a relup. The normal hot code loading is the lowlevel functionality needed for this.
If you only want to replace one module and don't need to upgrade your state you can often just load the new module from the shell (no need for the purge, it can actually be harmful).
There are up to two versions of each module running. When doing local calls (without a module name in the call) you'll stay in the old version. However when you do a qualified call (with module name) you'll always jump to the new code.
loop(S) ->
do_processing(S), % stays in the same version
?MODULE:loop(S). % always jumps to the newest version
For more complicated hot upgrades you need appups and relups which are a advanced topic, best intro is in LYSE
Upvotes: 2