How do I have a GenServer process available to my entire app?

983 views Asked by At

I'm using GenServer for a queueing system. How can I have the same process available for the entire app to access?

I thought putting it in application.ex would do it, like so:

children = [
  supervisor(Prefect, [], name: PrefectQueue)
]

My Prefect module is the GenServer:

defmodule Prefect do
  use GenServer
  alias Prefect.Document

  # client
  def start_link() do
    GenServer.start_link(__MODULE__, [])
  end

  def add(pid, item) do
    GenServer.cast(pid, item)
  end

  # server
  def handle_cast(item, list) do
    updated_list = [item | list]
    {:noreply, updated_list}
  end
end

I can't seem to access it in my controller, though:

defmodule PrefectWeb.DocumentController do
  use PrefectWeb, :controller

  def create(conn, params) do
    Prefect.add(PrefectQueue, params["id"])
    conn
    |> send_resp(200, "Queued #{Prefect.view(PrefectQueue)}")
  end
end

Posting to that create function gives this error:

[info] POST /api/documents
[debug] Processing with PrefectWeb.DocumentController.create/2
  Parameters: %{"id" => "1"}
  Pipelines: [:api]
[error] #PID<0.349.0> running PrefectWeb.Endpoint terminated
Server: 0.0.0.0:4000 (http)
Request: POST /api/documents
** (exit) exited in: GenServer.call(Prefect.Queue, :view, 5000)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
1

There are 1 answers

0
Phillipp On BEST ANSWER

You have to name the process in the GenServer itself, not via the supervisor childs list. Try:

defmodule Prefect do
  use GenServer
  alias Prefect.Document

  # client
  def start_link() do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def add(pid, item) do
    GenServer.cast(pid, item)
  end

  # server
  def handle_cast(item, list) do
    updated_list = [item | list]
    {:noreply, updated_list}
  end
end

Your process should now be called like this Prefect.view(Prefect).