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
Giving the same name twice for multiple workers/supervisor is not going to work.
Processes should have a unique name ![]()
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.
3
Last Post!
aligredo
Ohhkaaaay, I thought I should write the name of the Genserver in the supervisor start_link, what a noob?! ![]()
Thank you so much!
0
Trending in Questions
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
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
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
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
Hi!
What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Other Trending Topics
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










