JasterV

JasterV

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.

Showing Posts 1 to 2

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, [])
JasterV

JasterV OP

That solves it, thank you so much!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews