Class: SidekiqUniqueJobs::Locksmith
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::Locksmith
- Includes:
- Connection, JSON, Logging, Reflectable, Script::Caller, Timing
- Defined in:
- lib/sidekiq_unique_jobs/locksmith.rb
Overview
Lock manager class that handles all the various locks
Constant Summary collapse
- CLOCK_DRIFT_FACTOR =
Returns used to take into consideration the inaccuracy of redis timestamps.
0.01- NETWORK_FACTOR =
0.04- MAX_BLOCKING_WAIT =
Returns Maximum wait time for blocking Redis operations (in seconds) Prevents blocking web requests indefinitely when used in client middleware.
5
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#item ⇒ Object
readonly
Returns the value of attribute item.
-
#job_id ⇒ Object
readonly
Returns the value of attribute job_id.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Compare this locksmith with another.
-
#delete ⇒ Object
Deletes the lock unless it has a pttl set.
-
#delete! ⇒ Object
Deletes the lock regardless of if it has a pttl set.
- #execute(&block) ⇒ Object
-
#initialize(item, redis_pool = nil) ⇒ Locksmith
constructor
Initialize a new Locksmith instance.
- #inspect ⇒ Object
-
#lock(wait: nil) ⇒ String
Create a lock for the Sidekiq job.
-
#locked?(conn = nil) ⇒ true, false
Checks if this instance is considered locked.
-
#to_s ⇒ String
Nicely formatted string with information about self.
-
#unlock(conn = nil) ⇒ false, String
Removes the lock keys from Redis if locked by the provided jid/token.
-
#unlock!(conn = nil) ⇒ false, String
Removes the lock keys from Redis.
Methods included from JSON
dump_json, load_json, safe_load_json
Methods included from Script::Caller
call_script, debug_lua, do_call, extract_args, max_history, normalize_argv, now_f, redis_version
Methods included from Timing
clock_stamp, now_f, time_source, timed
Methods included from Reflectable
Methods included from Logging
#build_message, 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
Constructor Details
#initialize(item, redis_pool = nil) ⇒ Locksmith
Initialize a new Locksmith instance
68 69 70 71 72 73 74 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 68 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
#config ⇒ Object (readonly)
Returns the value of attribute config.
53 54 55 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 53 def config @config end |
#item ⇒ Object (readonly)
Returns the value of attribute item.
57 58 59 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 57 def item @item end |
#job_id ⇒ Object (readonly)
Returns the value of attribute job_id.
49 50 51 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 49 def job_id @job_id end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
45 46 47 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 45 def key @key end |
Instance Method Details
#==(other) ⇒ true, false
Compare this locksmith with another
182 183 184 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 182 def ==(other) key == other.key && job_id == other.job_id end |
#delete ⇒ Object
Deletes the lock unless it has a pttl set
80 81 82 83 84 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 80 def delete return if config.pttl.positive? delete! end |
#delete! ⇒ Object
Deletes the lock regardless of if it has a pttl set
rubocop:disable Naming/PredicateMethod
90 91 92 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 90 def delete! call_script(:delete, key.to_a, argv).to_i.positive? end |
#execute(&block) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 109 def execute(&block) raise SidekiqUniqueJobs::InvalidArgument, "#execute needs a block" unless block redis(redis_pool) do |conn| lock!(conn, method(:primed_async), &block) end end |
#inspect ⇒ Object
171 172 173 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 171 def inspect to_s end |
#lock(wait: nil) ⇒ String
Create a lock for the Sidekiq job
100 101 102 103 104 105 106 107 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 100 def lock(wait: nil) method_name = wait ? :primed_async : :primed_sync redis(redis_pool) do |conn| lock!(conn, method(method_name), wait) do return job_id end end end |
#locked?(conn = nil) ⇒ true, false
Checks if this instance is considered locked
152 153 154 155 156 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 152 def locked?(conn = nil) return taken?(conn) if conn redis { |rcon| taken?(rcon) } end |
#to_s ⇒ String
Nicely formatted string with information about self
164 165 166 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 164 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
123 124 125 126 127 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 123 def unlock(conn = nil) return false unless locked?(conn) unlock!(conn) end |
#unlock!(conn = nil) ⇒ false, String
Removes the lock keys from Redis
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/sidekiq_unique_jobs/locksmith.rb', line 135 def unlock!(conn = nil) call_script(:unlock, key.to_a, argv, conn) do |unlocked_jid| if unlocked_jid == job_id reflect(:debug, :unlocked, item, unlocked_jid) reflect(:unlocked, item) end unlocked_jid end end |