Class: HermesMessengerOfTheGods::Endpoints::Sqs

Inherits:
Base
  • Object
show all
Defined in:
lib/hermes_messenger_of_the_gods/endpoints/sqs.rb

Constant Summary collapse

VISIBILITY_EXTEND_DURATION =
120
VISIBILITY_EXTEND_FREQUENCY =
60

Constants inherited from Base

Base::DEFAULT_OPTIONS, Base::DEFAULT_RETRYS

Instance Attribute Summary

Attributes inherited from Base

#endpoint, #errors, #name, #options, #result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_transmit_payload, #backoff, #bulk_dispatch!, #dispatch, #dispatch!, #fetch_option, #handle_failure, #handle_success, #max_retries, #retry_from, #teardown, #transform_message

Constructor Details

#initialize(*args) ⇒ Sqs

Returns a new instance of Sqs.



42
43
44
45
46
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 42

def initialize(*args)
  super
  @message_mux = Monitor.new
  @work_start_time_mux = Monitor.new
end

Class Method Details

.k8s_clientObject



19
20
21
22
23
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 19

def self.k8s_client
  return unless defined?(K8s)

  @k8s_client ||= K8s::Client.in_cluster_config
end

.set_deletion_cost(cost) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 25

def self.set_deletion_cost(cost)
  # If there is no k8s client or the cost is the same as the last time we set it, we don't need to do anything
  return unless k8s_client || @last_deletion_cost == cost

  k8s_client.api('v1').resource('pods', namespace: ENV['SYSTEM_NAMESPACE']).merge_patch(ENV['HOSTNAME'], {
    metadata: {
      annotations: { 
        "controller.kubernetes.io/pod-deletion-cost" => cost.to_s
      },
    }
  })

  @last_deletion_cost = cost
rescue StandardError => e
  STDERR.puts "Error setting deletion cost: #{e.message}"
end

Instance Method Details

#bulk_transmit(payloads) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 203

def bulk_transmit(payloads)
  all_entries = payloads.map! do |payload|
    {id: SecureRandom.uuid}.merge(payload)
  end

  failed_msgs = []

  all_entries.each_slice(10) do |entries|
    resp = sqs_client.send_message_batch(queue_url: endpoint, entries: entries)
    failed_msgs.concat(resp.failed)
  end

  if failed_msgs.any?
    all_sender_fault = failed_msgs.failed.all?(&:sender_fault)
    raise  FatalError, "Error in dispatching: #{failed_msgs[0].message}" if all_sender_fault

    raise "Some messages failed to send due to recoverable error #{failed_msgs[0].message}"
  end

  true
end

#has_pending_work?Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 180

def has_pending_work?
  data = queue_data.attributes

  approximate_pending_messages = data['ApproximateNumberOfMessages'].to_i -
                                 data['ApproximateNumberOfMessagesNotVisible'].to_i -
                                 data['ApproximateNumberOfMessagesDelayed'].to_i

  # Just in case the math is off
  approximate_pending_messages.positive?
end

#inflight_messagesObject



73
74
75
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 73

def inflight_messages
  @message_mux.synchronize { @inflight_messages ||= [] }
end

#inflight_messages=(val) ⇒ Object



77
78
79
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 77

def inflight_messages=(val)
  @message_mux.synchronize { @inflight_messages = val }
end

#poll_optionsObject



96
97
98
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 96

def poll_options
  (options[:poll_options] || {}).merge(skip_delete: true)
end

#pollerObject



48
49
50
51
52
53
54
55
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 48

def poller
  @poller ||= Aws::SQS::QueuePoller.new(
    endpoint,
    {
      client: HermesMessengerOfTheGods.configuration.sqs_client,
    }.merge(options[:client_options] || {}),
  )
end

#queueObject



167
168
169
170
171
172
173
174
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 167

def queue
  @queue ||= Aws::SQS::Queue.new(
    endpoint,
    {
      client: HermesMessengerOfTheGods.configuration.sqs_client,
    }.merge(options[:client_options] || {}),
  )
end

#queue_dataObject



176
177
178
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 176

def queue_data
  queue.reload.data
end

#received_work_in_last_check=(val) ⇒ Object



69
70
71
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 69

def received_work_in_last_check=(val)
  @work_start_time_mux.synchronize { @received_work_in_last_check = val }
end

#received_work_in_last_check?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 65

def received_work_in_last_check?
  @work_start_time_mux.synchronize { @received_work_in_last_check || false } 
end

#reset_work_start_time!Object



61
62
63
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 61

def reset_work_start_time!
  @work_start_time_mux.synchronize { @work_start_time = Time.now }
end

#set_reexecution_time(message, duration_or_time) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 133

def set_reexecution_time(message, duration_or_time)
  # You can pass a time to re-run at or you can pass the seconds in the future to run at
  extend_time = if duration_or_time.is_a?(Time)
                  duration_or_time - Time.now
                else
                  duration_or_time
                end

  message_array = [message]

  @message_mux.synchronize do
    set_message_visibility(message_array, (Time.now - work_start_time) + extend_time)

    @inflight_messages -= message_array
  end
end

#shutdown!Object

Basic Shutdown behavior:

Allow in-progress message to finish working.
Reset visbility timeout to all un-executed messages (from current message to end of array) to 0 so they move
to other works

Break from polling


91
92
93
94
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 91

def shutdown!
  warn 'Reveived shutdown signal'
  @shutdown = true
end

#shutting_down?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 81

def shutting_down?
  @shutdown || false
end

#sqs_clientObject



163
164
165
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 163

def sqs_client
  HermesMessengerOfTheGods.configuration.sqs_client
end

#to_transmit_payload(message, raw_message, dispatch_options = {}) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 191

def to_transmit_payload(message, raw_message, dispatch_options = {})
  send_opts = fetch_option(:send_options, raw_message) || {}

  message = JSON.dump(message) if options[:jsonify]

  send_opts.merge(dispatch_options, message_body: message)
end

#transmit(payload) ⇒ Object



199
200
201
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 199

def transmit(payload)
  bulk_transmit([payload])
end

#work_message(message) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 150

def work_message(message)
  message_body = decode_message(message)

  skip_delete = catch(:skip_delete) do
    yield(message_body, message)
    false
  end

  !skip_delete
rescue StandardError => e
  false
end

#work_off(&blk) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 100

def work_off(&blk)
  poller.before_request do |_stats| 
    throw :stop_polling if shutting_down?

    self.class.set_deletion_cost(0) unless received_work_in_last_check?

    self.received_work_in_last_check = false
  end

  poller.poll(poll_options) do |messages, _stats|
    self.inflight_messages = messages = Array.wrap(messages)

    self.received_work_in_last_check = true
    self.class.set_deletion_cost(messages.size)

    working_messages do
      completion_results = messages.group_by do |msg|
        # We return false if we are shutting down so the messages are not deleted.
        # It is also possible that this process has already releases these SQS messages
        # back in to the queue so they may be picked up by another process.
        #
        # Work message returns true if the messager should be considered successful
        shutting_down? ? :shutdown : work_message(msg, &blk)
      end

      poller.delete_messages(completion_results[true] & inflight_messages) unless completion_results.fetch(true, []).empty?
      # Messages skipped due to shutdowns get their visibility set back to 0 so they restart
      # normal failed jobs will be left in queue until their visibility timeout expires to indicate a backoff
      set_message_visibility(completion_results[:shutdown] & inflight_messages, 0) unless completion_results.fetch(:shutdown, []).empty?
    end
  end
end

#work_start_timeObject



57
58
59
# File 'lib/hermes_messenger_of_the_gods/endpoints/sqs.rb', line 57

def work_start_time
  @work_start_time_mux.synchronize { @work_start_time }
end