Class: Resque::DataStore::Workers

Inherits:
Object
  • Object
show all
Defined in:
lib/resque/data_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Workers

Returns a new instance of Workers.



215
216
217
# File 'lib/resque/data_store.rb', line 215

def initialize(redis)
  @redis = redis
end

Instance Method Details

#all_heartbeatsObject



274
275
276
# File 'lib/resque/data_store.rb', line 274

def all_heartbeats
  @redis.hgetall(HEARTBEAT_KEY)
end

#get_worker_payload(worker_id) ⇒ Object

return the worker’s payload i.e. job



231
232
233
# File 'lib/resque/data_store.rb', line 231

def get_worker_payload(worker_id)
  @redis.get("worker:#{worker_id}")
end

#heartbeat(worker) ⇒ Object



265
266
267
268
# File 'lib/resque/data_store.rb', line 265

def heartbeat(worker)
  heartbeat = @redis.hget(HEARTBEAT_KEY, worker.to_s)
  heartbeat && Time.parse(heartbeat)
end

#heartbeat!(worker, time) ⇒ Object



270
271
272
# File 'lib/resque/data_store.rb', line 270

def heartbeat!(worker, time)
  @redis.hset(HEARTBEAT_KEY, worker.to_s, time.iso8601)
end

#register_worker(worker) ⇒ Object



239
240
241
242
243
244
# File 'lib/resque/data_store.rb', line 239

def register_worker(worker)
  @redis.pipelined do
    @redis.sadd(:workers, worker)
    worker_started(worker)
  end
end

#remove_heartbeat(worker) ⇒ Object



261
262
263
# File 'lib/resque/data_store.rb', line 261

def remove_heartbeat(worker)
  @redis.hdel(HEARTBEAT_KEY, worker.to_s)
end

#set_worker_payload(worker, data) ⇒ Object



278
279
280
# File 'lib/resque/data_store.rb', line 278

def set_worker_payload(worker, data)
  @redis.set(redis_key_for_worker(worker), data)
end

#unregister_worker(worker, &block) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/resque/data_store.rb', line 250

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) ⇒ Object



286
287
288
289
290
291
# File 'lib/resque/data_store.rb', line 286

def worker_done_working(worker, &block)
  @redis.pipelined do
    @redis.del(redis_key_for_worker(worker))
    block.call
  end
end

#worker_exists?(worker_id) ⇒ Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/resque/data_store.rb', line 235

def worker_exists?(worker_id)
  @redis.sismember(:workers, worker_id)
end

#worker_idsObject



219
220
221
# File 'lib/resque/data_store.rb', line 219

def worker_ids
  Array(@redis.smembers(:workers))
end

#worker_start_time(worker) ⇒ Object



282
283
284
# File 'lib/resque/data_store.rb', line 282

def worker_start_time(worker)
  @redis.get(redis_key_for_worker_start_time(worker))
end

#worker_started(worker) ⇒ Object



246
247
248
# File 'lib/resque/data_store.rb', line 246

def worker_started(worker)
  @redis.set(redis_key_for_worker_start_time(worker), Time.now.to_s)
end

#workers_map(worker_ids) ⇒ Object

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



225
226
227
228
# File 'lib/resque/data_store.rb', line 225

def workers_map(worker_ids)
  redis_keys = worker_ids.map { |id| "worker:#{id}" }
  @redis.mapped_mget(*redis_keys)
end