Class: RobotLab::Web::ActivityLog
- Inherits:
-
Object
- Object
- RobotLab::Web::ActivityLog
- 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
- .clear ⇒ Object
- .instance ⇒ Object
- .log(type, details = {}) ⇒ Object
- .recent(limit = 10) ⇒ Object
-
.safe_log(type, details = {}) ⇒ Object
Record an event, swallowing any error.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize ⇒ ActivityLog
constructor
A new instance of ActivityLog.
- #log(type, details = {}) ⇒ Object
- #recent(limit = 10) ⇒ Object
Constructor Details
#initialize ⇒ ActivityLog
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
.clear ⇒ Object
35 36 37 |
# File 'lib/robot_lab/web/activity_log.rb', line 35 def clear instance.clear end |
.instance ⇒ Object
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
#clear ⇒ Object
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 |