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
22
# 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
  @on_redis_connection_error = job.lock_options[:on_redis_connection_error] || config.on_redis_connection_error
  @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

#on_redis_connection_errorObject (readonly)

Returns the value of attribute on_redis_connection_error.



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

def on_redis_connection_error
  @on_redis_connection_error
end

Instance Method Details

#after_performObject



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

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

#around_enqueue(block) ⇒ Object



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

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

#around_perform(block) ⇒ Object



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

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

#before_enqueueObject



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

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

#before_performObject



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

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

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



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

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



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

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