Question

I am trying to find out whether it is possible to start a gen_server with a given state.

I would like to be able to set up a monitor/supervisor that restarts the server with its last valid state when this server crashes.

Any suggestion on how to tackle this problem would be very Welcome.

So far my only idea is to have a special handle_call/3 that changes the server state to the desired state when called, but I would like to avoid modifying the server module and handle this purely from my monitor/supervisor process if possible.

Thank you for your time.

Was it helpful?

Solution

gen_server:init takes argument Args. You can pass whatever state you want and set it as the state of the server. You can pass Args to start_link and it will pass it to init for you.

http://www.erlang.org/doc/man/gen_server.html#Module:init-1

http://www.erlang.org/doc/man/gen_server.html#start_link-3

I think that in your case you might want to store the state in mnesia. That way you don't have to take care of passing last valid state to the gen_server. In case you don't want to start mnesia you can use ETS. Create public ETS in some process that won't die and use it from your gen_server (note that when server that created ets dies, the ets is destroyed)

http://www.erlang.org/doc/man/ets.html

http://www.erlang.org/doc/man/mnesia.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top