module ActiveJob::Lockable

Constants

VERSION

Public Instance Methods

enqueue(options = {}) click to toggle source
Calls superclass method
# File lib/activejob/lockable/lockable.rb, line 17
def enqueue(options = {})
  @options = options
  return trigger_on_locked_action unless lock!

  super(options)
end
lock!() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 24
def lock!
  return true if lock_period.to_i <= 0
  logger.info "Acquiring lock #{lock_extra_info}"
  # `:ex => Fixnum`: Set the specified expire time, in seconds.
  # `:nx => true`: Only set the key if it does not already exist.
  # Returns boolean, lock acquired or not
  ActiveJob::Lockable::RedisStore.set(
    lock_key,
    self.job_id,
    { ex: lock_period.to_i, nx: true }
  )
rescue StandardError => e
  logger.info "EXCEPTION acquiring lock #{lock_extra_info}"
  raise
end
lock_extra_info() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 72
def lock_extra_info
  "[key #{lock_key}] [seconds #{lock_period.to_i}] [job_id #{self.job_id}] [class_name: #{self.class}]"
end
lock_key() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 46
def lock_key
  md5 = Digest::MD5.hexdigest(self.arguments.join)
  "#{self.class.name.downcase}:#{md5}"
end
lock_period() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 61
def lock_period
  return 0 unless options

  options[:lock].to_i
end
locked?() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 51
def locked?
  ActiveJob::Lockable::RedisStore.exists?(lock_key)
end
locked_ttl() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 55
def locked_ttl
  ActiveJob::Lockable::RedisStore.ttl(lock_key)
end
redis() click to toggle source

Returns the current Redis connection, raising an error if it hasn't been created

# File lib/activejob/lockable.rb, line 34
def redis
  return @redis if @redis
  raise 'Redis is not configured'
end
redis=(server) click to toggle source

Accepts:

1. A redis URL (valid for `Redis.new(url: url)`)
2. an options hash compatible with `Redis.new`
3. or a valid Redis instance (one that responds to `#smembers`). Likely,
   this will be an instance of either `Redis`, `Redis::Client`,
   `Redis::DistRedis`, or `Redis::Namespace`.
# File lib/activejob/lockable.rb, line 20
def redis=(server)
  @redis = if server.is_a?(String)
    Redis.new(:url => server, :thread_safe => true)
  elsif server.is_a?(Hash)
    Redis.new(server.merge(:thread_safe => true))
  elsif server.respond_to?(:smembers)
    server
  else
    raise ArgumentError,
      'You must supply a url, options hash or valid Redis connection instance'
  end
end
trigger_on_locked_action() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 67
def trigger_on_locked_action
  logger.info "Job is locked, expires in #{locked_ttl} second(s)"
  public_send(on_locked_action) if on_locked_action && respond_to?(on_locked_action)
end
unlock!() click to toggle source
# File lib/activejob/lockable/lockable.rb, line 40
def unlock!
  return unless locked?
  logger.info "Releasing lock #{lock_extra_info}"
  ActiveJob::Lockable::RedisStore.del(lock_key)
end