Module: Gitlab::LoopHelpers

Included in:
Ci::DestroyExpiredJobArtifactsService, Environments::AutoStopService
Defined in:
lib/gitlab/loop_helpers.rb

Instance Method Summary collapse

Instance Method Details

#loop_until(timeout: nil, limit: 1_000_000) ⇒ Object

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.

Raises:

  • (ArgumentError)

10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/loop_helpers.rb', line 10

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