yurko

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”?

Showing Posts 1 to 3

msambarino

msambarino

{:error, reason} is an unexpected return value for the init callback, the possible return values are listed in the GenServer docs:

Returning {:ok, state} will cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning {:ok, state, timeout} is similar to {:ok, state} except handle_info(:timeout, state) will be called after timeout milliseconds if no messages are received within the timeout.

Returning {:ok, state, :hibernate} is similar to {:ok, state} except the process is hibernated before entering the loop. See handle_call/3 for more information on hibernation.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop or calling terminate/2. If used when part of a supervision tree the parent supervisor will not fail to start nor immediately try to restart the GenServer. The remainder of the supervision tree will be (re)started and so the GenServer should not be required by other processes. It can be started later with Supervisor.restart_child/2 as the child specification is saved in the parent supervisor. The main use cases for this are:

The GenServer is disabled by configuration but might be enabled later.
An error occurred and it will be handled by a different mechanism than the Supervisor. Likely this approach involves calling Supervisor.restart_child/2 after a delay to attempt a restart.
Returning {:stop, reason} will cause start_link/3 to return {:error, reason} and the process to exit with reason reason without entering the loop or calling terminate/2.

I think you can achieve your desired result by returning {:stop, reason} from your init callback:

def init(list) do
  case Registry.register(MyCoolRegistry, list[:key], :ok) do
    {:ok, _} -> {:ok, list}
    {:error, reason} -> {:stop, reason}
  end
end
yurko

yurko OP

nice of you to post it instead of “RTFM”, I don’t know how I missed it :smiley: Thanks!

UPD in my case it would be returning :ignore

msambarino

msambarino

Happy to help! :smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews