Much of the diagnostics implementation can be private

Much of the diagnostics implementation can be private

From c5248e12379c9781f2dd3b6f756f819dd0de1f44 Mon Sep 17 00:00:00 2001
From: Ben Langfeld <blangfeld@powerhrg.com>
Date: Wed, 28 Nov 2018 10:42:53 -0200
Subject: [PATCH] Much of the diagnostics implementation can be private


diff --git a/lib/message_bus/diagnostics.rb b/lib/message_bus/diagnostics.rb
index 65485d7..b6457a8 100644
--- a/lib/message_bus/diagnostics.rb
+++ b/lib/message_bus/diagnostics.rb
@@ -1,53 +1,57 @@
 class MessageBus::Diagnostics
-  def self.full_process_path
-    begin
-      system = `uname`.strip
-      if system == "Darwin"
-        `ps -o "comm=" -p #{Process.pid}`
-      elsif system == "FreeBSD"
-        `ps -o command -p #{Process.pid}`.split("\n", 2)[1].strip
-      else
-        info = `ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`
-        info.strip.split('|$|')[1]
+  class << self
+    def enable(bus = MessageBus)
+      full_path = full_process_path
+      start_time = Time.now.to_f
+      hostname = self.hostname
+
+      # it may make sense to add a channel per machine/host to streamline
+      #  process to process comms
+      bus.subscribe('/_diagnostics/hup') do |msg|
+        if Process.pid == msg.data["pid"] && hostname == msg.data["hostname"]
+          $shutdown = true
+          sleep 4
+          Process.kill("HUP", $$)
+        end
       end
-    rescue
-      # skip it ... not linux or something weird
-    end
-  end
 
-  def self.hostname
-    begin
-      `hostname`.strip
-    rescue
-      # skip it
+      bus.subscribe('/_diagnostics/discover') do |msg|
+        bus.on_connect.call msg.site_id if bus.on_connect
+        bus.publish '/_diagnostics/process-discovery', {
+          pid: Process.pid,
+          process_name: $0,
+          full_path: full_path,
+          uptime: (Time.now.to_f - start_time).to_i,
+          hostname: hostname
+        }, user_ids: [msg.data["user_id"]]
+        bus.on_disconnect.call msg.site_id if bus.on_disconnect
+      end
     end
-  end
 
-  def self.enable(bus = MessageBus)
-    full_path = full_process_path
-    start_time = Time.now.to_f
-    hostname = self.hostname
+    private
 
-    # it may make sense to add a channel per machine/host to streamline
-    #  process to process comms
-    bus.subscribe('/_diagnostics/hup') do |msg|
-      if Process.pid == msg.data["pid"] && hostname == msg.data["hostname"]
-        $shutdown = true
-        sleep 4
-        Process.kill("HUP", $$)
+    def full_process_path
+      begin
+        system = `uname`.strip
+        if system == "Darwin"
+          `ps -o "comm=" -p #{Process.pid}`
+        elsif system == "FreeBSD"
+          `ps -o command -p #{Process.pid}`.split("\n", 2)[1].strip
+        else
+          info = `ps -eo "%p|$|%a" | grep '^\\s*#{Process.pid}'`
+          info.strip.split('|$|')[1]
+        end
+      rescue
+        # skip it ... not linux or something weird
       end
     end
 
-    bus.subscribe('/_diagnostics/discover') do |msg|
-      bus.on_connect.call msg.site_id if bus.on_connect
-      bus.publish '/_diagnostics/process-discovery', {
-        pid: Process.pid,
-        process_name: $0,
-        full_path: full_path,
-        uptime: (Time.now.to_f - start_time).to_i,
-        hostname: hostname
-      }, user_ids: [msg.data["user_id"]]
-      bus.on_disconnect.call msg.site_id if bus.on_disconnect
+    def hostname
+      begin
+        `hostname`.strip
+      rescue
+        # skip it
+      end
     end
   end
 end
diff --git a/lib/message_bus/rack/diagnostics.rb b/lib/message_bus/rack/diagnostics.rb
index fd11e3f..809fdd9 100644
--- a/lib/message_bus/rack/diagnostics.rb
+++ b/lib/message_bus/rack/diagnostics.rb
@@ -8,6 +8,44 @@ class MessageBus::Rack::Diagnostics
     @bus = config[:message_bus] || MessageBus
   end
 
+  def call(env)
+    return @app.call(env) unless env['PATH_INFO'].start_with? '/message-bus/_diagnostics'
+
+    route = env['PATH_INFO'].split('/message-bus/_diagnostics')[1]
+
+    if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
+      return [403, {}, ['not allowed']]
+    end
+
+    return index unless route
+
+    if route == '/discover'
+      user_id = @bus.user_id_lookup.call(env)
+      @bus.publish('/_diagnostics/discover', user_id: user_id)
+      return [200, {}, ['ok']]
+    end
+
+    if route =~ /^\/hup\//
+      hostname, pid = route.split('/hup/')[1].split('/')
+      @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
+      return [200, {}, ['ok']]
+    end
+
+    asset = route.split('/assets/')[1]
+    if asset && !asset !~ /\//
+      content = asset_contents(asset)
+      split = asset.split('.')
+      if split[1] == 'handlebars'
+        content = translate_handlebars(split[0], content)
+      end
+      return [200, { 'content-type' => 'text/javascript;' }, [content]]
+    end
+
+    return [404, {}, ['not found']]
+  end
+
+  private
+
   def js_asset(name)
     return generate_script_tag(name) unless @bus.cache_assets
 
@@ -62,40 +100,4 @@ class MessageBus::Rack::Diagnostics
   def indent(string)
     string.gsub(/$(.)/m, "\\1  ").strip
   end
-
-  def call(env)
-    return @app.call(env) unless env['PATH_INFO'].start_with? '/message-bus/_diagnostics'
-
-    route = env['PATH_INFO'].split('/message-bus/_diagnostics')[1]
-
-    if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
-      return [403, {}, ['not allowed']]
-    end
-
-    return index unless route
-
-    if route == '/discover'
-      user_id = @bus.user_id_lookup.call(env)
-      @bus.publish('/_diagnostics/discover', user_id: user_id)
-      return [200, {}, ['ok']]
-    end
-
-    if route =~ /^\/hup\//
-      hostname, pid = route.split('/hup/')[1].split('/')
-      @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
-      return [200, {}, ['ok']]
-    end
-
-    asset = route.split('/assets/')[1]
-    if asset && !asset !~ /\//
-      content = asset_contents(asset)
-      split = asset.split('.')
-      if split[1] == 'handlebars'
-        content = translate_handlebars(split[0], content)
-      end
-      return [200, { 'content-type' => 'text/javascript;' }, [content]]
-    end
-
-    return [404, {}, ['not found']]
-  end
 end

GitHub