Class: ActiveJob::Uniqueness::Strategies::WhileExecuting

Inherits:
Base
  • Object
show all
Defined in:
lib/active_job/uniqueness/strategies/while_executing.rb

Overview

Locks the job when the job starts. Unlocks the job when the job is finished.

Constant Summary

Constants inherited from Base

Base::ACTIVEJOB_SUPPORTS_THROW_ABORT

Instance Attribute Summary

Attributes inherited from Base

#job, #lock_key, #lock_ttl, #on_conflict

Instance Method Summary collapse

Methods inherited from Base

#after_perform, #around_enqueue, #before_enqueue, #initialize, #lock, #unlock

Constructor Details

This class inherits a constructor from ActiveJob::Uniqueness::Strategies::Base

Instance Method Details

#around_perform(block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/active_job/uniqueness/strategies/while_executing.rb', line 16

def around_perform(block)
  return if @job_aborted # ActiveJob 4.2 workaround

  block.call
ensure
  unlock(resource: lock_key, event: :runtime_unlock) unless @job_aborted
end

#before_performObject



9
10
11
12
13
14
# File 'lib/active_job/uniqueness/strategies/while_executing.rb', line 9

def before_perform
  return if lock(resource: lock_key, ttl: lock_ttl, event: :runtime_lock)

  handle_conflict(resource: lock_key, event: :runtime_conflict, on_conflict: on_conflict)
  abort_job
end