[erlang-questions] preserving state through a gen_server restart
Dominic Williams
mail@REDACTED
Tue Oct 23 11:42:05 CEST 2012
Hi Ivan,
Le 19 oct. 2012 à 17:03, Ivan Uemlianin a écrit :
> Dear All
>
> I have an erlang/OTP application consisting mostly of gen_servers. One repeated pattern is having a gateway/aggregate gen_server process that provides access (and other management) to individual gen_server processes, e.g.:
>
> my_sup
> |
> my_lambs
> / \ \
> my_lamb my_lamb my_lamb ...
>
> my_lambs' state variable holds a dictionary {LambName: LambPid} and among other functions it allows me to call lambs by name instead of my Pid.
>
> If my_lambs terminates normally --- e.g., if the whole application is closing down --- its terminate/s function can call my:lamb:stop for each of the processes in its care.
>
> However, if my_lambs terminates abnormally, and is restarted by my_sup, I think I'd like to preserve its state variable --- at least the Pids of the my_lamb processes --- so that it can be passed on to the new version of my_lambs.
>
> I'd like to avoid losing all of the my_lamb processes, if the my_lambs process crashes.
>
> Can state be passed to the supervisor, to be used when it restarts its child?
>
> Could I park the state variable in some kind of holding process, so that the new my_lambs can retrieve it?
>
> Or is there another way of doing this?
>
> With thanks and best wishes
>
> Ivan
I usually achieve this by inverting the dependency: the N my_lamb workers come and register themselves with the my_lambs gateway (which they find by name). If you put this registration in a gen_server timeout, then they do it when they become idle, which is going to happen if the original my_lambs dies and stops passing stuff to them.
Best regards,
Dominic Williams
http://dominicwilliams.net
More information about the erlang-questions
mailing list