Class: RailsPerformance::SystemMonitor::ResourcesMonitor
- Inherits:
-
Object
- Object
- RailsPerformance::SystemMonitor::ResourcesMonitor
- Defined in:
- lib/rails_performance/system_monitor/resources_monitor.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
Instance Method Summary collapse
-
#initialize(context, role) ⇒ ResourcesMonitor
constructor
A new instance of ResourcesMonitor.
- #monitors ⇒ Object
- #payload ⇒ Object
- #run ⇒ Object
- #server_id ⇒ Object
- #start_monitoring ⇒ Object
- #stop_monitoring ⇒ Object
- #store_data(data) ⇒ Object
Constructor Details
#initialize(context, role) ⇒ ResourcesMonitor
Returns a new instance of ResourcesMonitor.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 8 def initialize(context, role) @context = context @role = role @mutex = Mutex.new @thread = nil return unless RailsPerformance._resource_monitor_enabled start_monitoring end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 6 def context @context end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
6 7 8 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 6 def role @role end |
Instance Method Details
#monitors ⇒ Object
50 51 52 53 54 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 50 def monitors @monitors ||= RailsPerformance.system_monitors.map do |class_name| RailsPerformance::SystemMonitor.const_get(class_name).new(nil) end end |
#payload ⇒ Object
44 45 46 47 48 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 44 def payload monitors.reduce({}) do |data, monitor| data.merge(monitor.key => monitor.measure) end end |
#run ⇒ Object
56 57 58 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 56 def run store_data(payload) end |
#server_id ⇒ Object
75 76 77 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 75 def server_id @server_id ||= ENV["RAILS_PERFORMANCE_SERVER_ID"] || `hostname`.strip end |
#start_monitoring ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 19 def start_monitoring @mutex.synchronize do return if @thread @thread = Thread.new do loop do run rescue => e ::Rails.logger.error "Monitor error: #{e.}" ensure sleep 60 end end end end |
#stop_monitoring ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 35 def stop_monitoring @mutex.synchronize do return unless @thread @thread.kill @thread = nil end end |
#store_data(data) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rails_performance/system_monitor/resources_monitor.rb', line 60 def store_data(data) ::Rails.logger.info("Server: #{server_id}, Context: #{context}, Role: #{role}, data: #{data}") now = RailsPerformance::Utils.kind_of_now now = now.change(sec: 0, usec: 0) RailsPerformance::Models::ResourceRecord.new( server: server_id, context: context, role: role, datetime: now.strftime(RailsPerformance::FORMAT), datetimei: now.to_i, json: data ).save end |