2012-01-26 12:45:04 -08:00
|
|
|
require 'celluloid'
|
2012-02-08 17:04:02 -08:00
|
|
|
require 'sidekiq/util'
|
|
|
|
|
2012-02-19 13:02:32 -08:00
|
|
|
require 'sidekiq/middleware/server/active_record'
|
2012-03-17 13:41:53 -07:00
|
|
|
require 'sidekiq/middleware/server/retry_jobs'
|
2012-02-25 13:43:53 -08:00
|
|
|
require 'sidekiq/middleware/server/logging'
|
2012-04-26 08:40:07 -07:00
|
|
|
require 'sidekiq/middleware/server/timeout'
|
2012-02-19 13:02:32 -08:00
|
|
|
|
2012-01-25 13:32:51 -08:00
|
|
|
module Sidekiq
|
2012-06-12 21:55:06 -07: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.
|
2012-01-25 13:32:51 -08:00
|
|
|
class Processor
|
2012-01-26 12:45:04 -08:00
|
|
|
include Util
|
2012-01-29 14:35:16 -08:00
|
|
|
include Celluloid
|
2012-01-25 13:32:51 -08:00
|
|
|
|
2012-02-19 13:02:32 -08:00
|
|
|
def self.default_middleware
|
|
|
|
Middleware::Chain.new do |m|
|
2012-02-25 13:43:53 -08:00
|
|
|
m.add Middleware::Server::Logging
|
2012-03-17 23:04:31 -07:00
|
|
|
m.add Middleware::Server::RetryJobs
|
2012-02-19 13:02:32 -08:00
|
|
|
m.add Middleware::Server::ActiveRecord
|
2012-04-26 08:40:07 -07:00
|
|
|
m.add Middleware::Server::Timeout
|
2012-02-19 13:02:32 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-26 22:56:49 -07:00
|
|
|
# store the actual working thread so we
|
|
|
|
# can later kill if it necessary during
|
|
|
|
# hard shutdown.
|
|
|
|
attr_accessor :actual_work_thread
|
|
|
|
|
2012-02-08 17:04:02 -08:00
|
|
|
def initialize(boss)
|
|
|
|
@boss = boss
|
2012-02-04 16:53:09 -08:00
|
|
|
end
|
|
|
|
|
2013-01-05 21:17:08 -08:00
|
|
|
def process(work)
|
|
|
|
msgstr = work.message
|
|
|
|
queue = work.queue_name
|
2012-09-13 09:16:24 -07:00
|
|
|
defer do
|
2013-03-26 22:56:49 -07:00
|
|
|
@actual_work_thread = Thread.current
|
2012-09-13 09:16:24 -07:00
|
|
|
begin
|
|
|
|
msg = Sidekiq.load_json(msgstr)
|
2012-10-27 12:48:34 -07:00
|
|
|
klass = msg['class'].constantize
|
2012-09-13 09:16:24 -07:00
|
|
|
worker = klass.new
|
2012-11-03 20:00:06 -07:00
|
|
|
worker.jid = msg['jid']
|
2012-06-29 20:37:45 -07:00
|
|
|
|
2012-09-13 09:16:24 -07:00
|
|
|
stats(worker, msg, queue) do
|
|
|
|
Sidekiq.server_middleware.invoke(worker, msg, queue) do
|
|
|
|
worker.perform(*cloned(msg['args']))
|
|
|
|
end
|
2012-08-16 18:12:25 -07:00
|
|
|
end
|
2013-03-26 22:56:49 -07:00
|
|
|
rescue Sidekiq::Shutdown
|
|
|
|
# Had to force kill this job because it didn't finish
|
|
|
|
# within the timeout.
|
2012-12-19 09:38:47 -08:00
|
|
|
rescue Exception => ex
|
|
|
|
handle_exception(ex, msg || { :message => msgstr })
|
2012-09-13 09:16:24 -07:00
|
|
|
raise
|
2013-01-05 21:17:08 -08:00
|
|
|
ensure
|
|
|
|
work.acknowledge
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
2012-02-04 16:53:09 -08:00
|
|
|
end
|
2012-11-03 19:56:06 -07:00
|
|
|
@boss.async.processor_done(current_actor)
|
2012-01-25 13:32:51 -08:00
|
|
|
end
|
2012-01-29 14:35:16 -08:00
|
|
|
|
|
|
|
# See http://github.com/tarcieri/celluloid/issues/22
|
|
|
|
def inspect
|
2012-02-10 23:16:12 -08:00
|
|
|
"#<Processor #{to_s}>"
|
|
|
|
end
|
|
|
|
|
2013-03-26 22:55:07 -07:00
|
|
|
private
|
|
|
|
|
|
|
|
def identity
|
2012-02-15 12:30:31 -08:00
|
|
|
@str ||= "#{hostname}:#{process_id}-#{Thread.current.object_id}:default"
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def stats(worker, msg, queue)
|
2012-03-14 09:56:13 -07:00
|
|
|
redis do |conn|
|
2012-02-11 13:14:03 -08:00
|
|
|
conn.multi do
|
2013-03-26 22:55:07 -07:00
|
|
|
conn.sadd('workers', identity)
|
|
|
|
conn.setex("worker:#{identity}:started", EXPIRY, Time.now.to_s)
|
2012-09-16 07:27:49 -07:00
|
|
|
hash = {:queue => queue, :payload => msg, :run_at => Time.now.to_i }
|
2013-03-26 22:55:07 -07:00
|
|
|
conn.setex("worker:#{identity}", EXPIRY, Sidekiq.dump_json(hash))
|
2012-02-11 13:14:03 -08:00
|
|
|
end
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield
|
2012-05-12 13:23:23 -07:00
|
|
|
rescue Exception
|
2012-03-14 09:56:13 -07:00
|
|
|
redis do |conn|
|
2012-02-11 13:14:03 -08:00
|
|
|
conn.multi do
|
|
|
|
conn.incrby("stat:failed", 1)
|
2012-12-05 20:35:49 -05:00
|
|
|
conn.incrby("stat:failed:#{Time.now.utc.to_date}", 1)
|
2012-02-11 13:14:03 -08:00
|
|
|
end
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
|
|
|
raise
|
|
|
|
ensure
|
2012-03-14 09:56:13 -07:00
|
|
|
redis do |conn|
|
2012-02-11 13:14:03 -08:00
|
|
|
conn.multi do
|
2013-03-26 22:55:07 -07:00
|
|
|
conn.srem("workers", identity)
|
|
|
|
conn.del("worker:#{identity}")
|
|
|
|
conn.del("worker:#{identity}:started")
|
2012-02-11 13:14:03 -08:00
|
|
|
conn.incrby("stat:processed", 1)
|
2012-12-05 20:35:49 -05:00
|
|
|
conn.incrby("stat:processed:#{Time.now.utc.to_date}", 1)
|
2012-02-11 13:14:03 -08:00
|
|
|
end
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
|
|
|
end
|
2012-08-04 12:11:46 -07:00
|
|
|
end
|
2012-02-10 23:16:12 -08:00
|
|
|
|
2012-08-05 20:53:59 -07:00
|
|
|
# Singleton classes are not clonable.
|
2012-10-18 09:28:51 -05:00
|
|
|
SINGLETON_CLASSES = [ NilClass, TrueClass, FalseClass, Symbol, Fixnum, Float, Bignum ].freeze
|
2012-08-05 20:53:59 -07:00
|
|
|
|
2012-08-04 12:11:46 -07: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
|
2012-02-10 23:16:12 -08:00
|
|
|
end
|
2012-01-25 13:32:51 -08:00
|
|
|
end
|
|
|
|
end
|