Class: SidekiqUniqueJobs::Lock::BaseLock Abstract

Inherits:
Object
  • Object
show all
Includes:
SidekiqUniqueJobs::Logging
Defined in:
lib/sidekiq_unique_jobs/lock/base_lock.rb

Overview

This class is abstract.

Abstract base class for locks

Author:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SidekiqUniqueJobs::Logging

included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context, #with_configured_loggers_context, #with_logging_context

Constructor Details

#initialize(item, callback, redis_pool = nil) ⇒ BaseLock

Returns a new instance of BaseLock.

Parameters:

  • item (Hash)

    the Sidekiq job hash

  • callback (Proc)

    the callback to use after unlock

  • redis_pool (Sidekiq::RedisConnection, ConnectionPool) (defaults to: nil)

    the redis connection



26
27
28
29
30
31
32
33
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 26

def initialize(item, callback, redis_pool = nil)
  @item       = item
  @callback   = callback
  @redis_pool = redis_pool
  @attempt    = 0
  prepare_item # Used to ease testing
  @lock_config = LockConfig.new(item)
end

Class Method Details

.validate_options(options = {}) ⇒ void

This method returns an undefined value.

Validates that the sidekiq_options for the worker is valid

Parameters:

  • options (Hash) (defaults to: {})

    the sidekiq_options given to the worker



19
20
21
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 19

def self.validate_options(options = {})
  Validator.validate(options)
end

Instance Method Details

#deleteObject

Deletes the job from redis if it is locked.



64
65
66
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 64

def delete
  locksmith.delete # Soft delete (don't forcefully remove when expiration is set)
end

#delete!Object

Forcefully deletes the job from redis.

This is good for jobs when a previous lock was not unlocked


70
71
72
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 70

def delete!
  locksmith.delete! # Force delete the lock
end

#executeObject

Execute the job in the Sidekiq server processor

Raises:

  • (NotImplementedError)

    needs to be implemented in child class



52
53
54
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 52

def execute
  raise NotImplementedError, "##{__method__} needs to be implemented in #{self.class}"
end

#lock { ... } ⇒ String?

Note:

Will call a conflict strategy if lock can’t be achieved.

Locks a sidekiq job

Yields:

  • to the caller when given a block

Returns:

  • (String, nil)

    the locked jid when properly locked, else nil.



44
45
46
47
48
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 44

def lock(&block)
  return call_strategy unless (locked_token = locksmith.lock(&block))

  locked_token
end

#locked?true, false

Checks if the item has achieved a lock

Returns:

  • (true)

    when this jid has locked the job

  • (false)

    when this jid has not locked the job



77
78
79
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 77

def locked?
  locksmith.locked?
end

#locksmithSidekiqUniqueJobs::Locksmith

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The lock manager/client

Returns:



87
88
89
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 87

def locksmith
  @locksmith ||= SidekiqUniqueJobs::Locksmith.new(item, redis_pool)
end

#unlockString, false

Unlocks the job from redis

Returns:

  • (String)

    sidekiq job id when successful

  • (false)

    when unsuccessful



59
60
61
# File 'lib/sidekiq_unique_jobs/lock/base_lock.rb', line 59

def unlock
  locksmith.unlock # Only signal to release the lock
end