class Resque::DataStore::Workers
Public Class Methods
new(redis)
click to toggle source
# File lib/resque/data_store.rb, line 214 def initialize(redis) @redis = redis end
Public Instance Methods
acquire_pruning_dead_worker_lock(worker, expiry)
click to toggle source
# File lib/resque/data_store.rb, line 277 def acquire_pruning_dead_worker_lock(worker, expiry) @redis.set(redis_key_for_worker_pruning, worker.to_s, :ex => expiry, :nx => true) end
all_heartbeats()
click to toggle source
# File lib/resque/data_store.rb, line 273 def all_heartbeats @redis.hgetall(HEARTBEAT_KEY) end
get_worker_payload(worker_id)
click to toggle source
return the worker's payload i.e. job
# File lib/resque/data_store.rb, line 230 def get_worker_payload(worker_id) @redis.get("worker:#{worker_id}") end
heartbeat(worker)
click to toggle source
# File lib/resque/data_store.rb, line 264 def heartbeat(worker) heartbeat = @redis.hget(HEARTBEAT_KEY, worker.to_s) heartbeat && Time.parse(heartbeat) end
heartbeat!(worker, time)
click to toggle source
# File lib/resque/data_store.rb, line 269 def heartbeat!(worker, time) @redis.hset(HEARTBEAT_KEY, worker.to_s, time.iso8601) end
register_worker(worker)
click to toggle source
# File lib/resque/data_store.rb, line 238 def register_worker(worker) @redis.pipelined do @redis.sadd(:workers, worker) worker_started(worker) end end
remove_heartbeat(worker)
click to toggle source
# File lib/resque/data_store.rb, line 260 def remove_heartbeat(worker) @redis.hdel(HEARTBEAT_KEY, worker.to_s) end
set_worker_payload(worker, data)
click to toggle source
# File lib/resque/data_store.rb, line 281 def set_worker_payload(worker, data) @redis.set(redis_key_for_worker(worker), data) end
unregister_worker(worker, &block)
click to toggle source
# File lib/resque/data_store.rb, line 249 def unregister_worker(worker, &block) @redis.pipelined do @redis.srem(:workers, worker) @redis.del(redis_key_for_worker(worker)) @redis.del(redis_key_for_worker_start_time(worker)) @redis.hdel(HEARTBEAT_KEY, worker.to_s) block.call end end
worker_done_working(worker, &block)
click to toggle source
# File lib/resque/data_store.rb, line 289 def worker_done_working(worker, &block) @redis.pipelined do @redis.del(redis_key_for_worker(worker)) block.call end end
worker_exists?(worker_id)
click to toggle source
# File lib/resque/data_store.rb, line 234 def worker_exists?(worker_id) @redis.sismember(:workers, worker_id) end
worker_ids()
click to toggle source
# File lib/resque/data_store.rb, line 218 def worker_ids Array(@redis.smembers(:workers)) end
worker_start_time(worker)
click to toggle source
# File lib/resque/data_store.rb, line 285 def worker_start_time(worker) @redis.get(redis_key_for_worker_start_time(worker)) end
worker_started(worker)
click to toggle source
# File lib/resque/data_store.rb, line 245 def worker_started(worker) @redis.set(redis_key_for_worker_start_time(worker), Time.now.to_s) end
workers_map(worker_ids)
click to toggle source
Given a list of worker ids, returns a map of those ids to the worker's value in redis, even if that value maps to nil
# File lib/resque/data_store.rb, line 224 def workers_map(worker_ids) redis_keys = worker_ids.map { |id| "worker:#{id}" } @redis.mapped_mget(*redis_keys) end
Private Instance Methods
redis_key_for_worker(worker)
click to toggle source
# File lib/resque/data_store.rb, line 298 def redis_key_for_worker(worker) "worker:#{worker}" end
redis_key_for_worker_pruning()
click to toggle source
# File lib/resque/data_store.rb, line 306 def redis_key_for_worker_pruning "pruning_dead_workers_in_progress" end
redis_key_for_worker_start_time(worker)
click to toggle source
# File lib/resque/data_store.rb, line 302 def redis_key_for_worker_start_time(worker) "#{redis_key_for_worker(worker)}:started" end