Module: Legion::Gaia::OfflineHandler

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)


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

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



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

def drain_pending(worker_id)
  pending_store.delete(worker_id) || []
end

.handle_offline_delivery(input_frame, worker_id:) ⇒ Object



9
10
11
12
13
# File 'lib/legion/gaia/offline_handler.rb', line 9

def handle_offline_delivery(input_frame, worker_id:)
  queue_message(input_frame, worker_id)
  notify_sender(input_frame)
  { queued: true, worker_id: worker_id }
end

.pending_count(worker_id) ⇒ Object



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

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

.record_presence(worker_id) ⇒ Object



22
23
24
# File 'lib/legion/gaia/offline_handler.rb', line 22

def record_presence(worker_id)
  presence_store[worker_id] = { last_seen: Time.now }
end

.reset!Object



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

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