Hi, I have a simple agent called Queue and I need to start two queues from the supervisor. However, can’t figure out the syntax - either I’m running into “already started” or I can’t access any identifier…
This is the piece of supervisor (with various futile attempts)
children = [
Counter,
Supervisor.child_spec({Queue,initial_value: 0, name: :q1}, id: :q1),
Supervisor.child_spec({Queue,initial_value: 0, name: :q2}, id: :q2)
# Supervisor.child_spec({Queue, :start_link}, id: :q1),
# Supervisor.child_spec({Queue, :start_link}, id: :q2)
# Supervisor.child_spec(Queue, id: :queue2)
# {Queue, name: QueueOne},
# {Queue, name: QueueTwo}
# worker(Queue, [name: :q1]),
# worker(Queue, [name: :q2])
]
and this is the Queue agent for completeness. Not sure the start_link params are defined properly either:
defmodule Queue do
use Agent
def start(initial_value \\ []) do
Agent.start(fn -> initial_value end)
end
def start_link(initial_value \\ [], name \\ __MODULE__) do
IO.puts "Starting queue..."
Agent.start_link(fn -> initial_value end, name: name)
end
def all(process) do
Agent.get(process,& &1)
end
def push(process, value) do
Agent.update(process, &[value | &1])
end
def pop(process) do
Agent.get_and_update(process, &List.pop_at(&1, -1))
end
end
Ideally, I would have liked something like QueueOne and QueueTwo to be able to call QueueOne.all etc.
Thank you for your help