owaisqayum

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

Showing Posts 1 to 9

Laetitia

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

owaisqayum OP

Thanks for such a prompt response. Actually, I will have other features in my Base module which i want to affiliate with other genserver. Keeping them under ‘GS’ will make it crowded.

Laetitia

Laetitia

I will be happy to help. I need more infos.
Why it is a Base? What is the role of this module?

owaisqayum

owaisqayum OP

So Base is like a Base table in database where tables can be created or deleted and also can add or delete data from it. GS will be a genserver module where i can create multiple tables concurrently and later in my program add data to tables concurrently.

Laetitia

Laetitia

In this case, keep the handle_call function in GS. The Base module is a TableCreator (surely, you can find a better name)

defmodule TableCreator do
   def create_tables(tables) when is_list(tables) do
      tables |> Enum.map(fn table -> create(table) end)
   end

  defp create(table) do
    try do
      :ets.new(table, [:named_table, :bag])
    rescue
      ArgumentError -> table
    end
  end
end

and in the genserver module the function handle_call would be

def handle_call({:creating_tables}, _from, state_tables_list) do
    tables = TableCreator.create_tables(state_tables_list)
    {:reply, tables, state_tables_list}
  end

i hope it helps :wink:

owaisqayum

owaisqayum OP

So, Basically i must have to use handle call inside my GenServer.

gregvaughn

gregvaughn

I agree with @Laetitia and will add that handle_call and 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

owaisqayum OP

Yes, I agree, and thanks a lot for such valuable feedback. Actually, I will make each :ets table 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

lud

You must define handle_call/3 in the module you give to GenServer.start_link/3.

Your code:

 GenServer.start_link(__MODULE__, [], opts ++ [name: gen])

Here you give __MODULE__, so you give GS.

— 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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
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
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
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews