-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move queue pausing responsibility to PollingStrategy
- Loading branch information
1 parent
314038d
commit ef73e17
Showing
7 changed files
with
200 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,79 @@ | ||
module Shoryuken | ||
class Fetcher | ||
include Celluloid | ||
include Util | ||
module Shoryuken | ||
class Fetcher | ||
include Celluloid | ||
include Util | ||
|
||
FETCH_LIMIT = 10 | ||
FETCH_LIMIT = 10 | ||
|
||
def initialize(manager) | ||
@manager = manager | ||
end | ||
def initialize(manager, polling_strategy) | ||
@manager = manager | ||
@polling_strategy = polling_strategy | ||
@delay = Shoryuken.options[:delay].to_f | ||
end | ||
|
||
def fetch(queue, available_processors) | ||
watchdog('Fetcher#fetch died') do | ||
started_at = Time.now | ||
def fetch(queue, available_processors) | ||
watchdog('Fetcher#fetch died') do | ||
started_at = Time.now | ||
|
||
logger.debug { "Looking for new messages in '#{queue}'" } | ||
logger.debug { "Looking for new messages in '#{queue}'" } | ||
|
||
begin | ||
batch = Shoryuken.worker_registry.batch_receive_messages?(queue.name) | ||
limit = batch ? FETCH_LIMIT : available_processors | ||
begin | ||
batch = Shoryuken.worker_registry.batch_receive_messages?(queue.name) | ||
limit = batch ? FETCH_LIMIT : available_processors | ||
|
||
if (sqs_msgs = Array(receive_messages(queue, limit))).any? | ||
logger.debug { "Found #{sqs_msgs.size} messages for '#{queue}'" } | ||
sqs_msgs = Array(receive_messages(queue, limit)) | ||
logger.info { "Found #{sqs_msgs.size} messages for '#{queue}'" } | ||
|
||
if batch | ||
@manager.async.assign(queue.name, patch_sqs_msgs!(sqs_msgs)) | ||
else | ||
sqs_msgs.each { |sqs_msg| @manager.async.assign(queue.name, sqs_msg) } | ||
end | ||
|
||
@manager.async.messages_present(queue) | ||
else | ||
logger.debug { "No message found for '#{queue}'" } | ||
@polling_strategy.messages_found(queue, sqs_msgs.size) | ||
|
||
@manager.async.queue_empty(queue) | ||
logger.debug { "Fetcher for '#{queue}' completed in #{elapsed(started_at)} ms" } | ||
rescue => ex | ||
logger.error { "Error fetching message: #{ex}" } | ||
logger.error { ex.backtrace.first } | ||
end | ||
|
||
logger.debug { "Fetcher for '#{queue}' completed in #{elapsed(started_at)} ms" } | ||
rescue => ex | ||
logger.error { "Error fetching message: #{ex}" } | ||
logger.error { ex.backtrace.first } | ||
@manager.async.dispatch | ||
end | ||
end | ||
|
||
@manager.async.dispatch | ||
def next_queue(*args) | ||
@polling_strategy.next_queue(*args) | ||
end | ||
end | ||
|
||
private | ||
def active_queues(*args) | ||
@polling_strategy.active_queues(*args) | ||
end | ||
|
||
def receive_messages(queue, limit) | ||
# AWS limits the batch size by 10 | ||
limit = limit > FETCH_LIMIT ? FETCH_LIMIT : limit | ||
private | ||
|
||
options = (Shoryuken.options[:aws][:receive_message] || {}).dup | ||
options[:max_number_of_messages] = limit | ||
options[:message_attribute_names] = %w(All) | ||
options[:attribute_names] = %w(All) | ||
def receive_messages(queue, limit) | ||
# AWS limits the batch size by 10 | ||
limit = limit > FETCH_LIMIT ? FETCH_LIMIT : limit | ||
|
||
options.merge!(queue.options) | ||
options = (Shoryuken.options[:aws][:receive_message] || {}).dup | ||
options[:max_number_of_messages] = limit | ||
options[:message_attribute_names] = %w(All) | ||
options[:attribute_names] = %w(All) | ||
|
||
Shoryuken::Client.queues(queue.name).receive_messages options | ||
end | ||
options.merge!(queue.options) | ||
|
||
def patch_sqs_msgs!(sqs_msgs) | ||
sqs_msgs.instance_eval do | ||
def message_id | ||
"batch-with-#{size}-messages" | ||
end | ||
Shoryuken::Client.queues(queue.name).receive_messages options | ||
end | ||
|
||
sqs_msgs | ||
def patch_sqs_msgs!(sqs_msgs) | ||
sqs_msgs.instance_eval do | ||
def message_id | ||
"batch-with-#{size}-messages" | ||
end | ||
end | ||
|
||
sqs_msgs | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.