Class: Shoryuken::Fetcher

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Util
Defined in:
lib/shoryuken/fetcher.rb

Constant Summary collapse

FETCH_LIMIT =
10

Instance Method Summary collapse

Methods included from Util

#elapsed, #fire_event, #logger, #unparse_queues, #watchdog, #worker_name

Constructor Details

#initialize(manager) ⇒ Fetcher

Returns a new instance of Fetcher


8
9
10
# File 'lib/shoryuken/fetcher.rb', line 8

def initialize(manager)
  @manager = manager
end

Instance Method Details

#fetch(queue, available_processors) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shoryuken/fetcher.rb', line 24

def fetch(queue, available_processors)
  watchdog('Fetcher#fetch died') do
    started_at = Time.now

    logger.debug { "Looking for new messages in '#{queue}'" }

    begin
      batch = Shoryuken.worker_registry.batch_receive_messages?(queue)
      limit = batch ? FETCH_LIMIT : available_processors

      if (sqs_msgs = Array(receive_messages(queue, limit))).any?
        logger.info { "Found #{sqs_msgs.size} messages for '#{queue}'" }

        if batch
          @manager.async.assign(queue, patch_sqs_msgs!(sqs_msgs))
        else
          sqs_msgs.each { |sqs_msg| @manager.async.assign(queue, sqs_msg) }
        end

        @manager.async.rebalance_queue_weight!(queue)
      else
        logger.debug { "No message found for '#{queue}'" }

        @manager.async.pause_queue!(queue)
      end

      @manager.async.dispatch

      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

end

#receive_messages(queue, limit) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shoryuken/fetcher.rb', line 12

def receive_messages(queue, limit)
  # AWS limits the batch size by 10
  limit = limit > FETCH_LIMIT ? FETCH_LIMIT : limit

  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).receive_messages options
end