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

Refactor ProcessSet to yield a Sidekiq::Process

This commit is contained in:
Mike Perham 2014-05-13 20:33:20 -07:00
parent 4953799f8e
commit f81f09dee4
4 changed files with 31 additions and 16 deletions

View file

@ -449,18 +449,8 @@ module Sidekiq
# right now. Each process send a heartbeat to Redis every 5 seconds
# so this set should be relatively accurate, barring network partitions.
#
# Yields a hash of data which looks something like this:
# Yields a Sidekiq::Process.
#
# {
# 'hostname' => 'app-1.example.com',
# 'started_at' => <process start time>,
# 'pid' => 12345,
# 'tag' => 'myapp'
# 'concurrency' => 25,
# 'queues' => ['default', 'low'],
# 'busy' => 10,
# 'beat' => <last heartbeat>,
# }
class ProcessSet
include Enumerable
@ -486,7 +476,7 @@ module Sidekiq
# in to Redis and probably died.
(to_prune << sorted[i]; next) if info.nil?
hash = Sidekiq.load_json(info)
yield hash.merge('busy' => busy.to_i, 'beat' => at_s.to_f)
yield Process.new(hash.merge('busy' => busy.to_i, 'beat' => at_s.to_f))
end
end
@ -503,6 +493,29 @@ module Sidekiq
end
end
#
# Sidekiq::Process has a set of attributes which look like this:
#
# {
# 'hostname' => 'app-1.example.com',
# 'started_at' => <process start time>,
# 'pid' => 12345,
# 'tag' => 'myapp'
# 'concurrency' => 25,
# 'queues' => ['default', 'low'],
# 'busy' => 10,
# 'beat' => <last heartbeat>,
# }
class Process
def initialize(hash)
@attribs = hash
end
def [](key)
@attribs[key]
end
end
##
# Programmatic access to the current active worker set.
#

View file

@ -158,7 +158,7 @@ module Sidekiq
files_to_reopen << file unless file.closed?
end
Process.daemon(true, true)
::Process.daemon(true, true)
files_to_reopen.each do |file|
begin
@ -323,7 +323,7 @@ module Sidekiq
if path = options[:pidfile]
pidfile = File.expand_path(path)
File.open(pidfile, 'w') do |f|
f.puts Process.pid
f.puts ::Process.pid
end
end
end

View file

@ -7,7 +7,7 @@ module Sidekiq
class Pretty < Logger::Formatter
# Provide a call() method that returns the formatted message.
def call(severity, time, program_name, message)
"#{time.utc.iso8601} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
"#{time.utc.iso8601} #{::Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
end
def context

View file

@ -356,7 +356,9 @@ class TestApi < Sidekiq::Test
ps = Sidekiq::ProcessSet.new.to_a
assert_equal 1, ps.size
data = ps.first
assert_equal odata.merge('busy' => 10, 'beat' => time), data
assert_equal 10, data['busy']
assert_equal time, data['beat']
assert_equal 123, data['pid']
end
it 'can enumerate workers' do