JasterV

JasterV

Can't start a :gen_statem implementation under a registry

I have a :gen_statem implementation that I want to start via a registry like this:

defmodule Arca.PodImpl do
  @moduledoc false

  @behaviour :gen_statem

  def start_link(opts) do
    cell_ref = Keyword.get(opts, :cell_ref)

    case cell_ref do
      nil ->
        {:error, "option 'cell_ref' is required to start the Pod"}

      cell_ref ->
        :gen_statem.start_link(__MODULE__, opts, name: via_registry(cell_ref))
    end
  end
end

  defp via_registry(cell_ref) do
    {:via, Registry, {Arca.Registry, {__MODULE__, cell_ref}}}
  end

The first problem is that I can’t start this module under a supervision tree because :gen_statem doesn’t implement child_spec. So then I have to implement the child_spec myself:

  # This is not automatically implemented by the gen_statem behaviour
  def child_spec(opts) do
    %{
      id: __MODULE__,
      start: {__MODULE__, :start_link, [opts]}
    }
  end

So I end up with a module like this:

defmodule Arca.PodImpl do
  @moduledoc false

  def start_link(opts) do
    cell_ref = Keyword.get(opts, :cell_ref)

    case cell_ref do
      nil ->
        {:error, "option 'cell_ref' is required to start the Pod"}

      cell_ref ->
        :gen_statem.start_link(__MODULE__, opts, name: via_registry(cell_ref))
    end
  end

  # This is not automatically implemented by the gen_statem behaviour
  def child_spec(opts) do
    %{
      id: __MODULE__,
      start: {__MODULE__, :start_link, [opts]}
    }
  end

  @impl true
  def lock(ref), do: :gen_statem.cast(via_registry(ref), :lock)

  @impl true
  def unlock(ref), do: :gen_statem.cast(via_registry(ref), :unlock)

  @impl true
  def get_state(ref), do: :gen_statem.call(via_registry(ref), :get_state)

  defp via_registry(cell_ref) do
    {:via, Registry, {Arca.Registry, {__MODULE__, cell_ref}}}
  end
end

For context, this is how I define my application children:

  def children(_target, _env) do
    [
      {Registry, keys: :unique, name: Arca.Registry},
      {Arca.PodImpl, cell_ref: "A1", lights_range: 75..95},
      {Arca.PodImpl, cell_ref: "A2", lights_range: 61..73},
      {Arca.PodImpl, cell_ref: "A3", lights_range: 20..58}
    ]
  end

Now, the main problem is that the PodImpl process seems to not be registered on my registry.

So when I start my application, the PodImpl modules seem to be linked to the main supervisor (makes sense) but when I query the Registry there are no registered processes.

So when I try to call PodImpl.get_state("A1") it returns an error saying that there is no process with that name (the one constructed by the via_registry private function)

I hope someone can help me understand what is going on.

Marked As Solved

jswanner

jswanner

The docs for start_link/3 state:

Equivalent to start_link/4 except that the gen_statem process is not registered with any name service.

I’m pretty sure you want:

:gen_statem.start_link(via_registry(cell_ref), __MODULE__, opts, [])

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New

Other popular topics Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement