Class: SqsBuffer::Client
- Inherits:
-
Object
- Object
- SqsBuffer::Client
- Defined in:
- lib/sqs_buffer/client.rb
Instance Method Summary collapse
- #before_request_block(&block) ⇒ Object
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #last_process_time_stale? ⇒ Boolean
- #process_all_messages ⇒ Object
- #process_block(&block) ⇒ Object
- #queue_empty? ⇒ Boolean
- #queue_full? ⇒ Boolean
- #queue_length ⇒ Object
- #queue_url ⇒ Object
- #running? ⇒ Boolean
- #shutting_down? ⇒ Boolean
- #start_polling ⇒ Object
- #stop_polling ⇒ Object
- #worker_thread_alive? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sqs_buffer/client.rb', line 7 def initialize(opts) @queue_url = opts.fetch(:queue_url) { |k| missing_key!(k) } client = opts.fetch(:client) { |k| missing_key!(k) } @poller = Aws::SQS::QueuePoller.new(@queue_url, client: client) @skip_delete = opts.fetch(:skip_delete, true) = opts.fetch(:max_number_of_messages, 10) @logger = opts.fetch(:logger, Logger.new(STDOUT)) @process_block = Concurrent::MutexAtomicReference.new @before_request_block = Concurrent::MutexAtomicReference.new @process_block = Concurrent::MutexAtomicReference.new = Concurrent::Array.new @last_process_time = Concurrent::AtomicFixnum.new(Time.now.to_i) @running = Concurrent::AtomicBoolean.new(false) @max_wait_time = Concurrent::AtomicFixnum.new( opts.fetch(:max_wait_time, 300) ) @max_queue_threshold = Concurrent::AtomicFixnum.new( opts.fetch(:max_queue_threshold, 100) ) configure_before_request_block end |
Instance Method Details
#before_request_block(&block) ⇒ Object
97 98 99 |
# File 'lib/sqs_buffer/client.rb', line 97 def before_request_block(&block) @before_request_block.value = block end |
#last_process_time_stale? ⇒ Boolean
78 79 80 |
# File 'lib/sqs_buffer/client.rb', line 78 def last_process_time_stale? @last_process_time.value < Time.now.to_i - @max_wait_time.value end |
#process_all_messages ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sqs_buffer/client.rb', line 82 def # This will be a collection of SQS messages # I am pretty sure this exposes the @message_queue to mutability # Maybe I should deep dup this? @process_block.value.call() touch_process_time rescue StandardError => e @logger.error "An exception(#{e.message}) occurred while process the message queue: #{@message_queue.join("\n")} | Backtrace: #{e.backtrace}" end |
#process_block(&block) ⇒ Object
93 94 95 |
# File 'lib/sqs_buffer/client.rb', line 93 def process_block(&block) @process_block.value = block end |
#queue_empty? ⇒ Boolean
58 59 60 |
# File 'lib/sqs_buffer/client.rb', line 58 def queue_empty? .empty? end |
#queue_full? ⇒ Boolean
54 55 56 |
# File 'lib/sqs_buffer/client.rb', line 54 def queue_full? .length >= @max_queue_threshold.value end |
#queue_length ⇒ Object
62 63 64 |
# File 'lib/sqs_buffer/client.rb', line 62 def queue_length .length end |
#queue_url ⇒ Object
46 47 48 |
# File 'lib/sqs_buffer/client.rb', line 46 def queue_url @queue_url end |
#running? ⇒ Boolean
70 71 72 |
# File 'lib/sqs_buffer/client.rb', line 70 def running? @running.true? && worker_thread_alive? end |
#shutting_down? ⇒ Boolean
66 67 68 |
# File 'lib/sqs_buffer/client.rb', line 66 def shutting_down? @running.false? && worker_thread_alive? end |
#start_polling ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/sqs_buffer/client.rb', line 30 def start_polling @running.make_true @worker_thread = Thread.new do opts = { skip_delete: @skip_delete, max_number_of_messages: } @poller.poll(opts) do || () end end # End worker thread @running.value end |
#stop_polling ⇒ Object
50 51 52 |
# File 'lib/sqs_buffer/client.rb', line 50 def stop_polling @running.make_false end |
#worker_thread_alive? ⇒ Boolean
74 75 76 |
# File 'lib/sqs_buffer/client.rb', line 74 def worker_thread_alive? !@worker_thread.nil? && @worker_thread.alive? end |