1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Add started_at info to the Workers API, thanks @mariovisic

This commit is contained in:
Mike Perham 2013-05-22 21:50:22 -07:00
parent 60ff419151
commit f024c6079c
2 changed files with 12 additions and 6 deletions

View file

@ -372,15 +372,16 @@ module Sidekiq
# WARNING WARNING WARNING
#
# This is live data that can change every millisecond.
# If you do #size => 5 and then expect #each to be
# 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|
# # name is a unique identifier per Processor instance
# 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
class Workers
@ -390,9 +391,12 @@ module Sidekiq
Sidekiq.redis do |conn|
workers = conn.smembers("workers")
workers.each do |w|
msg = conn.get("worker:#{w}")
msg, time = conn.multi do
conn.get("worker:#{w}")
conn.get("worker:#{w}:started")
end
next unless msg
block.call(w, Sidekiq.load_json(msg))
block.call(w, Sidekiq.load_json(msg), time)
end
end
end

View file

@ -310,13 +310,15 @@ class TestApi < Minitest::Test
c.multi do
c.sadd('workers', s)
c.set("worker:#{s}", data)
c.set("worker:#{s}:started", Time.now.to_s)
end
end
assert_equal 1, w.size
w.each do |x, y|
w.each do |x, y, z|
assert_equal s, x
assert_equal 'default', y['queue']
assert_equal Time.now.year, DateTime.parse(z).year
end
end