Module: ProcessBalancer::Lock::SimpleRedis

Defined in:
lib/process_balancer/lock/simple_redis.rb

Overview

This is a simple implementation of a lock to ensure only one job runner is running for a worker This is only save for a single redis instance setup something more resilient should be used instead, e.g. and advisory lock in a DB or using RedLock ( github.com/leandromoreira/redlock-rb )

Defined Under Namespace

Classes: LockHandler

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.time_sourceObject



10
11
12
13
14
15
16
# File 'lib/process_balancer/lock/simple_redis.rb', line 10

def self.time_source
  @time_source ||= if defined?(Process::CLOCK_MONOTONIC)
                     proc { (Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1000).to_i }
                   else
                     proc { (Time.now.to_f * 1000).to_i }
                   end
end

Instance Method Details

#worker_lockObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/process_balancer/lock/simple_redis.rb', line 65

def worker_lock
  lock = LockHandler.new("lock_#{job_id}_#{worker_index}", ProcessBalancer.identity, runtime_lock_timeout)
  lock.acquire!

  if lock.acquired?
    begin
      yield lock
    ensure
      lock.release!
    end
  end
end