Class: SidekiqUniqueJobs::LockConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/lock_config.rb

Overview

Gathers all configuration for a lock

which helps reduce the amount of instance variables

Author:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  @worker      = job_hash[CLASS]
  @limit       = job_hash.fetch(LOCK_LIMIT, 1)
  @timeout     = job_hash.fetch(LOCK_TIMEOUT, 0)
  @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

#errorsObject (readonly)

Returns the value of attribute errors.



46
47
48
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 46

def errors
  @errors
end

#limitObject (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_infoObject (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_conflictObject (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

#pttlObject (readonly)

Returns the value of attribute pttl.



34
35
36
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 34

def pttl
  @pttl
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



26
27
28
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 26

def timeout
  @timeout
end

#ttlInteger? (readonly)

Returns the time (in milliseconds) to live after successful.

Returns:

  • (Integer, nil)

    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

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 14

def type
  @type
end

#workerObject (readonly)

Returns the value of attribute worker.



18
19
20
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 18

def worker
  @worker
end

Class Method Details

.from_worker(options) ⇒ LockConfig

Instantiate a new lock_config based on sidekiq options in worker

Parameters:

  • options (Hash)

    sidekiq_options for worker

Returns:



55
56
57
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 55

def self.from_worker(options)
  new(options.deep_stringify_keys)
end

Instance Method Details

#errors_as_stringString

Return a nice descriptive message with all validation errors

Returns:

  • (String)


100
101
102
103
104
105
106
107
108
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 100

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

#on_client_conflictObject

the strategy to use as conflict resolution from sidekiq client



111
112
113
114
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 111

def on_client_conflict
  @on_client_conflict ||= on_conflict["client"] if on_conflict.is_a?(Hash)
  @on_client_conflict ||= on_conflict
end

#on_server_conflictObject

the strategy to use as conflict resolution from sidekiq server



117
118
119
120
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 117

def on_server_conflict
  @on_server_conflict ||= on_conflict["server"] if on_conflict.is_a?(Hash)
  @on_server_conflict ||= on_conflict
end

#valid?true, false

Is the configuration valid?

Returns:

  • (true, false)


90
91
92
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 90

def valid?
  errors.empty?
end

#wait_for_lock?true, fakse

Indicate if timeout was set

Returns:

  • (true, fakse)


80
81
82
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 80

def wait_for_lock?
  timeout.nil? || timeout.positive?
end