Let's say I have GenServer instance:
defmodule MyModule do
use GenServer
def init(_) do
{:ok, %{}}
end
#...
end
I want MyModule be supervised, but when it crashes, do something before it restarts with state before crash:
defmodule MyModule do
use GenServer
def init(_) do
{:ok, %{}}
end
def init(:restart, previous_state) do
some_func(previous_state)
{:ok, previous_state}
end
#...
end
But I'm not sure how to implement this
terminate/2callback and store the state somewhere else (Agent, Database, ETS) and load it ininit/1but I don't think you can make the supervisor pass the old state on restart automatically. - Dogbert