Class: EventQ::Amazon::QueueWorker

Inherits:
Object
  • Object
show all
Includes:
WorkerId
Defined in:
lib/eventq/eventq_aws/aws_queue_worker.rb

Constant Summary collapse

APPROXIMATE_RECEIVE_COUNT =
'ApproximateReceiveCount'
MESSAGE =
'Message'
AWS_MAX_VISIBILITY_TIMEOUT =

12h

43_200

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WorkerId

#tag_processing_thread, #untag_processing_thread

Constructor Details

#initializeQueueWorker

Returns a new instance of QueueWorker.



17
18
19
20
21
22
23
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 17

def initialize
  @serialization_provider_manager = EventQ::SerializationProviders::Manager.new
  @signature_provider_manager = EventQ::SignatureProviders::Manager.new
  @calculate_visibility_timeout = Amazon::CalculateVisibilityTimeout.new(
    max_timeout: AWS_MAX_VISIBILITY_TIMEOUT
  )
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



15
16
17
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 15

def context
  @context
end

Instance Method Details

#acknowledge_message(poller, msg) ⇒ Object

Logic for the RabbitMq adapter when a message is accepted



77
78
79
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 77

def acknowledge_message(poller, msg)
  poller.delete_message(msg)
end

#configure(options = {}) ⇒ Object



70
71
72
73
74
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 70

def configure(options = {})
  options[:queue_poll_wait] ||= 10

  EventQ.logger.info("[#{self.class}] - Configuring. Queue Poll Wait: #{options[:queue_poll_wait]}")
end

#deserialize_message(payload) ⇒ Object



60
61
62
63
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 60

def deserialize_message(payload)
  provider = @serialization_provider_manager.get_provider(EventQ::Configuration.serialization_provider)
  provider.deserialize(payload)
end

#pre_process(context, options) ⇒ Object



25
26
27
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 25

def pre_process(context, options)
  # don't do anything specific to set up the process before threads are fired off.
end

#serialize_message(msg) ⇒ Object



65
66
67
68
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 65

def serialize_message(msg)
  provider = @serialization_provider_manager.get_provider(EventQ::Configuration.serialization_provider)
  provider.serialize(msg)
end

#thread_process_iteration(queue, options, block) ⇒ Object



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
# File 'lib/eventq/eventq_aws/aws_queue_worker.rb', line 29

def thread_process_iteration(queue, options, block)
  client = options[:client]
  manager = options[:manager] || EventQ::Amazon::QueueManager.new({ client: client })

  # get the queue
  queue_url = manager.get_queue(queue)
  poller = Aws::SQS::QueuePoller.new(queue_url, attribute_names: [APPROXIMATE_RECEIVE_COUNT])

  # Polling will block indefinitely unless we force it to stop
  poller.before_request do |stats|
    unless context.running?
      EventQ.logger.info("AWS Poller shutting down")
      throw :stop_polling
    end
  end

  poller.poll(skip_delete: true, wait_time_seconds: options[:queue_poll_wait]) do |msg, stats|
    begin
      tag_processing_thread
      process_message(msg, poller, queue, block)
    rescue => e
      EventQ.logger.error do
        "[#{self.class}] - An unhandled error occurred. Error: #{e} | Backtrace: #{e.backtrace}"
      end
      context.call_on_error_block(error: e)
    ensure
      untag_processing_thread
    end
  end
end