Class: Legate::ActivityLog

Inherits:
Object
  • Object
show all
Defined in:
lib/legate/activity_log.rb

Overview

Simple in-memory activity log for tracking system events.

Constant Summary collapse

MAX_EVENTS =
50

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActivityLog

Returns a new instance of ActivityLog.



35
36
37
38
# File 'lib/legate/activity_log.rb', line 35

def initialize
  @events = []
  @mutex = Mutex.new
end

Class Method Details

.clearObject



30
31
32
# File 'lib/legate/activity_log.rb', line 30

def clear
  instance.clear
end

.instanceObject



9
10
11
# File 'lib/legate/activity_log.rb', line 9

def instance
  @instance ||= new
end

.log(event_type, details = {}) ⇒ Object

Delegate class methods to instance



14
15
16
# File 'lib/legate/activity_log.rb', line 14

def log(event_type, details = {})
  instance.log(event_type, details)
end

.recent(limit = 10) ⇒ Object



26
27
28
# File 'lib/legate/activity_log.rb', line 26

def recent(limit = 10)
  instance.recent(limit)
end

.safe_log(event_type, details = {}) ⇒ Object

Records an event, swallowing any error — activity logging must never break the request that triggered it.



20
21
22
23
24
# File 'lib/legate/activity_log.rb', line 20

def safe_log(event_type, details = {})
  log(event_type, details)
rescue StandardError => e
  Legate.logger&.debug { "ActivityLog: failed to record #{event_type}: #{e.message}" }
end

Instance Method Details

#clearObject

Clear all events



65
66
67
68
69
# File 'lib/legate/activity_log.rb', line 65

def clear
  @mutex.synchronize do
    @events = []
  end
end

#log(event_type, details = {}) ⇒ Object

Log a new event

Parameters:

  • event_type (Symbol)

    Type of event (:agent_started, :agent_stopped, etc.)

  • details (Hash) (defaults to: {})

    Event details (e.g., { name: ‘agent_name’ })



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legate/activity_log.rb', line 43

def log(event_type, details = {})
  @mutex.synchronize do
    event = {
      type: event_type.to_sym,
      details: details,
      timestamp: Time.now.utc
    }
    @events.unshift(event)
    @events = @events.first(MAX_EVENTS)
  end
end

#recent(limit = 10) ⇒ Array<Hash>

Get recent events

Parameters:

  • limit (Integer) (defaults to: 10)

    Number of events to return

Returns:

  • (Array<Hash>)

    Recent events



58
59
60
61
62
# File 'lib/legate/activity_log.rb', line 58

def recent(limit = 10)
  @mutex.synchronize do
    @events.first(limit)
  end
end