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.



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

def initialize(listener:, message:, listener_metadata:)
  @listener = listener
  @message = message
  @metadata = .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.



10
11
12
# File 'lib/phobos/actions/process_message.rb', line 10

def 
  @metadata
end

Instance Method Details

#executeObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/phobos/actions/process_message.rb', line 23

def execute
  payload = force_encoding(@message.value)

  begin
    process_message(payload)
  rescue StandardError => e
    handle_error(e)
    retry
  end
end

#snooze(interval) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/phobos/actions/process_message.rb', line 34

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