Class: ActiveJob::Uniqueness::Strategies::UntilAndWhileExecuting

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

Overview

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.

Constant Summary

Constants inherited from Base

Base::ACTIVEJOB_SUPPORTS_THROW_ABORT

Instance Attribute Summary collapse

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, #lock, #unlock

Constructor Details

#initialize(job:) ⇒ UntilAndWhileExecuting

Returns a new instance of UntilAndWhileExecuting.



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

def initialize(job:)
  super
  @runtime_lock_key = job.runtime_lock_key

  runtime_lock_ttl_option = job.lock_options[:runtime_lock_ttl]
  @runtime_lock_ttl = runtime_lock_ttl_option.present? ? runtime_lock_ttl_option.to_i * 1000 : lock_ttl

  @on_runtime_conflict = job.lock_options[:on_runtime_conflict] || on_conflict
end

Instance Attribute Details

#on_runtime_conflictObject (readonly)

Returns the value of attribute on_runtime_conflict.



12
13
14
# File 'lib/active_job/uniqueness/strategies/until_and_while_executing.rb', line 12

def on_runtime_conflict
  @on_runtime_conflict
end

#runtime_lock_keyObject (readonly)

Returns the value of attribute runtime_lock_key.



12
13
14
# File 'lib/active_job/uniqueness/strategies/until_and_while_executing.rb', line 12

def runtime_lock_key
  @runtime_lock_key
end

#runtime_lock_ttlObject (readonly)

Returns the value of attribute runtime_lock_ttl.



12
13
14
# File 'lib/active_job/uniqueness/strategies/until_and_while_executing.rb', line 12

def runtime_lock_ttl
  @runtime_lock_ttl
end

Instance Method Details

#around_perform(block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/active_job/uniqueness/strategies/until_and_while_executing.rb', line 33

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_performObject



24
25
26
27
28
29
30
31
# File 'lib/active_job/uniqueness/strategies/until_and_while_executing.rb', line 24

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