Skip to content

Commit

Permalink
Fix type of @next_job_scheduled_at
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy committed Mar 26, 2023
1 parent 65db179 commit 926b34f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
13 changes: 7 additions & 6 deletions lib/exekutor/internal/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def start

# Always poll at startup to fill up threads, use small jitter so workers started at the same time dont hit
# the db at the same time
@next_poll_at.set (1 + (2 * Kernel.rand)).seconds.from_now
@next_poll_at.set (1 + (2 * Kernel.rand)).seconds.from_now.to_f
start_thread
true
end
Expand All @@ -73,17 +73,17 @@ def poll
# argument is given. Updates the timestamp for the earliest job is a timestamp is given and that timestamp is
# before the known timestamp. Does nothing if a timestamp is given and the earliest job timestamp is not known.
# @param scheduled_at [Time,Numeric] the time a job is scheduled at
# @return [Time] the timestamp for the next job, or +nil+ if the timestamp is unknown or no jobs are pending
# @return [float,nil] the timestamp for the next job, or +nil+ if the timestamp is unknown or no jobs are pending
def update_earliest_scheduled_at(scheduled_at = UNKNOWN)
overwrite_unknown = false
case scheduled_at
when UNKNOWN
# If we fetch the value from the DB, we can safely overwrite the UNKNOWN value
overwrite_unknown = true
scheduled_at = @reserver.earliest_scheduled_at
when Numeric
scheduled_at = Time.zone.at(scheduled_at)
scheduled_at = @reserver.earliest_scheduled_at&.to_f
when Time
scheduled_at = scheduled_at.to_f
when Numeric
# All good
else
raise ArgumentError, "scheduled_at must be a Time or Numeric"
Expand All @@ -92,7 +92,7 @@ def update_earliest_scheduled_at(scheduled_at = UNKNOWN)
updated = false
scheduled_at = @next_job_scheduled_at.update do |current|
if current == UNKNOWN
if overwrite_unknown || scheduled_at <= Time.now
if overwrite_unknown || scheduled_at <= Time.now.to_f
updated = true
scheduled_at
else
Expand All @@ -105,6 +105,7 @@ def update_earliest_scheduled_at(scheduled_at = UNKNOWN)
current
end
end

if scheduled_at == UNKNOWN
nil
else
Expand Down
8 changes: 4 additions & 4 deletions test/internal/provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_update_earliest_scheduled_at_without_args
reserver.expects(:earliest_scheduled_at).returns(scheduled_at)
provider.update_earliest_scheduled_at

assert_equal scheduled_at, provider.send(:next_job_scheduled_at)
assert_equal scheduled_at.to_f, provider.send(:next_job_scheduled_at)
end

def test_update_earliest_scheduled_at_when_unknown
Expand All @@ -154,7 +154,7 @@ def test_update_earliest_scheduled_at_with_numeric
scheduled_at = 5.minutes.from_now.to_f
provider.update_earliest_scheduled_at(scheduled_at)

assert_equal Time.at(scheduled_at), provider.send(:next_job_scheduled_at)
assert_equal scheduled_at, provider.send(:next_job_scheduled_at)
end

def test_update_earliest_scheduled_at_with_time
Expand All @@ -165,7 +165,7 @@ def test_update_earliest_scheduled_at_with_time
scheduled_at = 5.minutes.from_now
provider.update_earliest_scheduled_at(scheduled_at)

assert_equal scheduled_at, provider.send(:next_job_scheduled_at)
assert_equal scheduled_at.to_f, provider.send(:next_job_scheduled_at)
end

def test_update_earliest_scheduled_at_with_later_time
Expand All @@ -175,7 +175,7 @@ def test_update_earliest_scheduled_at_with_later_time

provider.update_earliest_scheduled_at(10.minutes.from_now)

assert_equal scheduled_at, provider.send(:next_job_scheduled_at)
assert_equal scheduled_at.to_f, provider.send(:next_job_scheduled_at)
end

def test_update_earliest_scheduled_at_with_string
Expand Down

0 comments on commit 926b34f

Please sign in to comment.