Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unique's period option to take units. #975

Merged
merged 3 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/oban/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ defmodule Oban.Job do

Insert a job, ensuring that it is unique within the past minute:

MyApp.Worker.new(%{id: 1}, unique: [period: 60])
MyApp.Worker.new(%{id: 1}, unique: [period: {1, :minute}])

Insert a unique job where the period is compared to the `scheduled_at` timestamp rather than
`inserted_at`:
Expand Down Expand Up @@ -366,12 +366,34 @@ defmodule Oban.Job do
end
end

@doc false
@spec cast_period(pos_integer() | {atom(), pos_integer()}) :: pos_integer()
def cast_period({value, unit}) do
unit = to_string(unit)

cond do
unit in ~w(second seconds) -> value
unit in ~w(minute minutes) -> value * 60
unit in ~w(hour hours) -> value * 60 * 60
unit in ~w(day days) -> value * 24 * 60 * 60
true -> unit
end
end

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
sorentwo marked this conversation as resolved.
Show resolved Hide resolved

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 Expand Up @@ -447,7 +469,7 @@ defmodule Oban.Job do
|> Map.new()
|> Map.put_new(:fields, @unique_fields)
|> Map.put_new(:keys, [])
|> Map.put_new(:period, @unique_period)
|> Map.update(:period, @unique_period, &cast_period/1)
|> Map.put_new(:states, @unique_states)
|> Map.put_new(:timestamp, @unique_timestamp)

Expand Down
6 changes: 6 additions & 0 deletions test/oban/job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ defmodule Oban.JobTest do
}
end

test ":unique will translate period" do
changeset = Job.new(%{}, worker: Fake, unique: [period: {1, :hour}])

assert %{period: 3600} = changeset.changes[:unique]
end

test ":unique does not accept other types of values or options" do
assert Job.new(%{}, worker: Fake, unique: true).errors[:unique]
assert Job.new(%{}, worker: Fake, unique: []).errors[:unique]
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
Loading