Repository: quantum-elixir/quantum-core
Pull request #463 (2020-10-16 15:21:01)

Support setting job `state` in scheduler's config
Currently job `state` cannot be set through config. This is useful if we wish to start with jobs deactivated and activate them on runtime.

coveralls: ## Pull Request Test Coverage Report for [Build 457808da6b182af8b24aeda73f11ccc4d293a58e-PR-463](https://coveralls.io/builds/34233156)

* **1** of **1**   **(100.0%)**  changed or added relevant line in **1** file are covered.
* **26** unchanged lines in **5** files lost coverage.
* Overall coverage decreased (**-6.3%**) to **79.501%**

---


|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [lib/quantum/date_library.ex](https://coveralls.io/builds/34233156/source?filename=lib%2Fquantum%2Fdate_library.ex#L11) | 1 | 55.0% |
| [lib/quantum/executor.ex](https://coveralls.io/builds/34233156/source?filename=lib%2Fquantum%2Fexecutor.ex#L150) | 1 | 93.1% |
| [lib/quantum/job.ex](https://coveralls.io/builds/34233156/source?filename=lib%2Fquantum%2Fjob.ex#L210) | 1 | 94.12% |
| [test/support/test_storage.ex](https://coveralls.io/builds/34233156/source?filename=test%2Fsupport%2Ftest_storage.ex#L34) | 1 | 88.89% |
| [lib/quantum/execution_broadcaster.ex](https://coveralls.io/builds/34233156/source?filename=lib%2Fquantum%2Fexecution_broadcaster.ex#L102) | 22 | 44.83% |
<!-- | **Total:** | **26** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/34233156/badge)](https://coveralls.io/builds/34233156) |
| :-- | --: |
| Change from base [Build b46f60e1057a8c9e86457a58eca0ff0e4db6ff43](https://coveralls.io/builds/34151998): |  -6.3% |
| Covered Lines: | 287 |
| Relevant Lines: | 361 |

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

maennchen: @pnezis Thanks a lot! ❤️ 

Patch:
diff --git a/lib/quantum/normalizer.ex b/lib/quantum/normalizer.ex
index ceb8814..4afc18e 100644
--- a/lib/quantum/normalizer.ex
+++ b/lib/quantum/normalizer.ex
@@ -85,6 +85,10 @@ defmodule Quantum.Normalizer do
     Job.set_timezone(job, normalize_timezone(timezone))
   end
 
+  defp normalize_job_option({:state, state}, job) do
+    Job.set_state(job, state)
+  end
+
   defp normalize_job_option(_, job), do: job
 
   @spec normalize_task(config_task) :: Job.task() | no_return
diff --git a/pages/configuration.md b/pages/configuration.md
index 3e683c5..5f5ab38 100644
--- a/pages/configuration.md
+++ b/pages/configuration.md
@@ -15,7 +15,7 @@ config :your_app, YourApp.Scheduler,
     # Runs on 18, 20, 22, 0, 2, 4, 6:
     {"0 18-6/2 * * *",         fn -> :mnesia.backup('/var/backup/mnesia') end},
     # Runs every midnight:
-    {"@daily",                 {Backup, :backup, []}}
+    {"@daily",                 {Backup, :backup, []}, state: :inactive}
   ]
 ```
 
@@ -72,6 +72,7 @@ Possible options:
 - `task` function to be performed, ex: `{Heartbeat, :send, []}` or `fn -> :something end`
 - `run_strategy` strategy on how to run tasks inside of cluster, default: `%Quantum.RunStrategy.Random{nodes: :cluster}`
 - `overlap` set to false to prevent next job from being executed if previous job is still running, default: `true`
+- `state` set to `:inactive` to deactivate a job or `:active` to activate it
 
 It is possible to control the behavior of jobs at runtime.
 


Test patch:
diff --git a/test/quantum/normalizer_test.exs b/test/quantum/normalizer_test.exs
index d9c2f6d..d331304 100644
--- a/test/quantum/normalizer_test.exs
+++ b/test/quantum/normalizer_test.exs
@@ -74,6 +74,22 @@ defmodule Quantum.NormalizerTest do
     assert normalize(Scheduler.new_job(), job) == expected_job
   end
 
+  test "normalizer of state" do
+    job = {
+      :newsletter,
+      [
+        state: :inactive
+      ]
+    }
+
+    expected_job =
+      Scheduler.new_job()
+      |> Job.set_name(:newsletter)
+      |> Job.set_state(:inactive)
+
+    assert normalize(Scheduler.new_job(), job) == expected_job
+  end
+
   test "expression tuple not extended" do
     job = {
       :newsletter,
diff --git a/test/quantum_startup_test.exs b/test/quantum_startup_test.exs
index 2b6ca2d..1431b78 100644
--- a/test/quantum_startup_test.exs
+++ b/test/quantum_startup_test.exs
@@ -19,6 +19,7 @@ defmodule QuantumStartupTest do
       test_jobs = [
         {:test_job, [schedule: ~e[1 * * * *], task: fn -> :ok end]},
         {:test_job, [schedule: ~e[2 * * * *], task: fn -> :ok end]},
+        {:inactive_job, [schedule: ~e[* * * * *], task: fn -> :ok end, state: :inactive]},
         {"3 * * * *", fn -> :ok end},
         {"4 * * * *", fn -> :ok end}
       ]
@@ -28,8 +29,9 @@ defmodule QuantumStartupTest do
       capture_log(fn ->
         {:ok, _pid} = start_supervised(Scheduler)
 
-        assert Enum.count(QuantumStartupTest.Scheduler.jobs()) == 3
+        assert Enum.count(QuantumStartupTest.Scheduler.jobs()) == 4
         assert QuantumStartupTest.Scheduler.find_job(:test_job).schedule == ~e[1 * * * *]
+        assert QuantumStartupTest.Scheduler.find_job(:inactive_job).state == :inactive
 
         :ok = stop_supervised(Scheduler)
       end)