gitlab-org--gitlab-foss/lib/gitlab/job_waiter.rb
Yorick Peterse d505a2b8e6
Reduce wait timings for Sidekiq jobs
This reduces the time spent waiting for Sidekiq jobs to complete in
JobWaiter, and reduces the sleep interval when trying to acquire the
lease for refreshing authorizations. These changes should reduce the
time spent just waiting for a lock, which we seem to be spending most
time in when running the AuthorizedProjectsWorker.
2017-06-19 16:14:07 +02:00

27 lines
785 B
Ruby

module Gitlab
# JobWaiter can be used to wait for a number of Sidekiq jobs to complete.
class JobWaiter
# The sleep interval between checking keys, in seconds.
INTERVAL = 0.1
# jobs - The job IDs to wait for.
def initialize(jobs)
@jobs = jobs
end
# Waits for all the jobs to be completed.
#
# timeout - The maximum amount of seconds to block the caller for. This
# ensures we don't indefinitely block a caller in case a job takes
# long to process, or is never processed.
def wait(timeout = 10)
start = Time.current
while (Time.current - start) <= timeout
break if SidekiqStatus.all_completed?(@jobs)
sleep(INTERVAL) # to not overload Redis too much.
end
end
end
end