Class: Sidekiq::Workers
- Inherits:
-
Object
- Object
- Sidekiq::Workers
- Includes:
- Enumerable
- Defined in:
- lib/sidekiq/api.rb
Overview
Programmatic access to the current active worker set.
WARNING WARNING WARNING
This is live data that can change every millisecond. If you call #size => 5 and then expect #each to be called 5 times, you’re going to have a bad time.
workers = Sidekiq::Workers.new
workers.size => 2
workers.each do |name, work, started_at|
# name is a unique identifier per worker
# work is a Hash which looks like:
# { 'queue' => name, 'run_at' => timestamp, 'payload' => msg }
# started_at is a String rep of the time when the worker started working on the job
end
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#prune(older_than = 60*60) ⇒ Object
Prune old worker entries from the Busy set.
- #size ⇒ Object
Instance Method Details
#each(&block) ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/sidekiq/api.rb', line 423 def each(&block) Sidekiq.redis do |conn| workers = conn.smembers("workers") workers.each do |w| json = conn.get("worker:#{w}") next unless json msg = Sidekiq.load_json(json) block.call(w, msg, Time.at(msg['run_at']).to_s) end end end |
#prune(older_than = 60*60) ⇒ Object
Prune old worker entries from the Busy set. Worker entries can be orphaned if Sidekiq hard crashes while processing jobs. Default is to delete worker entries older than one hour.
Returns the number of records removed.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/sidekiq/api.rb', line 446 def prune(older_than=60*60) to_rem = [] Sidekiq.redis do |conn| conn.smembers('workers').each do |w| msg = conn.get("worker:#{w}") if !msg to_rem << w else m = Sidekiq.load_json(msg) run_at = Time.at(m['run_at']) # prune jobs older than one hour if run_at < (Time.now - older_than) to_rem << w else end end end end if to_rem.size > 0 Sidekiq.redis { |conn| conn.srem('workers', to_rem) } end to_rem.size end |
#size ⇒ Object
435 436 437 438 439 |
# File 'lib/sidekiq/api.rb', line 435 def size Sidekiq.redis do |conn| conn.scard("workers") end.to_i end |