[erlang-questions] Can OTP worker resume from same state after being restarted by supervisor?

Ulf Wiger <>
Sun Sep 19 14:28:00 CEST 2010


On 09/19/2010 02:21 AM, Jachym Holecek wrote:
> 
> Yes. If you want to store state persistently, you'd probably go with
> Mnesia table, for transient storage (not preserved across application
> restarts) you could use a public ETS table owned by a do-nothing server
> process living alongside you child-supervisor.

An old trick is to create the ets table in the start function,
before starting the child process. This way, the supervisor becomes
the owner of the table, and it will survive child restarts.

The drawback is that you are never allowed to execute code in
the supervisor again (except at the next child restart), which
makes it a bit awkward to perform certain upgrade functions,
e.g. deleting the table (you can rename it, though).

A newer trick is to specify an heir of the table (see the docs
for ets:new/2). It would be possible to make the supervisor the
heir of your table, in which case the supervisor will receive
an unexpected message occasionally and complain to the error_logger.
So the old trick could be enhanced by creating the ets table in
the supervisor, then giving it to the newly started child, having
set the heir to itself (as described in the docs for ets:give_away/2).

These are tricks for convenience - not necessarily better than
creating a table owner process.

BR,
Ulf W


More information about the erlang-questions mailing list