From 0dd52457aeb958057fe810ad46df9fd9fd27f0a8 Mon Sep 17 00:00:00 2001 From: schneems Date: Wed, 1 Jun 2016 09:08:13 -0500 Subject: [PATCH] Code formatting --- lib/get_process_mem.rb | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/get_process_mem.rb b/lib/get_process_mem.rb index 8c85282..b8934b2 100644 --- a/lib/get_process_mem.rb +++ b/lib/get_process_mem.rb @@ -1,13 +1,6 @@ require 'pathname' require 'bigdecimal' -if Gem.win_platform? - require 'rubygems' - require 'sys/proctable' - include Sys -end - - # Cribbed from Unicorn Worker Killer, thanks! class GetProcessMem KB_TO_BYTE = 1024 # 2**10 = 1024 @@ -17,6 +10,19 @@ class GetProcessMem ROUND_UP = BigDecimal.new("0.5") attr_reader :pid + RUNS_ON_WINDOWS = Gem.win_platform? + + if RUNS_ON_WINDOWS + begin + require 'sys/proctable' + rescue LoadError => e + message = "Please add `sys-proctable` to your Gemfile for windows machines\n" + message << e.message + raise e, message + end + include Sys + end + def initialize(pid = Process.pid) @status_file = Pathname.new "/proc/#{pid}/status" @process_file = Pathname.new "/proc/#{pid}/smaps" @@ -80,13 +86,11 @@ def linux_memory(file = @process_file) # Pull memory from `ps` command, takes more resources and can freeze # in low memory situations def ps_memory - case - when /mswin|mingw/ =~ RUBY_PLATFORM - size = ProcTable.ps(pid).working_set_size; - BigDecimal.new(size) - else - KB_TO_BYTE * BigDecimal.new(`ps -o rss= -p #{pid}`) + if RUNS_ON_WINDOWS + size = ProcTable.ps(pid).working_set_size + BigDecimal.new(size) + else + KB_TO_BYTE * BigDecimal.new(`ps -o rss= -p #{pid}`) end end - end