Class: Chore::Strategy::SingleConsumerStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/chore/strategies/consumer/single_consumer_strategy.rb

Overview

Consumer strategy for requesting batches of work in a linear fashion. Ideally used for running a single Chore job locally in a development environment where performance or throughput may not matter. SingleConsumerStrategy will raise an exception if you're configured to listen for more than 1 queue

Instance Method Summary collapse

Constructor Details

#initialize(fetcher, opts = {}) ⇒ SingleConsumerStrategy

Returns a new instance of SingleConsumerStrategy.



8
9
10
# File 'lib/chore/strategies/consumer/single_consumer_strategy.rb', line 8

def initialize(fetcher, opts={})
  @fetcher = fetcher
end

Instance Method Details

#fetchObject

Begins fetching from the configured queue by way of the configured Consumer. This can only be used if you have a single queue which can be kept up with at a relatively low volume. If you have more than a single queue configured, it will raise an exception.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chore/strategies/consumer/single_consumer_strategy.rb', line 15

def fetch
  Chore.logger.debug "Starting up consumer strategy: #{self.class.name}"
  queues = Chore.config.queues
  raise "When using SingleConsumerStrategy only one queue can be defined. Queues: #{queues}" unless queues.size == 1

  @consumer = Chore.config.consumer.new(queues.first)
  @consumer.consume do |message_id, message_receipt_handle, queue_name, queue_timeout, body, previous_attempts|
    work = UnitOfWork.new(message_id, message_receipt_handle, queue_name, queue_timeout, body, previous_attempts, @consumer)
    @fetcher.manager.assign(work)
  end
end

#stop!Object

Stops consuming messages from the queue



28
29
30
31
# File 'lib/chore/strategies/consumer/single_consumer_strategy.rb', line 28

def stop!
  Chore.logger.info "Shutting down fetcher: #{self.class.name.to_s}"
  @consumer.stop
end