dalhorinek
Stopping child (GenServer) in DynamicSupervisor gracefully without restarting it
Hello.
I have a GenServer running as a child under DynamicSupervisor. The GenServer does something and using info status message I check it’s state and when the GenServer finished it’s job, I return
{:stop, :normal, state}
from it. And I want to stop the process, but the DynamicSupervisor restarts it again. I tried to send a message before returning the {:stop… } and in the handler of the message call terminate_child, but the supervisor seems to be faster restarting the child before terminating it.
I want the process to be supervised for error reasons, so when there is a problem, it restarts it, but I want to end gracefully when finished without restarting it.
What can I do or what’s best way how to handle such scenario?
Thanks in advance.
Marked As Solved
kokolegorille
What child spec do You pass to dynamic supervisor to start your worker?
Here is one I use that does not imply worker restart…
spec = %{
id: name,
start: {Worker, :start_link, [args]},
restart: :temporary,
type: :worker
}
Also Liked
dalhorinek
Hey, I’m using the Module child spec.
DynamicSupervisor.start_child(
Service.ServiceSupervisor,
{Service.Manager, [id: id, opts: opts]}
)
I think it’s the restart: option. now I see in the docs
:transient- the child process is restarted only if it terminates abnormally, i.e., with an exit reason other than:normal,:shutdown, or{:shutdown, term}.
This seems to be what I was looking for, thanks for the hint with restart: option!
kokolegorille
I am using restart: :temporary because I delegate the monitoring task to another process… making my dynamic supervisor superdumb.
But I would use :transient as well in your case ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









