Class: Sidetiq::Lock::MetaData

Inherits:
Object
  • Object
show all
Extended by:
Sidekiq::ExceptionHandler
Defined in:
lib/sidetiq/lock/meta_data.rb

Constant Summary collapse

OWNER =
"#{Socket.gethostname}:#{Process.pid}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ MetaData

Returns a new instance of MetaData.



31
32
33
34
35
# File 'lib/sidetiq/lock/meta_data.rb', line 31

def initialize(hash = {})
  @owner = hash[:owner]
  @timestamp = hash[:timestamp]
  @key = hash[:key]
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/sidetiq/lock/meta_data.rb', line 6

def key
  @key
end

#ownerObject

Returns the value of attribute owner.



6
7
8
# File 'lib/sidetiq/lock/meta_data.rb', line 6

def owner
  @owner
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/sidetiq/lock/meta_data.rb', line 6

def timestamp
  @timestamp
end

Class Method Details

.for_new_lock(key) ⇒ Object



11
12
13
# File 'lib/sidetiq/lock/meta_data.rb', line 11

def for_new_lock(key)
  new(owner: OWNER, timestamp: Sidetiq.clock.gettime.to_f, key: key)
end

.from_json(json = "") ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sidetiq/lock/meta_data.rb', line 15

def from_json(json = "")
  # Avoid TypeError when nil is passed to Sidekiq.load_json.
  json = "" if json.nil?

  hash = Sidekiq.load_json(json).symbolize_keys
  new(hash)
rescue StandardError => e
  if json != ""
    # Looks like garbage lock metadata, so report it.
    handle_exception(e, context: "Garbage lock meta data detected: #{json}")
  end

  new
end

Instance Method Details

#pttlObject



37
38
39
# File 'lib/sidetiq/lock/meta_data.rb', line 37

def pttl
  Sidekiq.redis { |r| r.pttl(key) }
end

#to_jsonObject



41
42
43
44
45
# File 'lib/sidetiq/lock/meta_data.rb', line 41

def to_json
  instance_variables.each_with_object({}) do |var, hash|
    hash[var.to_s.delete("@")] = instance_variable_get(var)
  end.to_json
end

#to_sObject



47
48
49
# File 'lib/sidetiq/lock/meta_data.rb', line 47

def to_s
  "Sidetiq::Lock on #{key} set at #{timestamp} by #{owner}"
end