Class: GcReporter::Reporter
- Inherits:
-
Object
- Object
- GcReporter::Reporter
- Defined in:
- lib/gc_reporter/reporter.rb
Class Method Summary collapse
-
.report_once! ⇒ Object
Sends a single, synchronous report.
- .start ⇒ Object
-
.stop ⇒ Object
Stops the background reporting thread.
Class Method Details
.report_once! ⇒ Object
Sends a single, synchronous report. Ideal for short-lived scripts like Rake tasks.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gc_reporter/reporter.rb', line 43 def self.report_once! config = GcReporter.configuration return log("Cannot report: URL not configured.", level: :error) if config.url.nil? log("Performing a one-off report...") # Using a block ensures the client is created and torn down cleanly. ActionCableClient.start(config.url, channel: config.channel) do |client| client.subscribed do log("One-off report client subscribed. Sending data.") client.perform('report', collect_stats) # Disconnect after sending to allow the parent script to exit. client.disconnect! end end end |
.start ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gc_reporter/reporter.rb', line 7 def self.start config = GcReporter.configuration return if running? return log("Cannot start: URL not configured.", level: :error) if config.url.nil? @running = true @thread = Thread.new do client = ActionCableClient.new(config.url, channel: config.channel) log("Reporter thread started. Sending stats every #{config.interval}s.") client.connected { log("Successfully connected to Action Cable at #{config.url}.") } client.disconnected { log("Disconnected from Action Cable. Client will try to reconnect.") } loop do break unless @running begin client.perform('report', collect_stats) if client.subscribed? rescue => e log("Error reporting stats: #{e.message}", level: :error) end sleep config.interval end end end |
.stop ⇒ Object
Stops the background reporting thread.
33 34 35 36 37 38 39 |
# File 'lib/gc_reporter/reporter.rb', line 33 def self.stop return unless running? @running = false @thread&.kill @thread = nil log("Reporter stopped.") end |