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

102 lines
2.7 KiB
Ruby
Raw Normal View History

require 'celluloid'
require 'sidekiq/util'
require 'sidekiq/middleware/server/active_record'
require 'sidekiq/middleware/server/exception_handler'
require 'sidekiq/middleware/server/retry_jobs'
2012-02-25 16:43:53 -05:00
require 'sidekiq/middleware/server/logging'
require 'sidekiq/middleware/server/timeout'
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
include Util
include Celluloid
def self.default_middleware
Middleware::Chain.new do |m|
m.add Middleware::Server::ExceptionHandler
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
m.add Middleware::Server::Timeout
end
end
def initialize(boss)
@boss = boss
end
def process(msgstr, queue)
2012-06-13 00:55:06 -04:00
# Celluloid actor calls are performed within a Fiber.
# This would give us a terribly small 4KB stack on MRI
# so we use Celluloid's defer to run things in a thread pool
# in order to get a full-sized stack for the Worker.
defer do
msg = Sidekiq.load_json(msgstr)
klass = constantize(msg['class'])
worker = klass.new
stats(worker, msg, queue) do
Sidekiq.server_middleware.invoke(worker, msg, queue) do
worker.perform(*msg['args'])
end
end
end
@boss.processor_done!(current_actor)
end
# See http://github.com/tarcieri/celluloid/issues/22
def inspect
"#<Processor #{to_s}>"
end
def to_s
@str ||= "#{hostname}:#{process_id}-#{Thread.current.object_id}:default"
end
private
def stats(worker, msg, queue)
redis do |conn|
conn.multi do
conn.sadd('workers', self)
2012-05-12 16:32:16 -04:00
conn.setex("worker:#{self}:started", EXPIRY, Time.now.to_s)
hash = {:queue => queue, :payload => msg, :run_at => Time.now.strftime("%Y/%m/%d %H:%M:%S %Z")}
2012-05-12 16:32:16 -04:00
conn.setex("worker:#{self}", EXPIRY, Sidekiq.dump_json(hash))
end
end
dying = false
begin
yield
2012-05-12 16:23:23 -04:00
rescue Exception
dying = true
redis do |conn|
conn.multi do
conn.incrby("stat:failed", 1)
end
end
raise
ensure
redis do |conn|
conn.multi do
conn.srem("workers", self)
conn.del("worker:#{self}")
conn.del("worker:#{self}:started")
conn.incrby("stat:processed", 1)
end
end
end
end
def hostname
@h ||= `hostname`.strip
end
end
end