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

141 lines
3.8 KiB
Ruby
Raw Normal View History

require 'sidekiq/util'
require 'sidekiq/actor'
require 'sidekiq/middleware/server/active_record'
require 'sidekiq/middleware/server/retry_jobs'
2012-02-25 16:43:53 -05:00
require 'sidekiq/middleware/server/logging'
module Sidekiq
2012-06-13 00:55:06 -04:00
##
# The Processor receives a message from the Manager and actually
# processes it. It instantiates the worker, runs the middleware
# chain and then calls Sidekiq::Worker#perform.
class Processor
STATS_TIMEOUT = 180 * 24 * 60 * 60
include Util
include Actor
def self.default_middleware
Middleware::Chain.new do |m|
2012-02-25 16:43:53 -05:00
m.add Middleware::Server::Logging
2012-03-18 02:04:31 -04:00
m.add Middleware::Server::RetryJobs
m.add Middleware::Server::ActiveRecord
end
end
attr_accessor :proxy_id
def initialize(boss)
@boss = boss
end
def process(work)
msgstr = work.message
queue = work.queue_name
do_defer do
@boss.async.real_thread(proxy_id, Thread.current)
begin
msg = Sidekiq.load_json(msgstr)
klass = msg['class'].constantize
worker = klass.new
worker.jid = msg['jid']
stats(worker, msg, queue) do
Sidekiq.server_middleware.invoke(worker, msg, queue) do
worker.perform(*cloned(msg['args']))
end
2012-08-16 21:12:25 -04:00
end
rescue Sidekiq::Shutdown
# Had to force kill this job because it didn't finish
# within the timeout.
rescue Exception => ex
handle_exception(ex, msg || { :message => msgstr })
raise
ensure
work.acknowledge
end
end
@boss.async.processor_done(current_actor)
end
def inspect
"<Processor##{object_id.to_s(16)}>"
end
2013-03-27 01:55:07 -04:00
private
# We use Celluloid's defer to workaround tiny little
# Fiber stacks (4kb!) in MRI 1.9.
#
# For some reason, Celluloid's thread dispatch, TaskThread,
# is unstable under heavy concurrency but TaskFiber has proven
# itself stable.
NEED_DEFER = (RUBY_ENGINE == 'ruby' && RUBY_VERSION < '2.0.0')
def do_defer(&block)
if NEED_DEFER
defer(&block)
else
yield
end
end
2013-03-27 01:55:07 -04:00
def identity
@str ||= "#{hostname}:#{process_id}-#{Thread.current.object_id}:default"
end
def stats(worker, msg, queue)
redis do |conn|
conn.multi do
2013-03-27 01:55:07 -04:00
conn.sadd('workers', identity)
conn.setex("worker:#{identity}:started", EXPIRY, Time.now.to_s)
2012-09-16 10:27:49 -04:00
hash = {:queue => queue, :payload => msg, :run_at => Time.now.to_i }
2013-03-27 01:55:07 -04:00
conn.setex("worker:#{identity}", EXPIRY, Sidekiq.dump_json(hash))
end
end
begin
yield
2012-05-12 16:23:23 -04:00
rescue Exception
redis do |conn|
failed = "stat:failed:#{Time.now.utc.to_date}"
result = conn.multi do
conn.incrby("stat:failed", 1)
conn.incrby(failed, 1)
end
conn.expire(failed, STATS_TIMEOUT) if result.last == 1
end
raise
ensure
redis do |conn|
processed = "stat:processed:#{Time.now.utc.to_date}"
result = conn.multi do
2013-03-27 01:55:07 -04:00
conn.srem("workers", identity)
conn.del("worker:#{identity}")
conn.del("worker:#{identity}:started")
conn.incrby("stat:processed", 1)
conn.incrby(processed, 1)
end
conn.expire(processed, STATS_TIMEOUT) if result.last == 1
end
end
end
2012-08-05 23:53:59 -04:00
# Singleton classes are not clonable.
SINGLETON_CLASSES = [ NilClass, TrueClass, FalseClass, Symbol, Fixnum, Float, Bignum ].freeze
2012-08-05 23:53:59 -04:00
# Clone the arguments passed to the worker so that if
# the message fails, what is pushed back onto Redis hasn't
# been mutated by the worker.
def cloned(ary)
ary.map do |val|
SINGLETON_CLASSES.include?(val.class) ? val : val.clone
end
end
end
end