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

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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 I...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement