I am a newbie of erlang
, So that i have a problem about gen_server
. I am still unclear how is code_change()
function work?
Can you explain for me?
Thanks and best regards.
It's quite easy really. When gen_server
is running it contains a state that is passed between calls (the last argument to handle_call
, handle_cast
and handle_info
). If this was a tuple with two elements {a, b}
and you do a hot code upgrade with release handler and say the new code expects tuple with three elements {a, b, c}
then you need to convert the state before the new code could use the old state.
You do that with code_change
. In the Release Handling Instructions you specify the update
instruction and then, when Release Handler upgrades the code on the live node, it calls the code_change
function to convert the state. While the state is being converted the processes is paused, then it resumes with the new state.
You probably don't need to worry about that if you are not using Release Handler to do release upgrades on live nodes. And even then it's used only if the state format has to be changed, which isn't that often.