2012-01-22 19:01:46 -05:00
|
|
|
require 'celluloid'
|
|
|
|
require 'redis'
|
|
|
|
require 'multi_json'
|
|
|
|
|
2012-01-23 17:05:03 -05:00
|
|
|
require 'sidekiq/util'
|
2012-01-22 19:01:46 -05:00
|
|
|
require 'sidekiq/worker'
|
|
|
|
|
2012-01-16 19:14:47 -05:00
|
|
|
module Sidekiq
|
|
|
|
|
|
|
|
##
|
2012-01-22 14:32:38 -05:00
|
|
|
# This is the main router in the system. This
|
|
|
|
# manages the worker state and fetches messages
|
|
|
|
# from Redis to be dispatched to ready workers.
|
2012-01-16 19:14:47 -05:00
|
|
|
#
|
|
|
|
class Server
|
2012-01-22 14:32:38 -05:00
|
|
|
include Util
|
2012-01-16 23:02:58 -05:00
|
|
|
include Celluloid
|
2012-01-16 19:18:36 -05:00
|
|
|
|
2012-01-22 14:32:38 -05:00
|
|
|
trap_exit :worker_died
|
|
|
|
|
2012-01-16 23:02:58 -05:00
|
|
|
def initialize(location, options={})
|
2012-01-22 19:01:46 -05:00
|
|
|
log "Starting sidekiq #{Sidekiq::VERSION} with Redis at #{location}"
|
|
|
|
verbose options.inspect
|
2012-01-22 14:32:38 -05:00
|
|
|
@count = options[:worker_count]
|
|
|
|
@queues = options[:queues]
|
|
|
|
@queue_idx = 0
|
|
|
|
@queues_size = @queues.size
|
2012-01-23 17:05:03 -05:00
|
|
|
@redis = Redis.connect(:url => location)
|
2012-01-22 14:32:38 -05:00
|
|
|
|
2012-01-22 19:01:46 -05:00
|
|
|
@done = false
|
|
|
|
@busy = []
|
|
|
|
@ready = []
|
|
|
|
@count.times do
|
|
|
|
@ready << Worker.new_link
|
|
|
|
end
|
2012-01-22 14:32:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
@done = true
|
|
|
|
@ready.each(&:terminate)
|
|
|
|
@ready.clear
|
|
|
|
|
2012-01-23 17:05:03 -05:00
|
|
|
after(5) do
|
|
|
|
signal(:shutdown)
|
2012-01-22 14:32:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2012-01-22 19:01:46 -05:00
|
|
|
dispatch
|
2012-01-22 14:32:38 -05:00
|
|
|
end
|
2012-01-16 19:18:36 -05:00
|
|
|
|
2012-01-22 14:32:38 -05:00
|
|
|
def worker_done(worker)
|
2012-01-22 19:01:46 -05:00
|
|
|
@busy.delete(worker)
|
2012-01-22 14:32:38 -05:00
|
|
|
if stopped?
|
|
|
|
worker.terminate
|
|
|
|
else
|
|
|
|
@ready << worker
|
2012-01-16 23:02:58 -05:00
|
|
|
end
|
2012-01-22 14:32:38 -05:00
|
|
|
dispatch
|
2012-01-16 19:18:36 -05:00
|
|
|
end
|
2012-01-16 23:02:58 -05:00
|
|
|
|
2012-01-22 14:32:38 -05:00
|
|
|
def worker_died(worker, reason)
|
2012-01-22 19:01:46 -05:00
|
|
|
@busy.delete(worker)
|
2012-01-23 17:05:03 -05:00
|
|
|
|
|
|
|
if reason
|
|
|
|
log "Worker death: #{reason}"
|
|
|
|
log reason.backtrace.join("\n")
|
|
|
|
end
|
2012-01-22 19:01:46 -05:00
|
|
|
|
|
|
|
unless stopped?
|
2012-01-23 17:05:03 -05:00
|
|
|
@ready << Worker.new_link
|
2012-01-22 19:01:46 -05:00
|
|
|
dispatch
|
|
|
|
end
|
2012-01-22 14:32:38 -05:00
|
|
|
end
|
|
|
|
|
2012-01-22 19:01:46 -05:00
|
|
|
private
|
|
|
|
|
2012-01-22 14:32:38 -05:00
|
|
|
def dispatch
|
|
|
|
watchdog("Fatal error in sidekiq, dispatch loop died") do
|
|
|
|
return if stopped?
|
|
|
|
|
|
|
|
# Our dispatch loop
|
|
|
|
queue_idx = 0
|
|
|
|
none_found = true
|
|
|
|
loop do
|
2012-01-22 19:01:46 -05:00
|
|
|
# return so that we don't dispatch again until worker_done
|
|
|
|
return if @ready.size == 0
|
|
|
|
|
|
|
|
current_queue = @queues[queue_idx]
|
|
|
|
msg = @redis.lpop("queue:#{current_queue}")
|
|
|
|
if msg
|
|
|
|
worker = @ready.pop
|
|
|
|
@busy << worker
|
|
|
|
worker.process! MultiJson.decode(msg)
|
|
|
|
none_found = false
|
|
|
|
end
|
2012-01-22 14:32:38 -05:00
|
|
|
|
|
|
|
queue_idx += 1
|
|
|
|
|
2012-01-22 19:01:46 -05:00
|
|
|
# Loop through the queues, looking for a message in each.
|
2012-01-22 14:32:38 -05:00
|
|
|
# if we find no messages in any of the queues, we can break
|
|
|
|
# out of the loop. Otherwise we loop again.
|
2012-01-22 19:01:46 -05:00
|
|
|
lastq = (queue_idx % @queues.size == 0)
|
|
|
|
if lastq && none_found
|
2012-01-22 14:32:38 -05:00
|
|
|
break
|
2012-01-22 19:01:46 -05:00
|
|
|
elsif lastq
|
2012-01-22 14:32:38 -05:00
|
|
|
queue_idx = 0
|
|
|
|
none_found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after(1) { dispatch }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stopped?
|
|
|
|
@done
|
|
|
|
end
|
2012-01-16 19:14:47 -05:00
|
|
|
end
|
|
|
|
end
|