Skip to content

Commit

Permalink
Retain state override with scheduled_in time
Browse files Browse the repository at this point in the history
The presence of a `scheduled_in` timestamp should only override the
state if it is "available" or no state is set.
  • Loading branch information
sorentwo committed Jul 1, 2023
1 parent 5d0245d commit 5005d5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/oban/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ defmodule Oban.Job do
end

defp put_state(changeset) do
case fetch_change(changeset, :scheduled_at) do
{:ok, _} -> put_change(changeset, :state, "scheduled")
:error -> changeset
case {get_change(changeset, :state), get_change(changeset, :scheduled_at)} do
{nil, %_{}} -> put_change(changeset, :state, "scheduled")
_ -> changeset
end
end

Expand Down
18 changes: 12 additions & 6 deletions test/oban/job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Oban.JobTest do
now = DateTime.utc_now()

for unit <- ~w(second seconds minute minutes hour hours day days week weeks)a do
%{changes: changes} = Job.new(%{}, worker: Fake, schedule_in: {1, unit})
%{changes: changes} = Job.new(%{}, schedule_in: {1, unit})

assert changes[:state] == "scheduled"
assert changes[:scheduled_at]
Expand All @@ -33,11 +33,17 @@ defmodule Oban.JobTest do
end

test ":schedule_in does not accept other types of values" do
assert Job.new(%{}, worker: Fake, schedule_in: true).errors[:schedule_in]
assert Job.new(%{}, worker: Fake, schedule_in: "10").errors[:schedule_in]
assert Job.new(%{}, worker: Fake, schedule_in: 0.12).errors[:schedule_in]
assert Job.new(%{}, worker: Fake, schedule_in: {6, :units}).errors[:schedule_in]
assert Job.new(%{}, worker: Fake, schedule_in: {6.0, :hours}).errors[:schedule_in]
assert Job.new(%{}, schedule_in: true).errors[:schedule_in]
assert Job.new(%{}, schedule_in: "10").errors[:schedule_in]
assert Job.new(%{}, schedule_in: 0.12).errors[:schedule_in]
assert Job.new(%{}, schedule_in: {6, :units}).errors[:schedule_in]
assert Job.new(%{}, schedule_in: {6.0, :hours}).errors[:schedule_in]
end

test "automatically setting the state to scheduled" do
assert %{changes: %{state: "scheduled"}} = Job.new(%{}, schedule_in: 1)
assert %{changes: %{state: "scheduled"}} = Job.new(%{}, schedule_in: 1, state: "available")
assert %{changes: %{state: "completed"}} = Job.new(%{}, schedule_in: 1, state: "completed")
end
end

Expand Down

0 comments on commit 5005d5a

Please sign in to comment.