hello i am implementing a queue, to store a list of agents/cities so i can push/pop after i send a message to them
ive tried to create a new genserver per city based on a table of cities but cannot get it to function properly
basically i want to send a msg to the unique city queue which has agents stored in its state
and pass the message through ti the agent on top of the state, and requeue the agent at the bottom
the problem im having is how to start seperate genservers per city and then route the message correctly
the msg looks like % {city:xxx, data: xxx }
defmodule Agent.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, :ok, name: :agent_supervisor)
end
def init(:ok) do
children = [
worker(Agent.Server, [])
]
supervise(children, strategy: :simple_one_for_one)
end
def start_city(name) do
# And we use `start_child/2` to start a new Chat.Server process
Supervisor.start_child(:agent_supervisor, [name])
end
end
defmodule Agent.Server do
use GenServer
# API
def start_link(name) do
GenServer.start_link(__MODULE__, [], name: via_tuple(name))
end
defp via_tuple(city_name) do
{:via, :gproc, {:n, :l, {:city, city_name}}}
end
def add_agent(city_name, agent) do
GenServer.cast(via_tuple(city_name), {:add_agent, agent})
end
def get_agents(city_name) do
GenServer.call(via_tuple(city_name), :get_agents)
end
# SERVER
def init(agents) do
{:ok, agents}
end
def handle_cast({:add_agent, new_agent}, agents) do
{:noreply, [new_agent | agents]}
end
def handle_call(:get_agents, _from, agents) do
{:reply, agents, agents}
end
end