aligredo

aligredo

Help with GenServers and Supervisors

I’ve implemented my first GenServer and Supervisor, here is the code

defmodule Spare.DataCenter do

  use GenServer

  #Client
  def start_link(_opts \\ []) do
    GenServer.start_link(__MODULE__, [
      {:ets_table_name, :link_cache_table},
      {:log_limit, 1_000_000}
    ], name: :data_center)
  end

  def add_question(question) do
    GenServer.cast(:data_center, question)
  end

  def view_questions() do
    GenServer.call(:data_center, :view_questions)
  end

  def get_question(question) do
    GenServer.call(:data_center, {:view_questions, question})
  end


  def remove_question(question) do
    GenServer.cast(:data_center, {:remove_question, question})
  end


  #Server
  def init(args) do
    [{:ets_table_name, ets_table_name}, {:log_limit, log_limit}] = args
    :ets.new(ets_table_name, [:named_table, :set, :protected])
    {:ok, %{log_limit: log_limit, ets_table_name: ets_table_name}}
  end

  def handle_cast({:remove_question, question}, state) do
    %{ets_table_name: ets_table_name} = state
    :ets.delete(ets_table_name, question)
    {:noreply, state}
  end

  def handle_cast(question, state) do
    %{ets_table_name: ets_table_name} = state
    :ets.insert_new(ets_table_name, question)
    {:noreply, state}
  end


  def handle_call(:view_questions, _from, state) do
    %{ets_table_name: ets_table_name} = state
    questions_list = :ets.tab2list(ets_table_name)
    {:reply, questions_list, state}
  end

  def handle_call({:get_question, question}, _from, state) do
    %{ets_table_name: ets_table_name} = state
    questions = :ets.lookup(ets_table_name, question)
    {:reply, questions, state}
  end


end

defmodule Spare.DataCenter.Supervisor do

  use Supervisor

  def start_link(_opts \\ [], _any \\ []) do
    Supervisor.start_link(__MODULE__, :ok, name: :data_center)
  end

  def init(:ok) do
    children = [
      worker(Spare.DataCenter, [[name: :data_center]])
    ]

    supervise(children, strategy: :one_for_one)
  end

end

and I added my Supervisor to the Application children like this

def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Start the Ecto repository
      Spare.Repo,
      # Start the endpoint when the application starts
      SpareWeb.Endpoint,
      # Starts a worker by calling: Spare.Worker.start_link(arg)
      # {Spare.Worker, arg},
      Spare.DataCenter.Supervisor,
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Spare.Supervisor]
    Supervisor.start_link(children, opts)
  end

However everytime I start my app with iex -S mix I get the following error;

Generated spare app
** (Mix) Could not start application spare: Spare.Application.start(:normal, []) returned an error: shutdown: failed to start child: Spare.Supervisor
    ** (EXIT) shutdown: failed to start child: Spare.DataCenter
        ** (EXIT) already started: #PID<0.352.0>

any help with that?
Thanks in advance.

Marked As Solved

kokolegorille

kokolegorille

Giving the same name twice for multiple workers/supervisor is not going to work.

Processes should have a unique name :slight_smile:

When there is a unique GenServer, I often use name: __ MODULE __ (no space… just here to avoid Markdown format)

But if You need to spawn the same GenServer multiple times, then You need a registry.

Last Post!

aligredo

aligredo

Ohhkaaaay, I thought I should write the name of the Genserver in the supervisor start_link, what a noob?! :smile:
Thank you so much!

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
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
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement