Class: Epi::RunningProcess
- Inherits:
-
Object
- Object
- Epi::RunningProcess
- Defined in:
- lib/epi/running_process.rb
Overview
noinspection RubyTooManyInstanceVariablesInspection
Constant Summary collapse
- DEFAULT_TIMEOUT =
20
- PS_FORMAT =
'pid,%cpu,%mem,rss,vsz,lstart,uid,gid,command'
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Class Method Summary collapse
Instance Method Summary collapse
-
#command ⇒ String
The command that was used to start the process, including its arguments.
-
#cpu_percentage ⇒ Float
CPU usage as a percentage.
-
#group ⇒ String
Name of the group that owns the process.
-
#initialize(pid, ps_line: nil, job: nil) ⇒ RunningProcess
constructor
A new instance of RunningProcess.
- #job ⇒ Object
-
#kill(timeout = DEFAULT_TIMEOUT) ⇒ RunningProcess
Kill a running process.
-
#kill! ⇒ RunningProcess
Kill a running process immediately and synchronously with kill -9.
- #logger ⇒ Object
-
#memory_percentage ⇒ Float
Physical memory usage as a percentage.
-
#physical_memory ⇒ Fixnum
Physical memory usage in bytes (rounded to the nearest kilobyte).
- #reload! ⇒ Object
- #restart! ⇒ Object
-
#root? ⇒ TrueClass|FalseClass
Whether the process is root-owned.
-
#started_at ⇒ Time
Time at which the process was started.
-
#total_memory ⇒ Fixnum
Sum of #physical_memory and #total_memory.
-
#uptime ⇒ Float
Duration the process has been running (in seconds).
-
#user ⇒ String
Name of the user that owns the process.
-
#virtual_memory ⇒ Fixnum
Virtual memory usage in bytes (rounded to the nearest kilobyte).
-
#was_alive? ⇒ Boolean
Returns
true
if the process was running when this instance was created.
Constructor Details
#initialize(pid, ps_line: nil, job: nil) ⇒ RunningProcess
Returns a new instance of RunningProcess.
50 51 52 53 54 55 56 |
# File 'lib/epi/running_process.rb', line 50 def initialize(pid, ps_line: nil, job: nil) @pid = pid.to_i @ps_line = ps_line @props = {} @job = job reload! unless ps_line end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
44 45 46 |
# File 'lib/epi/running_process.rb', line 44 def pid @pid end |
Class Method Details
.group_name(gid) ⇒ Object
17 18 19 |
# File 'lib/epi/running_process.rb', line 17 def group_name(gid) groups[gid.to_i] end |
.user_name(uid) ⇒ Object
13 14 15 |
# File 'lib/epi/running_process.rb', line 13 def user_name(uid) @users[uid.to_i] ||= %x[#{username_lookup_cmd % uid}].chomp end |
Instance Method Details
#command ⇒ String
The command that was used to start the process, including its arguments
125 126 127 |
# File 'lib/epi/running_process.rb', line 125 def command @command ||= parts[12] end |
#cpu_percentage ⇒ Float
CPU usage as a percentage
71 72 73 |
# File 'lib/epi/running_process.rb', line 71 def cpu_percentage @cpu_percentage ||= parts[1].to_f end |
#group ⇒ String
Name of the group that owns the process
119 120 121 |
# File 'lib/epi/running_process.rb', line 119 def group @group ||= self.class.group_name parts[11] end |
#job ⇒ Object
164 165 166 |
# File 'lib/epi/running_process.rb', line 164 def job @job ||= Jobs.by_pid[pid] end |
#kill(timeout = DEFAULT_TIMEOUT) ⇒ RunningProcess
Kill a running process
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/epi/running_process.rb', line 140 def kill(timeout = DEFAULT_TIMEOUT) if timeout.is_a? Numeric begin logger.info "Will wait #{timeout} second#{timeout != 1 && 's'} for process to terminate gracefully" Timeout::timeout(timeout) { kill false } rescue Timeout::Error kill true end else signal = timeout ? 'KILL' : 'TERM' logger.info "Sending #{signal} to process #{pid}" Process.kill signal, pid rescue Errno::ESRCH false sleep 0.2 while `ps -p #{pid} > /dev/null 2>&1; echo $?`.chomp.to_i == 0 logger.info "Process #{pid} terminated by signal #{signal}" end self end |
#kill! ⇒ RunningProcess
Kill a running process immediately and synchronously with kill -9
160 161 162 |
# File 'lib/epi/running_process.rb', line 160 def kill! kill true end |
#memory_percentage ⇒ Float
Physical memory usage as a percentage
77 78 79 |
# File 'lib/epi/running_process.rb', line 77 def memory_percentage @memory_percentage ||= parts[2].to_f end |
#physical_memory ⇒ Fixnum
Physical memory usage in bytes (rounded to the nearest kilobyte)
83 84 85 |
# File 'lib/epi/running_process.rb', line 83 def physical_memory @physical_memory ||= parts[3].to_i * 1024 end |
#reload! ⇒ Object
58 59 60 61 62 |
# File 'lib/epi/running_process.rb', line 58 def reload! @props = {} @parts = nil @ps_line = `ps -p #{pid} -o #{PS_FORMAT}`.lines[1] end |
#restart! ⇒ Object
168 169 170 171 |
# File 'lib/epi/running_process.rb', line 168 def restart! raise Exceptions::Fatal, 'Cannot restart this process because it is not managed by a job' unless job job.replace pid end |
#root? ⇒ TrueClass|FalseClass
Whether the process is root-owned
131 132 133 |
# File 'lib/epi/running_process.rb', line 131 def root? user == 'root' end |
#started_at ⇒ Time
Time at which the process was started
101 102 103 |
# File 'lib/epi/running_process.rb', line 101 def started_at @started_at ||= Time.parse parts[5..9].join ' ' end |
#total_memory ⇒ Fixnum
Sum of #physical_memory and #total_memory
95 96 97 |
# File 'lib/epi/running_process.rb', line 95 def total_memory @total_memory ||= physical_memory + virtual_memory end |
#uptime ⇒ Float
Duration the process has been running (in seconds)
107 108 109 |
# File 'lib/epi/running_process.rb', line 107 def uptime Time.now - started_at end |
#user ⇒ String
Name of the user that owns the process
113 114 115 |
# File 'lib/epi/running_process.rb', line 113 def user @user ||= self.class.user_name parts[10] end |
#virtual_memory ⇒ Fixnum
Virtual memory usage in bytes (rounded to the nearest kilobyte)
89 90 91 |
# File 'lib/epi/running_process.rb', line 89 def virtual_memory @virtual_memory ||= parts[4].to_i * 1024 end |
#was_alive? ⇒ Boolean
Returns true
if the process was running when this instance was created
65 66 67 |
# File 'lib/epi/running_process.rb', line 65 def was_alive? !@ps_line.nil? end |