From d2a97e62ee2e90563df6fd016f74827c70a77ea4 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sat, 21 Oct 2023 13:44:54 +0200 Subject: [PATCH] Allow specifying period with unit in macro --- lib/oban/job.ex | 3 +++ test/oban/worker_test.exs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/oban/job.ex b/lib/oban/job.ex index 6494bf0f..02bbe960 100644 --- a/lib/oban/job.ex +++ b/lib/oban/job.ex @@ -382,12 +382,15 @@ defmodule Oban.Job do def cast_period(period), do: period + @valid_period_units ~w(second seconds minute minutes hour hours day days)a + @doc false @spec valid_unique_opt?({:fields | :period | :states, [atom()] | integer()}) :: boolean() def valid_unique_opt?({:fields, [_ | _] = fields}), do: fields -- [:meta | @unique_fields] == [] def valid_unique_opt?({:keys, []}), do: true def valid_unique_opt?({:keys, [_ | _] = keys}), do: Enum.all?(keys, &is_atom/1) def valid_unique_opt?({:period, :infinity}), do: true + def valid_unique_opt?({:period, {period, unit}}), do: is_integer(period) and period > 0 and unit in @valid_period_units def valid_unique_opt?({:period, period}), do: is_integer(period) and period > 0 def valid_unique_opt?({:states, [_ | _] = states}), do: states -- states() == [] def valid_unique_opt?({:timestamp, stamp}), do: stamp in ~w(inserted_at scheduled_at)a diff --git a/test/oban/worker_test.exs b/test/oban/worker_test.exs index f672a1f5..4a8c1443 100644 --- a/test/oban/worker_test.exs +++ b/test/oban/worker_test.exs @@ -23,7 +23,7 @@ defmodule Oban.WorkerTest do max_attempts: @max_attempts, priority: 1, tags: ["scheduled", "special"], - unique: [fields: [:queue, :worker], period: 60, states: [:scheduled]] + unique: [fields: [:queue, :worker], period: {1, :minute}, states: [:scheduled]] @impl Worker def perform(%{attempt: attempt}) when attempt > 1, do: attempt