Class: SidekiqUniqueJobs::Locksmith

Inherits:
Object
  • Object
show all
Includes:
Connection, JSON, Logging, Script::Caller, Timing
Defined in:
lib/sidekiq_unique_jobs/locksmith.rb

Overview

Lock manager class that handles all the various locks

Author:

Constant Summary collapse

CLOCK_DRIFT_FACTOR =

Returns used to take into consideration the inaccuracy of redis timestamps.

Returns:

  • (Float)

    used to take into consideration the inaccuracy of redis timestamps

0.01

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JSON

dump_json, load_json

Methods included from Script::Caller

call_script, debug_lua, do_call, extract_args, max_history, now_f, redis_version

Methods included from Timing

clock_stamp, now_f, time_source, timed

Methods included from Logging

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

Methods included from Connection

included, #redis

Constructor Details

#initialize(item, redis_pool = nil) ⇒ Locksmith

Initialize a new Locksmith instance

Parameters:

  • item (Hash)

    a Sidekiq job hash

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

    the redis connection

Options Hash (item):



58
59
60
61
62
63
64
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 58

def initialize(item, redis_pool = nil)
  @item        = item
  @key         = Key.new(item[LOCK_DIGEST] || item[UNIQUE_DIGEST]) # fallback until can be removed
  @job_id      = item[JID]
  @config      = LockConfig.new(item)
  @redis_pool  = redis_pool
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



43
44
45
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 43

def config
  @config
end

#itemObject (readonly)

Returns the value of attribute item.



47
48
49
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 47

def item
  @item
end

#job_idObject (readonly)

Returns the value of attribute job_id.



39
40
41
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 39

def job_id
  @job_id
end

#keyObject (readonly)

Returns the value of attribute key.



35
36
37
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 35

def key
  @key
end

Instance Method Details

#==(other) ⇒ true, false

Compare this locksmith with another

Parameters:

  • other (Locksmith)

    the locksmith to compare with

Returns:

  • (true, false)


154
155
156
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 154

def ==(other)
  key == other.key && job_id == other.job_id
end

#deleteObject

Deletes the lock unless it has a pttl set



70
71
72
73
74
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 70

def delete
  return if config.pttl.positive?

  delete!
end

#delete!Object

Deletes the lock regardless of if it has a pttl set



79
80
81
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 79

def delete!
  call_script(:delete, key.to_a, [job_id, config.pttl, config.type, config.limit]).positive?
end

#inspectObject

See Also:



143
144
145
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 143

def inspect
  to_s
end

#lock(&block) ⇒ String

Create a lock for the Sidekiq job

Returns:

  • (String)

    the Sidekiq job_id that was locked/queued



88
89
90
91
92
93
94
95
96
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 88

def lock(&block)
  redis(redis_pool) do |conn|
    return lock_async(conn, &block) if block

    lock_sync(conn) do
      return job_id
    end
  end
end

#locked?(conn = nil) ⇒ true, false

Checks if this instance is considered locked

Returns:

  • (true, false)

    true when the :LOCKED hash contains the job_id



124
125
126
127
128
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 124

def locked?(conn = nil)
  return taken?(conn) if conn

  redis { |rcon| taken?(rcon) }
end

#to_sString

Nicely formatted string with information about self

Returns:

  • (String)


136
137
138
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 136

def to_s
  "Locksmith##{object_id}(digest=#{key} job_id=#{job_id}, locked=#{locked?})"
end

#unlock(conn = nil) ⇒ false, String

Removes the lock keys from Redis if locked by the provided jid/token

Returns:

  • (false)

    unless locked?

  • (String)

    Sidekiq job_id (jid) if successful



104
105
106
107
108
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 104

def unlock(conn = nil)
  return false unless locked?(conn)

  unlock!(conn)
end

#unlock!(conn = nil) ⇒ false, String

Removes the lock keys from Redis

Returns:

  • (false)

    unless locked?

  • (String)

    Sidekiq job_id (jid) if successful



116
117
118
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 116

def unlock!(conn = nil)
  call_script(:unlock, key.to_a, argv, conn)
end