Class: EventQ::Amazon::QueueWorker

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

Constant Summary collapse

APPROXIMATE_RECEIVE_COUNT =
'ApproximateReceiveCount'.freeze
MESSAGE =
'Message'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueueWorker

Returns a new instance of QueueWorker.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/eventq_aws/aws_queue_worker.rb', line 11

def initialize
  @threads = []
  @forks = []
  @is_running = false

  @retry_exceeded_block = nil

  @hash_helper = HashKit::Helper.new
  @serialization_provider_manager = EventQ::SerializationProviders::Manager.new

  @last_gc_flush = Time.now
  @gc_flush_interval = 10

  @queue_poll_wait = 10
end

Instance Attribute Details

#is_runningObject

Returns the value of attribute is_running.



9
10
11
# File 'lib/eventq_aws/aws_queue_worker.rb', line 9

def is_running
  @is_running
end

Instance Method Details

#deserialize_message(payload) ⇒ Object



175
176
177
178
# File 'lib/eventq_aws/aws_queue_worker.rb', line 175

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

#gc_flushObject



114
115
116
117
118
119
# File 'lib/eventq_aws/aws_queue_worker.rb', line 114

def gc_flush
  if Time.now - last_gc_flush > @gc_flush_interval
    GC.start
    @last_gc_flush = Time.now
  end
end

#last_gc_flushObject



121
122
123
# File 'lib/eventq_aws/aws_queue_worker.rb', line 121

def last_gc_flush
  @last_gc_flush
end

#on_retry_exceeded(&block) ⇒ Object



167
168
169
# File 'lib/eventq_aws/aws_queue_worker.rb', line 167

def on_retry_exceeded(&block)
  @retry_exceeded_block = block
end

#running?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/eventq_aws/aws_queue_worker.rb', line 171

def running?
  return @is_running
end

#start(queue, options = {}, &block) ⇒ Object



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

def start(queue, options = {}, &block)

  EventQ.log(:info, "[#{self.class}] - Preparing to start listening for messages.")

  configure(queue, options)

  if options[:client] == nil
    raise "[#{self.class}] - :client (QueueClient) must be specified."
  end

  raise "[#{self.class}] - Worker is already running." if running?

  EventQ.log(:info, "[#{self.class}] - Listening for messages.")

  @forks = []

  if @fork_count > 1
    @fork_count.times do
      pid = fork do
        start_process(options, queue, block)
      end
      @forks.push(pid)
    end

    if options.key?(:wait) && options[:wait] == true
      @forks.each { |pid| Process.wait(pid) }
    end

  else
    start_process(options, queue, block)
  end

  return true
end

#start_process(options, queue, block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/eventq_aws/aws_queue_worker.rb', line 62

def start_process(options, queue, block)

  %w'INT TERM'.each do |sig|
    Signal.trap(sig) {
      stop
      exit
    }
  end

  @is_running = true
  @threads = []

  #loop through each thread count
  @thread_count.times do
    thr = Thread.new do

      client = options[:client]
      manager = EventQ::Amazon::QueueManager.new({ client: client })

      #begin the queue loop for this thread
      while true do

        #check if the worker is still allowed to run and break out of thread loop if not
        if !@is_running
          break
        end

        has_message_received = thread_process_iteration(client, manager, queue, block)

        gc_flush

        if !has_message_received
          EventQ.log(:debug, "[#{self.class}] - No message received.")
          if @sleep > 0
            EventQ.log(:debug, "[#{self.class}] - Sleeping for #{@sleep} seconds")
            sleep(@sleep)
          end
        end

      end

    end
    @threads.push(thr)

  end

  if options.key?(:wait) && options[:wait] == true
    @threads.each { |thr| thr.join }
  end

end

#stopObject



160
161
162
163
164
165
# File 'lib/eventq_aws/aws_queue_worker.rb', line 160

def stop
  EventQ.log(:info, "[#{self.class}] - Stopping.")
  @is_running = false
  @threads.each { |thr| thr.join }
  return true
end

#thread_process_iteration(client, manager, queue, block) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/eventq_aws/aws_queue_worker.rb', line 125

def thread_process_iteration(client, manager, queue, block)
  #get the queue
  q = manager.get_queue(queue)

  received = false

  begin

    #request a message from the queue
    response = client.sqs.receive_message({
                                              queue_url: q,
                                              max_number_of_messages: 1,
                                              wait_time_seconds: @queue_poll_wait,
                                              attribute_names: [APPROXIMATE_RECEIVE_COUNT]
                                          })

    #check that a message was received
    if response.messages.length > 0
      received = true
      begin
        tag_processing_thread
        process_message(response, client, queue, q, block)
      ensure
        untag_processing_thread
      end

    end

  rescue => e
    EventQ.log(:error, "[#{self.class}] - An error occurred attempting to retrieve a message from the queue. Error: #{e.backtrace}")
  end

  return received
end