Skip to content

Commit

Permalink
Set conflict? flag on unique advisory lock
Browse files Browse the repository at this point in the history
The `conflict?` flag wasn't set when inserting a unique job was blocked
by an advisory lock. Now the flag is set on either a fetched duplicate,
or when the advisory lock is set.

Closes #1087
  • Loading branch information
sorentwo committed May 21, 2024
1 parent 2971861 commit 2ff529d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/oban/engines/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ defmodule Oban.Engines.Basic do
{:ok, %Job{job | conflict?: true}}
else
{:error, :locked} ->
Changeset.apply_action(changeset, :insert)
with {:ok, job} <- Changeset.apply_action(changeset, :insert) do
%Job{job | conflict?: true}
end

nil ->
Repo.insert(conf, changeset, opts)
Expand Down
11 changes: 5 additions & 6 deletions test/oban/engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,18 @@ for engine <- [Oban.Engines.Basic, Oban.Engines.Lite] do
fun = fn ->
Sandbox.allow(Repo, parent, self())

{:ok, %Job{id: id}} = Oban.insert(name, changeset)
{:ok, %Job{id: id, conflict?: conflict}} = Oban.insert(name, changeset)

id
{id, conflict}
end

ids =
results =
1..3
|> Enum.map(fn _ -> Task.async(fun) end)
|> Enum.map(&Task.await/1)
|> Enum.reject(&is_nil/1)
|> Enum.uniq()

assert 1 == length(ids)
assert 1 == results |> Enum.uniq_by(&elem(&1, 0)) |> length()
assert [false, true, true] == results |> Enum.map(&elem(&1, 1)) |> Enum.sort()
end

@tag :unique
Expand Down

0 comments on commit 2ff529d

Please sign in to comment.