merongivian
Task.Supervisor stops after one child fails on restarts
I have a task supervisor configured through a phoenix application, it will restart a failing child twice if it fails:
lib/nanoindie/application.ex
# children configuration on start
children = [
supervisor(Nanoindie.Repo, []),
supervisor(NanoindieWeb.Endpoint, []),
supervisor(Task.Supervisor, [[name: Nanoindie.TaskSupervisor, max_restarts: 2]]) # task supervisor
]
I add the children dynamically through start_link, what i do here is retrieving some info from blogs via web crawling
lib/nanoindie/jobs/blogs.exs
Enum.map Repo.all(Blog), fn(blog) ->
Task.Supervisor.start_child(Nanoindie.TaskSupervisor, fn ->
# web crawling
end
end, restart: :transient)
end
When i execute this, and for some reason one of the child fails, it will restart it normally, but if all restarts of this child fail it won’t run the rest of the processes
if i use restart: :temporary instead of restart: :transient it works fine, all processes run normally (without restarts)
Is this the right behavior?, i was reading through the DynamicSupervisor’s code and it seems that it will stop the dynamic supervisor if one the children doesn’t have any successful retry, i might be wrong though, im still learning elixir and OTP ![]()
Most Liked
svilen
The solution for me was to put a Supervisor in front of each job; so we ended up with 1 Dynamic Supervisor, that would start Supervisors with restart: temporary, and let the Supervisor manage the GenServer process with restart: :transient. A failing job would exit, bring down its Supervisor, which will be just killed by the Dynamic Supervisor above, leaving all other running jobs and their supervisors intact.
mbuhot
I recently saw Task.Supervisor suggested as a simple alternative to a sidekiq style job queue in mistakes-rails-developers-make-in-phoenix-pt-1-background-jobs
Given the way a single failing job will take out all other in-progress jobs after several failures, I’m wondering if this is actually an anti-pattern?
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









