Class: Phobos::Actions::ProcessMessage

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/phobos/actions/process_message.rb

Constant Summary collapse

MAX_SLEEP_INTERVAL =
3

Constants included from Instrumentation

Instrumentation::NAMESPACE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#instrument, subscribe, unsubscribe

Constructor Details

#initialize(listener:, message:, listener_metadata:) ⇒ ProcessMessage

Returns a new instance of ProcessMessage.



10
11
12
13
14
15
16
17
18
19
# File 'lib/phobos/actions/process_message.rb', line 10

def initialize(listener:, message:, listener_metadata:)
  @listener = listener
  @message = message
   = .merge(
    key: message.key,
    partition: message.partition,
    offset: message.offset,
    retry_count: 0
  )
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/phobos/actions/process_message.rb', line 8

def 
  
end

Instance Method Details

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phobos/actions/process_message.rb', line 21

def execute
  backoff = @listener.create_exponential_backoff
  payload = force_encoding(@message.value)

  begin
    process_message(payload)
  rescue => e
    retry_count = [:retry_count]
    interval = backoff.interval_at(retry_count).round(2)

    error = {
      waiting_time: interval,
      exception_class: e.class.name,
      exception_message: e.message,
      backtrace: e.backtrace
    }

    instrument('listener.retry_handler_error', error.merge()) do
      Phobos.logger.error do
        { message: "error processing message, waiting #{interval}s" }.merge(error).merge()
      end

      snooze(interval)
    end

    .merge!(retry_count: retry_count + 1)
    retry
  end
end

#snooze(interval) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/phobos/actions/process_message.rb', line 51

def snooze(interval)
  remaining_interval = interval

  @listener.send_heartbeat_if_necessary

  while remaining_interval.positive?
    sleep [remaining_interval, MAX_SLEEP_INTERVAL].min
    remaining_interval -= MAX_SLEEP_INTERVAL
    @listener.send_heartbeat_if_necessary
  end
end