class ActiveJob::Uniqueness::Strategies::UntilAndWhileExecuting

Locks the job when it is pushed to the queue. Unlocks the job before the job is started. Then creates runtime lock to prevent simultaneous jobs from being executed.

Attributes

on_runtime_conflict[R]
runtime_lock_ttl[R]

Public Class Methods

new(runtime_lock_ttl: nil, on_runtime_conflict: nil, **params) click to toggle source
# File lib/active_job/uniqueness/strategies/until_and_while_executing.rb, line 14
def initialize(runtime_lock_ttl: nil, on_runtime_conflict: nil, **params)
  super(**params)
  @runtime_lock_ttl = runtime_lock_ttl.present? ? runtime_lock_ttl.to_i * 1000 : lock_ttl
  @on_runtime_conflict = on_runtime_conflict || on_conflict
end

Public Instance Methods

around_perform(block) click to toggle source
# File lib/active_job/uniqueness/strategies/until_and_while_executing.rb, line 29
def around_perform(block)
  return if @job_aborted # ActiveJob 4.2 workaround

  block.call
ensure
  unlock(resource: runtime_lock_key, event: :runtime_unlock) unless @job_aborted
end
before_perform() click to toggle source
# File lib/active_job/uniqueness/strategies/until_and_while_executing.rb, line 20
def before_perform
  unlock(resource: lock_key)

  return if lock(resource: runtime_lock_key, ttl: runtime_lock_ttl, event: :runtime_lock)

  handle_conflict(on_conflict: on_runtime_conflict, resource: runtime_lock_key, event: :runtime_conflict)
  abort_job
end
runtime_lock_key() click to toggle source
# File lib/active_job/uniqueness/strategies/until_and_while_executing.rb, line 37
def runtime_lock_key
  [lock_key, 'runtime'].join(':')
end