Sha256: dc4421b4031fa7e893cf21391e64618e9c30029d4e469e0dcc17a72f10d93431

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

# vim:fileencoding=utf-8
require_relative 'base'

module Resque
  module Scheduler
    module Lock
      class Resilient < Base
        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 ||=
            Resque.redis.script(:load, <<-EOF.gsub(/^ {14}/, ''))
              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

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

          @acquire_sha ||=
            Resque.redis.script(:load, <<-EOF.gsub(/^ {14}/, ''))
              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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
resque-scheduler-4.3.0 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-4.2.1 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-4.2.0 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-4.1.0 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-4.0.0 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-3.1.0 lib/resque/scheduler/lock/resilient.rb
resque-scheduler-3.0.0 lib/resque/scheduler/lock/resilient.rb