Module: Legion::Gaia::OfflineHandler

Extended by:
Logging::Helper
Defined in:
lib/legion/gaia/offline_handler.rb

Constant Summary collapse

DEFAULT_OFFLINE_MESSAGE =
'The agent is currently offline. Your message has been queued.'

Class Method Summary collapse

Class Method Details

.agent_online?(worker_id) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/legion/gaia/offline_handler.rb', line 23

def agent_online?(worker_id)
  presence = presence_store[worker_id]
  return false unless presence

  (Time.now - presence[:last_seen]) < offline_threshold
end

.drain_pending(worker_id) ⇒ Object



39
40
41
42
43
# File 'lib/legion/gaia/offline_handler.rb', line 39

def drain_pending(worker_id)
  drained = pending_store.delete(worker_id) || []
  log.info("OfflineHandler drained pending worker_id=#{worker_id} count=#{drained.size}") if drained.any?
  drained
end

.handle_offline_delivery(input_frame, worker_id:) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/legion/gaia/offline_handler.rb', line 13

def handle_offline_delivery(input_frame, worker_id:)
  queue_message(input_frame, worker_id)
  notify_sender(input_frame)
  log.info(
    'OfflineHandler queued ' \
    "frame_id=#{input_frame.respond_to?(:id) ? input_frame.id : 'unknown'} worker_id=#{worker_id}"
  )
  { queued: true, worker_id: worker_id }
end

.pending_count(worker_id) ⇒ Object



35
36
37
# File 'lib/legion/gaia/offline_handler.rb', line 35

def pending_count(worker_id)
  pending_store[worker_id]&.size || 0
end

.record_presence(worker_id) ⇒ Object



30
31
32
33
# File 'lib/legion/gaia/offline_handler.rb', line 30

def record_presence(worker_id)
  presence_store[worker_id] = { last_seen: Time.now }
  log.debug("OfflineHandler recorded presence worker_id=#{worker_id}")
end

.reset!Object



45
46
47
48
# File 'lib/legion/gaia/offline_handler.rb', line 45

def reset!
  @presence_store = {}
  @pending_store = {}
end