shahryarjb
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
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
shahryarjb
Do you have any suggestion about Genserver?
benwilson512
Hey @shahryarjb I guess I’m a bit confused about your question.
When your GenServer is terminated, it’s going to die. I don’t understand what you mean by
re-initing its state in terminate, that state is about to be discarded when it dies.Depending on your restart strategy, the supervisor is going to restart the genserver and then
initsimply gets called again.shahryarjb
Hi Dear @benwilson512,
I am sorry because of confusing. Some Unknown errors maybe down or terminate my state, so if a problem happens I want to restart my state, not drop it completely, and bind it with my database data again. I am using
strategy: :one_for_one.I test a function like this:
I did not find a way to break a state, after it shows me a warning like this (I prefer to see an error instead of warning, but I could not force it to show an error instead of warning):
So after that you can’t access to the state because it is going to be deleted completely, but I have stored all the previous data on my PSQL, so I want to start the state again with the data I saved in my database before.
Unfortunately, with the
one_for_onestrategy it doesn’t restart the state and callinitagain, it is the problem I have, and I want it to try 5 times and after that I need it to make a timeout and try 5 times again, after that it skips and do not try if it is not succeed.Please if you require a more explanation let me know.
Thank you in advance.
benwilson512
Can you show your child spec you’re using for your genserver? The default value is
restart: :permanentwhich should mean that the genserver is automatically re-created.shahryarjb
Thank you
My Application
My Dynamic Supervisor
It should be noted, even the record of my registry is deleted after the warning
benwilson512
Can you show how you’re using the registry? Your children should be getting restarted, but they’ll have a different
pid. Are you using the registry to associate children with db ids or something?shahryarjb
Hi @benwilson512.
Yes, each of my records has a unique ID
After a problem happens I can’t search by ID, and I lose my Registry record (one of them which has a problem not all of them, just a record)
Search in Registry
Genserver function
Refs
– https://github.com/shahryarjb/mishka-cms/blob/master/apps/mishka_installer/lib/plugin_manager/state/plugin_state.ex
– https://github.com/shahryarjb/mishka-cms/blob/master/apps/mishka_installer/lib/plugin_manager/state/plugin_state_dynamic_supervisor.ex
shahryarjb
I added the below code in my Genserver, and now when I have a problem it calls the
initagain.I copy this code from this post:
https://levelup.gitconnected.com/genserver-dynamicsupervisor-and-registry-the-elixir-triad-to-manage-processes-a65d4c3351c1
Is it right? Or I am in the wrong way?