Sha256: 353d05c382e981ad8be7df2864fb11705812232ae3d414c6973e9a2533a32850

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 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

        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

6 entries across 6 versions & 1 rubygems

Version Path
resque-scheduler-2.5.4 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-2.5.3 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-2.5.2 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-2.5.1 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-2.5.0 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-2.4.0 lib/resque/scheduler/lock/resilient.rb