enqueue(:refresh_subscriptions, nil)
end)
GenServer.start_link(__MODULE__, %{
- in: {:sets.new(), [],
+ in: {:sets.new(), []},
out: {:sets.new(), []}
}, name: __MODULE__)
end
{:error, "Don't know what do do with this"}
end
- def enqueue(type, payload) do
+ def enqueue(type, payload, priority \\ 1) do
if Mix.env == :test do
handle(type, payload)
else
- GenServer.cast(__MODULE__, {:enqueue, type, payload})
+ GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})
end
end
def maybe_start_job(running_jobs, queue) do
if (:sets.size(running_jobs) < @max_jobs) && queue != [] do
- {{:value, {type, payload}}, queue} = queue_pop(queue)
+ {{type, payload}, queue} = queue_pop(queue)
{:ok, pid} = Task.start(fn -> handle(type, payload) end)
mref = Process.monitor(pid)
{:sets.add_element(mref, running_jobs), queue}
end
end
- def handle_cast({:enqueue, type, payload}, state) when type in [:incoming_doc] do
+ def handle_cast({:enqueue, type, payload, priority}, state) when type in [:incoming_doc] do
%{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
i_queue = enqueue_sorted(i_queue, {type, payload}, 1)
{i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue)
{:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}}
end
- def handle_cast({:enqueue, type, payload}, state) do
+ def handle_cast({:enqueue, type, payload, priority}, state) do
%{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state
o_queue = enqueue_sorted(o_queue, {type, payload}, 1)
{o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue)