Skip to content

Commit

Permalink
Allow specifying period with unit in macro
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Oct 21, 2023
1 parent 6dd8eb8 commit d2a97e6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/oban/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/oban/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d2a97e6

Please sign in to comment.