Class: Epi::RunningProcess

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid, ps_line: nil, job: nil) ⇒ RunningProcess

Returns a new instance of RunningProcess.



45
46
47
48
49
50
51
# File 'lib/epi/running_process.rb', line 45

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

#pidObject (readonly)

Returns the value of attribute pid.



39
40
41
# File 'lib/epi/running_process.rb', line 39

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] ||= `id -un #{uid}`.chomp
end

Instance Method Details

#commandString

The command that was used to start the process, including its arguments

Returns:



120
121
122
# File 'lib/epi/running_process.rb', line 120

def command
  @command ||= parts[12]
end

#cpu_percentageFloat

CPU usage as a percentage

Returns:

  • (Float)


66
67
68
# File 'lib/epi/running_process.rb', line 66

def cpu_percentage
  @cpu_percentage ||= parts[1].to_f
end

#groupString

Name of the group that owns the process

Returns:



114
115
116
# File 'lib/epi/running_process.rb', line 114

def group
  @group ||= self.class.group_name parts[11]
end

#jobObject



159
160
161
# File 'lib/epi/running_process.rb', line 159

def job
  @job ||= Jobs.by_pid[pid]
end

#kill(timeout = DEFAULT_TIMEOUT) ⇒ RunningProcess

Kill a running process

Parameters:

  • timeout (TrueClass|FalseClass|Numeric) (defaults to: DEFAULT_TIMEOUT)

    ‘true` to kill immediately (KILL), `false` to kill gracefully (TERM), or a number of seconds to wait between trying both (first TERM, then KILL).

Returns:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/epi/running_process.rb', line 135

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

Returns:



155
156
157
# File 'lib/epi/running_process.rb', line 155

def kill!
  kill true
end

#loggerObject



41
42
43
# File 'lib/epi/running_process.rb', line 41

def logger
  Epi.logger
end

#memory_percentageFloat

Physical memory usage as a percentage

Returns:

  • (Float)


72
73
74
# File 'lib/epi/running_process.rb', line 72

def memory_percentage
  @memory_percentage ||= parts[2].to_f
end

#physical_memoryFixnum

Physical memory usage in bytes (rounded to the nearest kilobyte)

Returns:

  • (Fixnum)


78
79
80
# File 'lib/epi/running_process.rb', line 78

def physical_memory
  @physical_memory ||= parts[3].to_i * 1024
end

#reload!Object



53
54
55
56
57
# File 'lib/epi/running_process.rb', line 53

def reload!
  @props = {}
  @parts = nil
  @ps_line = `ps -p #{pid} -o #{PS_FORMAT}`.lines[1]
end

#restart!Object

Raises:



163
164
165
166
# File 'lib/epi/running_process.rb', line 163

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

Returns:

  • (TrueClass|FalseClass)


126
127
128
# File 'lib/epi/running_process.rb', line 126

def root?
  user == 'root'
end

#started_atTime

Time at which the process was started

Returns:

  • (Time)


96
97
98
# File 'lib/epi/running_process.rb', line 96

def started_at
  @started_at ||= Time.parse parts[5..9].join ' '
end

#total_memoryFixnum

Returns:

  • (Fixnum)


90
91
92
# File 'lib/epi/running_process.rb', line 90

def total_memory
  @total_memory ||= physical_memory + virtual_memory
end

#uptimeFloat

Duration the process has been running (in seconds)

Returns:

  • (Float)


102
103
104
# File 'lib/epi/running_process.rb', line 102

def uptime
  Time.now - started_at
end

#userString

Name of the user that owns the process

Returns:



108
109
110
# File 'lib/epi/running_process.rb', line 108

def user
  @user ||= self.class.user_name parts[10]
end

#virtual_memoryFixnum

Virtual memory usage in bytes (rounded to the nearest kilobyte)

Returns:

  • (Fixnum)


84
85
86
# File 'lib/epi/running_process.rb', line 84

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/epi/running_process.rb', line 60

def was_alive?
  !@ps_line.nil?
end