owaisqayum
I have defined a module GS where genserver will be used and I need to create tables. I have the whole logic of the program In another module named base. How do I inherit handle_call in my genserver GS module ?
GS MODULE:
defmodule GS do
use GenServer
@name gen
# client
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, [], opts ++ [name: gen])
end
def creating_tables(tables_list) do
GenServer.call(@name, {:creating_tables, tables_list})
end
# server
def init(initial_state) do
{:ok, initial_state}
end
end
BASE MODULE
defmodule Base do
def handle_call({:creating_tables}, _from, state_tables_list) do
tables = Enum.map(state_tables_list, fn table -> create(table) end)
{:reply, tables, state_tables_list}
end
defp create(table) do
try do
:ets.new(table, [:named_table, :bag])
rescue
ArgumentError -> table
end
end
end
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hi, I’m evaluating Ash as a foundation for our future business applications.
So far I love it, and there’s just one usecase I haven’t be...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Laetitia
Why do you want to extract this function from the GS module? Do you want to create an extended genserver?
I think handle_call has to be in the genserver module as it is a callback implementation.
If you want, you can extract the logic of the table creation in another module and use it in your genserver.
owaisqayum
Thanks for such a prompt response. Actually, I will have other features in my
Basemodule which i want to affiliate with other genserver. Keeping them under ‘GS’ will make it crowded.Laetitia
I will be happy to help. I need more infos.
Why it is a Base? What is the role of this module?
owaisqayum
So
Baseis like a Base table in database where tables can be created or deleted and also can add or delete data from it.GSwill be a genserver module where i can create multiple tables concurrently and later in my program add data to tables concurrently.Laetitia
In this case, keep the handle_call function in GS. The Base module is a TableCreator (surely, you can find a better name)
and in the genserver module the function handle_call would be
i hope it helps
owaisqayum
So, Basically i must have to use handle call inside my GenServer.
gregvaughn
I agree with @Laetitia and will add that
handle_calland the tuple it returns is your contract with GenServer. That logic belongs in the GenServer callback module. But you can certainly call out to shared logic in another module in the body of the function.I’d also caution you about bottlenecking your system. You seem to want to make it easy to create multiple ets tables. Those are owned by a process for lifecycle management purposes and you’re not making them public (nor are you accepting a parameter to do so) so all access will have to go through a single GenServer, which only handles a single message at a time.
owaisqayum
Yes, I agree, and thanks a lot for such valuable feedback. Actually, I will make each
:etstable a separate table. So, all the entries to a single table are handled by a separate process. I was wrong initially when I stated that each would have a separate process. Thanks for correcting me.lud
You must define
handle_call/3in the module you give toGenServer.start_link/3.Your code:
Here you give
__MODULE__, so you giveGS.