Class: SqsBuffer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sqs_buffer/client.rb

Instance Method Summary collapse

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)
  @max_number_of_messages = 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
  @message_queue = 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



99
100
101
# File 'lib/sqs_buffer/client.rb', line 99

def before_request_block(&block)
  @before_request_block.value = block
end

#bufferObject



66
67
68
69
# File 'lib/sqs_buffer/client.rb', line 66

def buffer
  # Return a copy of the array events to guard against potential mutation
  Marshal.load( Marshal.dump(@message_queue) )
end

#buffer_empty?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/sqs_buffer/client.rb', line 58

def buffer_empty?
  @message_queue.empty?
end

#buffer_full?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/sqs_buffer/client.rb', line 54

def buffer_full?
  @message_queue.length >= @max_queue_threshold.value
end

#buffer_lengthObject



62
63
64
# File 'lib/sqs_buffer/client.rb', line 62

def buffer_length
  @message_queue.length
end

#last_process_time_stale?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/sqs_buffer/client.rb', line 83

def last_process_time_stale?
  @last_process_time.value < Time.now.to_i - @max_wait_time.value
end

#process_all_messagesObject



87
88
89
90
91
92
93
# File 'lib/sqs_buffer/client.rb', line 87

def process_all_messages
  @process_block.value.call(buffer)
  delete_all_messages
  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



95
96
97
# File 'lib/sqs_buffer/client.rb', line 95

def process_block(&block)
  @process_block.value = block
end

#queue_urlObject



46
47
48
# File 'lib/sqs_buffer/client.rb', line 46

def queue_url
  @queue_url
end

#running?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/sqs_buffer/client.rb', line 75

def running?
  @running.true? && worker_thread_alive?
end

#shutting_down?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/sqs_buffer/client.rb', line 71

def shutting_down?
  @running.false? && worker_thread_alive?
end

#start_pollingObject



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: @max_number_of_messages
    }
    @poller.poll(opts) do |messages|
      store_messages(messages)
    end
  end # End worker thread

  @running.value
end

#stop_pollingObject



50
51
52
# File 'lib/sqs_buffer/client.rb', line 50

def stop_polling
  @running.make_false
end

#worker_thread_alive?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/sqs_buffer/client.rb', line 79

def worker_thread_alive?
  !@worker_thread.nil? && @worker_thread.alive?
end