Sha256: 1e3f84a1821e1a95e1ef1dd636daf64737647335d85053dcbbc045534cafd90d

Contents?: true

Size: 1.84 KB

Versions: 9

Compression:

Stored size: 1.84 KB

Contents

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

module ResqueAdmin
  module Scheduler
    module Lock
      class Resilient < Base
        def acquire!
          evalsha(:acquire, [key], [value]).to_i == 1
        end

        def locked?
          evalsha(:locked, [key], [value]).to_i == 1
        end

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

        private

        def evalsha(script, keys, argv, refresh: false)
          sha_method_name = "#{script}_sha"
          ResqueAdmin.redis.evalsha(
            send(sha_method_name, refresh),
            keys: keys,
            argv: argv
          )
        rescue Redis::CommandError => e
          if e.message =~ /NOSCRIPT/
            refresh = true
            retry
          end
          raise
        end

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

          @locked_sha ||=
            ResqueAdmin.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 ||=
            ResqueAdmin.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

9 entries across 9 versions & 2 rubygems

Version Path
resque_admin-scheduler-1.2.0 lib/resque_admin/scheduler/lock/resilient.rb
resque_admin-scheduler-1.1.9 lib/resque_admin/scheduler/lock/resilient.rb
resque_admin-scheduler-1.1.8 lib/resque_admin/scheduler/lock/resilient.rb
resque_admin-scheduler-1.1.7 lib/resque_admin/scheduler/lock/resilient.rb
resque_admin-scheduler-1.0.4 lib/resque_admin/scheduler/lock/resilient.rb
resque_admin-scheduler-1.0.3 lib/resque-admin/scheduler/lock/resilient.rb
resque_admin-scheduler-1.3.0 lib/resque_admin/scheduler/lock/resilient.rb
resque-admin-scheduler-1.3.0 lib/resque_admin/scheduler/lock/resilient.rb
resque-admin-scheduler-4.3.0 lib/resque_admin/scheduler/lock/resilient.rb