Module: Resque::SchedulerLocking

Included in:
Scheduler
Defined in:
lib/resque/scheduler_locking.rb

Instance Method Summary collapse

Instance Method Details

#acquire_master_lock!Object



90
91
92
93
94
95
# File 'lib/resque/scheduler_locking.rb', line 90

def acquire_master_lock!
  if Resque.redis.setnx(master_lock_key, master_lock_value)
    extend_lock!
    true
  end
end

#extend_lock!Object



85
86
87
88
# File 'lib/resque/scheduler_locking.rb', line 85

def extend_lock!
  # If the master fails to checkin for 3 minutes, the lock is released and is up for grabs
  Resque.redis.expire(master_lock_key, lock_timeout)
end

#has_master_lock?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/resque/scheduler_locking.rb', line 97

def has_master_lock?
  if Resque.redis.get(master_lock_key) == master_lock_value
    extend_lock!
    # Since this process could lose the lock between checking
    # if it has it and extending the lock, check again to make 
    # sure it still has it.
    if Resque.redis.get(master_lock_key) == master_lock_value
      true
    end
  end
end

#hostnameObject



65
66
67
# File 'lib/resque/scheduler_locking.rb', line 65

def hostname
  Socket.gethostbyname(Socket.gethostname).first
end

#is_master?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/resque/scheduler_locking.rb', line 73

def is_master?
  acquire_master_lock! || has_master_lock?
end

#lock_timeoutObject



61
62
63
# File 'lib/resque/scheduler_locking.rb', line 61

def lock_timeout
  @lock_timeout ||= 60 * 3 # 3 minutes
end

#lock_timeout=(v) ⇒ Object

The TTL (in seconds) for the master lock



57
58
59
# File 'lib/resque/scheduler_locking.rb', line 57

def lock_timeout=(v)
  @lock_timeout = v
end

#master_lock_keyObject



81
82
83
# File 'lib/resque/scheduler_locking.rb', line 81

def master_lock_key
  :master_lock
end

#master_lock_valueObject



77
78
79
# File 'lib/resque/scheduler_locking.rb', line 77

def master_lock_value
  [hostname, process_id].join(':')
end

#process_idObject



69
70
71
# File 'lib/resque/scheduler_locking.rb', line 69

def process_id
  Process.pid
end