Class: SidekiqUniqueJobs::LockConfig
- Inherits:
-
Object
- Object
- SidekiqUniqueJobs::LockConfig
- Defined in:
- lib/sidekiq_unique_jobs/lock_config.rb
Overview
Gathers all configuration for a lock
which helps reduce the amount of instance variables
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#job ⇒ Object
readonly
Returns the value of attribute job.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#lock_info ⇒ Object
readonly
Returns the value of attribute lock_info.
-
#on_conflict ⇒ Object
readonly
Returns the value of attribute on_conflict.
-
#pttl ⇒ Object
readonly
Returns the value of attribute pttl.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#ttl ⇒ Integer?
readonly
The time (in milliseconds) to live after successful.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.from_worker(options) ⇒ LockConfig
Instantiate a new lock_config based on sidekiq options in worker.
Instance Method Summary collapse
-
#errors_as_string ⇒ String
Return a nice descriptive message with all validation errors.
-
#initialize(job_hash = {}) ⇒ LockConfig
constructor
A new instance of LockConfig.
- #lock_info? ⇒ Boolean
-
#on_client_conflict ⇒ Object
the strategy to use as conflict resolution from sidekiq client.
-
#on_server_conflict ⇒ Object
the strategy to use as conflict resolution from sidekiq server.
-
#valid? ⇒ true, false
Is the configuration valid?.
-
#wait_for_lock? ⇒ true, false
Indicate if timeout was set.
Constructor Details
#initialize(job_hash = {}) ⇒ LockConfig
Returns a new instance of LockConfig.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 59 def initialize(job_hash = {}) @type = job_hash[LOCK]&.to_sym @job = SidekiqUniqueJobs.safe_constantize(job_hash[CLASS]) @limit = job_hash.fetch(LOCK_LIMIT, 1)&.to_i @timeout = job_hash.fetch(LOCK_TIMEOUT, 0)&.to_i @ttl = job_hash.fetch(LOCK_TTL) { job_hash.fetch(LOCK_EXPIRATION, nil) }.to_i @pttl = ttl * 1_000 @lock_info = job_hash.fetch(LOCK_INFO) { SidekiqUniqueJobs.config.lock_info } @on_conflict = job_hash.fetch(ON_CONFLICT, nil) @errors = job_hash.fetch(ERRORS) { {} } @on_client_conflict = job_hash[ON_CLIENT_CONFLICT] @on_server_conflict = job_hash[ON_SERVER_CONFLICT] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
46 47 48 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 46 def errors @errors end |
#job ⇒ Object (readonly)
Returns the value of attribute job.
18 19 20 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 18 def job @job end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
22 23 24 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 22 def limit @limit end |
#lock_info ⇒ Object (readonly)
Returns the value of attribute lock_info.
38 39 40 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 38 def lock_info @lock_info end |
#on_conflict ⇒ Object (readonly)
Returns the value of attribute on_conflict.
42 43 44 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 42 def on_conflict @on_conflict end |
#pttl ⇒ Object (readonly)
Returns the value of attribute pttl.
34 35 36 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 34 def pttl @pttl end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
26 27 28 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 26 def timeout @timeout end |
#ttl ⇒ Integer? (readonly)
Returns the time (in milliseconds) to live after successful.
30 31 32 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 30 def ttl @ttl end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
14 15 16 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 14 def type @type end |
Class Method Details
.from_worker(options) ⇒ LockConfig
Instantiate a new lock_config based on sidekiq options in worker
55 56 57 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 55 def self.from_worker() new(.deep_stringify_keys) end |
Instance Method Details
#errors_as_string ⇒ String
Return a nice descriptive message with all validation errors
104 105 106 107 108 109 110 111 112 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 104 def errors_as_string return if valid? @errors_as_string ||= begin error_msg = +"\t" error_msg << errors.map { |key, val| "#{key}: :#{val}" }.join("\n\t") error_msg end end |
#lock_info? ⇒ Boolean
74 75 76 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 74 def lock_info? lock_info end |
#on_client_conflict ⇒ Object
the strategy to use as conflict resolution from sidekiq client
115 116 117 118 119 120 121 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 115 def on_client_conflict @on_client_conflict ||= if on_conflict.is_a?(Hash) on_conflict["client"] || on_conflict[:client] else on_conflict end end |
#on_server_conflict ⇒ Object
the strategy to use as conflict resolution from sidekiq server
124 125 126 127 128 129 130 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 124 def on_server_conflict @on_server_conflict ||= if on_conflict.is_a?(Hash) on_conflict["server"] || on_conflict[:server] else on_conflict end end |
#valid? ⇒ true, false
Is the configuration valid?
94 95 96 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 94 def valid? errors.empty? end |
#wait_for_lock? ⇒ true, false
Indicate if timeout was set
84 85 86 |
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 84 def wait_for_lock? timeout.nil? || timeout.positive? end |