owaisqayum

owaisqayum

How do i call handle_call outside module?

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

Marked As Solved

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:

Also Liked

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.

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.

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
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

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement