Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use :poolboy.status to get free workers #67

Merged
merged 2 commits into from
May 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/verk/workers_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ defmodule Verk.WorkersManager do
end

def handle_info(:timeout, state) do
workers = free_workers(state.monitors, state.pool_size)
workers = free_workers(state.pool_name)
if workers != 0 do
case QueueManager.dequeue(state.queue_manager_name, workers) do
jobs when is_list(jobs) ->
Expand Down Expand Up @@ -217,8 +217,10 @@ defmodule Verk.WorkersManager do
:ok = GenEvent.ack_notify(Verk.EventManager, event)
end

defp free_workers(monitors, size) do
size - :ets.info(monitors, :size)
defp free_workers(pool_name) do
{_, free, _, _} = :poolboy.status(pool_name)

free
end

defp random_timeout(timeout) do
Expand Down
9 changes: 7 additions & 2 deletions test/workers_manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ defmodule Verk.WorkersManagerTest do
end

test "handle info timeout with no free workers", %{ monitors: monitors } do
pool_name = "pool_name"
new Verk.QueueManager
state = %State{ monitors: monitors, pool_name: "pool_name", pool_size: 1 }
state = %State{ monitors: monitors, pool_name: pool_name, pool_size: 1 }

row = { self, "job_id", "job", make_ref, "start_time" }
:ets.insert(monitors, row)

expect(:poolboy, :status, ["pool_name"], {nil, 0, nil, nil})
assert handle_info(:timeout, state) == { :noreply, state }

assert validate Verk.QueueManager
Expand All @@ -128,9 +130,11 @@ defmodule Verk.WorkersManagerTest do
:rand.seed(:exs64, {1,2,3})
queue_manager_name = :queue_manager_name
timeout = 1000
state = %State{ monitors: monitors, pool_name: "pool_name",
pool_name = "pool_name"
state = %State{ monitors: monitors, pool_name: pool_name,
pool_size: 1, queue_manager_name: queue_manager_name, timeout: timeout }

expect(:poolboy, :status, ["pool_name"], {nil, 1, nil, nil})
expect(Verk.QueueManager, :dequeue, [queue_manager_name, 1], [])

assert handle_info(:timeout, state) == { :noreply, state, 1350 }
Expand All @@ -153,6 +157,7 @@ defmodule Verk.WorkersManagerTest do

expect(Verk.QueueManager, :dequeue, [queue_manager_name, 1], [:encoded_job])
expect(Verk.Job, :decode!, [:encoded_job], job)
expect(:poolboy, :status, [pool_name], {nil, 1, nil, nil})
expect(:poolboy, :checkout, [pool_name, false], worker)
expect(Verk.Worker, :perform_async, [worker, worker, job], :ok)

Expand Down