Repository: quantum-elixir/quantum-core
Pull request #348 (2018-07-30 09:22:50)

Solution: Add helpers for querying task registry
Closes: #344 

This PR adds functions for querying task registry if specific task is running in the cluster or if any tasks are running.

I have a question about doctests. All documented functions with examples in `TaskRegistry` module have doctests disabled. I followed what is currently there and listed the two new functions in `except`. Shall I delete doctests for this module or do you want to have them enabled and listed all functions in except?

Thanks for your time reviewing the changes!

coveralls: ## Pull Request Test Coverage Report for [Build 779](https://coveralls.io/builds/18224693)

* **8** of **8**   **(100.0%)**  changed or added relevant lines in **1** file are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage increased (+**0.1%**) to **94.925%**

---



|  Totals | [![Coverage Status](https://coveralls.io/builds/18224693/badge)](https://coveralls.io/builds/18224693) |
| :-- | --: |
| Change from base [Build 776](https://coveralls.io/builds/16368914): |  0.1% |
| Covered Lines: | 318 |
| Relevant Lines: | 335 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/lib/quantum/task_registry.ex b/lib/quantum/task_registry.ex
index 62a9d50..2bf63ef 100644
--- a/lib/quantum/task_registry.ex
+++ b/lib/quantum/task_registry.ex
@@ -54,6 +54,38 @@ defmodule Quantum.TaskRegistry do
     GenServer.cast(server, {:finished, task, node})
   end
 
+  @doc """
+  Query if a task with given name is running
+
+  ### Examples
+
+      iex> Quantum.TaskRegistry.is_running?(server, running_job.name)
+      true
+
+      iex> Quantum.TaskRegistry.is_running?(server, not_running_job.name)
+      false
+
+  """
+  def is_running?(server, task) do
+    GenServer.call(server, {:is_running?, task})
+  end
+
+  @doc """
+  Query if any tasks are running in the cluster
+
+  ### Examples
+
+      iex> Quantum.TaskRegistry.any_running?(server_with_running_tasks)
+      true
+
+      iex> Quantum.TaskRegistry.any_running?(server_without_running_tasks)
+      false
+
+  """
+  def any_running?(server) do
+    GenServer.call(server, :any_running?)
+  end
+
   @doc false
   def init(args) do
     {:ok, args}
@@ -68,6 +100,24 @@ defmodule Quantum.TaskRegistry do
     end
   end
 
+  @doc false
+  def handle_call({:is_running?, task}, _caller, state) do
+    if Enum.empty?(Map.get(state, task, [])) do
+      {:reply, false, state}
+    else
+      {:reply, true, state}
+    end
+  end
+
+  @doc false
+  def handle_call(:any_running?, _caller, state) do
+    if Enum.empty?(state) do
+      {:reply, false, state}
+    else
+      {:reply, true, state}
+    end
+  end
+
   @doc false
   def handle_cast({:finished, task, node}, state) do
     state = Map.update(state, task, [], &(&1 -- [node]))


Test patch:
diff --git a/test/quantum/task_registry_test.exs b/test/quantum/task_registry_test.exs
index d07a422..753a79d 100644
--- a/test/quantum/task_registry_test.exs
+++ b/test/quantum/task_registry_test.exs
@@ -5,7 +5,8 @@ defmodule Quantum.TaskRegistryTest do
 
   alias Quantum.TaskRegistry
 
-  doctest TaskRegistry, except: [mark_running: 3, mark_finished: 3]
+  doctest TaskRegistry,
+    except: [mark_running: 3, mark_finished: 3, is_running?: 2, any_running?: 1]
 
   setup do
     {:ok, registry} = start_supervised({TaskRegistry, __MODULE__})
@@ -44,4 +45,33 @@ defmodule Quantum.TaskRegistryTest do
       assert :ok = TaskRegistry.mark_finished(registry, task, self())
     end
   end
+
+  describe "is_running?" do
+    test "not running", %{registry: registry} do
+      task = make_ref()
+      assert false == TaskRegistry.is_running?(registry, task)
+    end
+
+    test "running", %{registry: registry} do
+      task = make_ref()
+
+      TaskRegistry.mark_running(registry, task, self())
+
+      assert true == TaskRegistry.is_running?(registry, task)
+    end
+  end
+
+  describe "any_running?" do
+    test "not running", %{registry: registry} do
+      assert false == TaskRegistry.any_running?(registry)
+    end
+
+    test "running", %{registry: registry} do
+      task = make_ref()
+
+      TaskRegistry.mark_running(registry, task, self())
+
+      assert true == TaskRegistry.any_running?(registry)
+    end
+  end
 end