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
  @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

#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

#jobObject (readonly)

Returns the value of attribute job.



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

def job
  @job
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

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)


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

Returns:

  • (Boolean)


74
75
76
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 74

def lock_info?
  lock_info
end

#on_client_conflictObject

the strategy to use as conflict resolution from sidekiq client



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

def on_client_conflict
  @on_client_conflict ||= on_conflict["client"] || 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



121
122
123
124
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 121

def on_server_conflict
  @on_server_conflict ||= on_conflict["server"] || 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)


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

Returns:

  • (true, false)


84
85
86
# File 'lib/sidekiq_unique_jobs/lock_config.rb', line 84

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