Skip to content

Commit

Permalink
Safeguard Time.now
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Jan 15, 2024
1 parent c23168d commit 17a4157
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 8 deletions.
9 changes: 9 additions & 0 deletions ruby/lib/ci/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ module Warnings
RESERVED_LOST_TEST = :RESERVED_LOST_TEST
end

GET_NOW = ::Time.method(:now)
private_constant :GET_NOW
def time_now
# Mocks like freeze_time should be cleaned when ci-queue runs, however,
# we experienced cases when tests were enqueued with wrong timestamps, so we
# safeguard Time.now here.
GET_NOW.call
end

def requeueable?(test_result)
requeueable.nil? || requeueable.call(test_result)
end
Expand Down
2 changes: 1 addition & 1 deletion ruby/lib/ci/queue/circuit_breaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def message
private

def current_timestamp
Time.now.to_i
CI::Queue.time_now.to_i
end
end

Expand Down
2 changes: 1 addition & 1 deletion ruby/lib/ci/queue/redis/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def exhausted?

def expired?
if (created_at = redis.get(key('created-at')))
(created_at.to_f + config.redis_ttl + TEN_MINUTES) < Time.now.to_f
(created_at.to_f + config.redis_ttl + TEN_MINUTES) < CI::Queue.time_now.to_f
else
# if there is no created at set anymore we assume queue is expired
true
Expand Down
2 changes: 1 addition & 1 deletion ruby/lib/ci/queue/redis/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def wait_for_workers

def active_workers?
# if there are running jobs we assume there are still agents active
redis.zrangebyscore(key('running'), Time.now.to_f - config.timeout, "+inf", limit: [0,1]).count > 0
redis.zrangebyscore(key('running'), CI::Queue.time_now.to_f - config.timeout, "+inf", limit: [0,1]).count > 0
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions ruby/lib/ci/queue/redis/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def try_to_reserve_test
key('worker', worker_id, 'queue'),
key('owners'),
],
argv: [Time.now.to_f],
argv: [CI::Queue.time_now.to_f],
)
end

Expand All @@ -188,7 +188,7 @@ def try_to_reserve_lost_test
key('worker', worker_id, 'queue'),
key('owners'),
],
argv: [Time.now.to_f, timeout],
argv: [CI::Queue.time_now.to_f, timeout],
)

if lost_test
Expand Down
2 changes: 1 addition & 1 deletion ruby/lib/ci/queue/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def created_at=(timestamp)
end

def expired?
(@created_at.to_f TEN_MINUTES) < Time.now.to_f
(@created_at.to_f TEN_MINUTES) < CI::Queue.time_now.to_f
end

def populated?
Expand Down
2 changes: 1 addition & 1 deletion ruby/lib/minitest/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def flaky?
private

def current_timestamp
Time.now.to_i
CI::Queue.time_now.to_i
end
end

Expand Down
2 changes: 1 addition & 1 deletion ruby/lib/minitest/queue/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def run_command
end
end

queue.rescue_connection_errors { queue.created_at = Time.now.to_f }
queue.rescue_connection_errors { queue.created_at = CI::Queue.time_now.to_f }

set_load_path
Minitest.queue = queue
Expand Down

0 comments on commit 17a4157

Please sign in to comment.