Hi folks,
I have a DynamicSupervisor
and under it, I have a Genserver
. Each state, when wants to be started, checks Postgres if there is a record which same as the ID, it loads it to init
so if there is not, it creates a simple map.
Imagine something happens and the Genserver is terminated, like pushing to state or something else.
def terminate(reason, %PluginState{} = state) do
MishkaInstaller.plugin_activity("read", state, "high", "throw")
# TODO: Introduce a strategy for preparing again ( load from database, disk ?)
Logger.warn(
"#{Map.get(state, :name)} from #{Map.get(state, :event)} event of Plugins manager was Terminated,
Reason of Terminate #{inspect(reason)}"
)
end
First option
Hence, I need to re-init
or re-bind
this state again, so I get the ID of record from the state is passed in the terminate
function and call the DynamicSupervisor.start_child
again to create a new state which loads from database.
Second option
If terminate is not :normal
I call a function inside another Genserver which loads my DynamicSupervisor
registry
and check which key in my database does not exist in registry so if there is a record, so it creates a new state again.
Third option
I create a new normal Genserver as Cache
which keeps all the {pid, id}
of the DynamicSupervisor
and if my terminate function is run, so it sends a request to this Cache Genserver
and it checks which key or PID is missing in my registry, so after finding it, it creates a new state again.
Which strategy do you suggest me? If your suggestion is not there, please let me know what you prefer to do in this situation.
Thank you