summaryrefslogtreecommitdiff
path: root/lib/gitlab/job_waiter.rb
blob: 208f0e1bbeaeb9854dc87688732a7e825e09c322 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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