Class: ActiveJob::Uniqueness::Strategies::Base

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

Overview

Base strategy is not supposed to actually be used as uniqueness strategy.

Defined Under Namespace

Modules: LockingOnEnqueue

Constant Summary collapse

ACTIVEJOB_SUPPORTS_THROW_ABORT =
ActiveJob.gem_version >= Gem::Version.new('5.0')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job:) ⇒ Base

Returns a new instance of Base.



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

def initialize(job:)
  @lock_key = job.lock_key
  @lock_ttl = (job.lock_options[:lock_ttl] || config.lock_ttl).to_i * 1000 # ms
  @on_conflict = job.lock_options[:on_conflict] || config.on_conflict
  @job = job
end

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



14
15
16
# File 'lib/active_job/uniqueness/strategies/base.rb', line 14

def job
  @job
end

#lock_keyObject (readonly)

Returns the value of attribute lock_key.



14
15
16
# File 'lib/active_job/uniqueness/strategies/base.rb', line 14

def lock_key
  @lock_key
end

#lock_ttlObject (readonly)

Returns the value of attribute lock_ttl.



14
15
16
# File 'lib/active_job/uniqueness/strategies/base.rb', line 14

def lock_ttl
  @lock_ttl
end

#on_conflictObject (readonly)

Returns the value of attribute on_conflict.



14
15
16
# File 'lib/active_job/uniqueness/strategies/base.rb', line 14

def on_conflict
  @on_conflict
end

Instance Method Details

#after_performObject



53
54
55
# File 'lib/active_job/uniqueness/strategies/base.rb', line 53

def after_perform
  # Expected to be overriden in the descendant strategy
end

#around_enqueue(block) ⇒ Object



43
44
45
46
# File 'lib/active_job/uniqueness/strategies/base.rb', line 43

def around_enqueue(block)
  # Expected to be overriden in the descendant strategy
  block.call
end

#around_perform(block) ⇒ Object



48
49
50
51
# File 'lib/active_job/uniqueness/strategies/base.rb', line 48

def around_perform(block)
  # Expected to be overriden in the descendant strategy
  block.call
end

#before_enqueueObject



35
36
37
# File 'lib/active_job/uniqueness/strategies/base.rb', line 35

def before_enqueue
  # Expected to be overriden in the descendant strategy
end

#before_performObject



39
40
41
# File 'lib/active_job/uniqueness/strategies/base.rb', line 39

def before_perform
  # Expected to be overriden in the descendant strategy
end

#lock(resource:, ttl:, event: :lock) ⇒ Object



23
24
25
26
27
# File 'lib/active_job/uniqueness/strategies/base.rb', line 23

def lock(resource:, ttl:, event: :lock)
  lock_manager.lock(resource, ttl).tap do |result|
    instrument(event, resource: resource, ttl: ttl) if result
  end
end

#unlock(resource:, event: :unlock) ⇒ Object



29
30
31
32
33
# File 'lib/active_job/uniqueness/strategies/base.rb', line 29

def unlock(resource:, event: :unlock)
  lock_manager.delete_lock(resource).tap do
    instrument(event, resource: resource)
  end
end