Sha256: 4f90f93ece67579eac235db9cac28ed67ce4899d990cef8a3b6f805ff0b3e47e

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

module Sidetiq
  module Lock
    class MetaData
      OWNER = "#{Socket.gethostname}:#{Process.pid}"

      attr_accessor :owner, :timestamp, :key

      class << self
        include Sidekiq::ExceptionHandler

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

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

          hash = JSON.parse(json, symbolize_names: true)
          new(hash)
        rescue JSON::ParserError => e
          if json != ""
            # Looks like garbage lock metadata, so report it.
            handle_exception(e, context: "Garbage lock meta data detected: #{json}")
          end

          new
        end
      end

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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sidetiq-0.4.0 lib/sidetiq/lock/meta_data.rb
sidetiq-0.4.0.rc4 lib/sidetiq/lock/meta_data.rb
sidetiq-0.4.0.rc3 lib/sidetiq/lock/meta_data.rb
sidetiq-0.4.0.rc2 lib/sidetiq/lock/meta_data.rb
sidetiq-0.4.0.rc1 lib/sidetiq/lock/meta_data.rb