diff --git a/lib/verk/workers_manager.ex b/lib/verk/workers_manager.ex index ef4291f..2d16de4 100644 --- a/lib/verk/workers_manager.ex +++ b/lib/verk/workers_manager.ex @@ -46,6 +46,16 @@ defmodule Verk.WorkersManager do end end + @process_info [:current_stacktrace, :initial_call, :reductions, :status] + @spec inspect_worker(binary | atom, binary) :: { :ok, Map.t } | { :error, :not_found } + def inspect_worker(queue, job_id) do + case :ets.match(name(queue), { :'$1', job_id, :'$2', :_, :'$3' }) do + [] -> { :error, :not_found } + [[pid, job, started_at]] -> + { :ok, %{ process: pid, job: job, started_at: started_at, info: Process.info(pid, @process_info) } } + end + end + @doc """ Create a table to monitor workers saving data about the assigned queue/pool """ diff --git a/test/workers_manager_test.exs b/test/workers_manager_test.exs index aa902e3..9df0c0f 100644 --- a/test/workers_manager_test.exs +++ b/test/workers_manager_test.exs @@ -55,6 +55,24 @@ defmodule Verk.WorkersManagerTest do assert running_jobs("queue_name") == [] end + test "inspect_worker with no matching job_id" do + inspect_worker("queue_name", "job_id") == { :error, :not_found } + end + + test "inspect_worker with matching job_id", %{ monitors: monitors } do + row = { self, "job_id", "job data", make_ref, "start_time" } + :ets.insert(monitors, row) + + { :ok, result } = inspect_worker("queue_name", "job_id") + + assert result[:job] == "job data" + assert result[:process] == self + assert result[:started_at] == "start_time" + + expected = [:current_stacktrace, :initial_call, :reductions, :status] + assert Enum.all?(expected, &Keyword.has_key?(result[:info], &1)) + end + test "init" do name = :workers_manager queue_name = "queue_name"