Class: RobotLab::Web::ActivityLog

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

Overview

Thread-safe, bounded, in-memory ring buffer for a dashboard "recent activity" feed.

The cardinal rule, enforced by .safe_log: instrumentation must never break the request that triggered it.

Constant Summary collapse

MAX_EVENTS =
50

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActivityLog

Returns a new instance of ActivityLog.



40
41
42
43
# File 'lib/robot_lab/web/activity_log.rb', line 40

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

Class Method Details

.clearObject



35
36
37
# File 'lib/robot_lab/web/activity_log.rb', line 35

def clear
  instance.clear
end

.instanceObject



16
17
18
# File 'lib/robot_lab/web/activity_log.rb', line 16

def instance
  @instance ||= new
end

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



20
21
22
# File 'lib/robot_lab/web/activity_log.rb', line 20

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

.recent(limit = 10) ⇒ Object



31
32
33
# File 'lib/robot_lab/web/activity_log.rb', line 31

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

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

Record an event, swallowing any error.



25
26
27
28
29
# File 'lib/robot_lab/web/activity_log.rb', line 25

def safe_log(type, details = {})
  log(type, details)
rescue StandardError
  nil
end

Instance Method Details

#clearObject



56
57
58
# File 'lib/robot_lab/web/activity_log.rb', line 56

def clear
  @mutex.synchronize { @events = [] }
end

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



45
46
47
48
49
50
# File 'lib/robot_lab/web/activity_log.rb', line 45

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

#recent(limit = 10) ⇒ Object



52
53
54
# File 'lib/robot_lab/web/activity_log.rb', line 52

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