Sha256: 942c4cafeb112625943e85d71553cfcc6b1bbc20e7e00faa0532be6255f6a00d

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

require 'resque/scheduler/lock/base'

module Resque
  class Scheduler
    module Lock
      class Resilient < Base # rubocop:disable TrailingComma
        def acquire!
          Resque.redis.evalsha(
            acquire_sha,
            :keys => [key],
            :argv => [value]
          ).to_i == 1
        end

        def locked?
          Resque.redis.evalsha(
            locked_sha,
            :keys => [key],
            :argv => [value]
          ).to_i == 1
        end

        def timeout=(seconds)
          if locked?
            @timeout = seconds
            @locked_sha = nil
            @acquire_sha = nil
          end
          @timeout
        end

        private

        def locked_sha(refresh = false)
          @locked_sha = nil if refresh

          @locked_sha ||= begin
            Resque.redis.script(
              :load,
              <<-EOF
if redis.call('GET', KEYS[1]) == ARGV[1]
then
  redis.call('EXPIRE', KEYS[1], #{timeout})

  if redis.call('GET', KEYS[1]) == ARGV[1]
  then
    return 1
  end
end

return 0
EOF
            )
          end
        end

        def acquire_sha(refresh = false)
          @acquire_sha = nil if refresh

          @acquire_sha ||= begin
            Resque.redis.script(
              :load,
              <<-EOF
if redis.call('SETNX', KEYS[1], ARGV[1]) == 1
then
  redis.call('EXPIRE', KEYS[1], #{timeout})
  return 1
else
  return 0
end
EOF
            )
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resque-scheduler-2.5.5 lib/resque/scheduler/lock/resilient.rb