yurko
I have a simple_one_for_one supervisor that spawns GenServers and I need to make sure there are workers that try to do the same task (it is an expected error and not an exception). I use the new shiny Registry to accomplish it.
Here is the simplified relevant part of the GenServer child that is getting started:
def init(list) do
case Registry.register(MyCoolRegistry, list[:key], :ok) do
{:ok, _} -> {:ok, list}
{:error, reason} -> {:error, reason}
end
end
the idea is to hook into init callback and return an {:error, reason} if I don’t want this server to proceed.
As far as I can tell it also works as intended, the first time I start a process it works and any further attempts do not spawn processes. What bugs me is the return value that the Supervisor.start_child gives me if I try to start such a “duplicate worker”:
{:error, {:bad_return_value, {:error, {:already_registered, #PID<0.450.0>}}}}
why is it a bad_return_value? What should I return from init callback of a simple_one_for_one GenServer worker to communicate “I’ve checked and decided not to proceed”?
Trending in Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
msambarino
{:error, reason}is an unexpected return value for theinitcallback, the possible return values are listed in the GenServer docs:I think you can achieve your desired result by returning
{:stop, reason}from yourinitcallback:yurko
nice of you to post it instead of “RTFM”, I don’t know how I missed it
Thanks!
UPD in my case it would be returning
:ignoremsambarino
Happy to help!