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

fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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, "eq...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement