summaryrefslogtreecommitdiff
path: root/lib/gitlab/loop_helpers.rb
blob: 3873156a3b058a4519ae4f4cb3f6972fc5a05a69 (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
# frozen_string_literal: true

module Gitlab
  module LoopHelpers
    ##
    # This helper method repeats the same task until it's expired.
    #
    # Note: ExpiredLoopError does not happen until the given block finished.
    #       Please do not use this method for heavy or asynchronous operations.
    def loop_until(timeout: nil, limit: 1_000_000)
      raise ArgumentError unless limit

      start = Time.now

      limit.times do
        return true unless yield

        return false if timeout && (Time.now - start) > timeout
      end

      false
    end
  end
end