Class: ProcessObserver::LinuxProcess

Inherits:
Process
  • Object
show all
Defined in:
lib/process_observer/process.rb

Overview

Class representing process in Unix.

Instance Attribute Summary collapse

Attributes inherited from Process

#memory, #name

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ LinuxProcess

Initialize new process.

Parameters:

  • options (Hash)

Options Hash (options):

  • comm (String)

    command which launched process.

  • pid (Integer)

    process ID.

  • stat (String, nil)

    process status.

  • time (String, nil)

    amount of time process is running.

  • rss (Integer, nil)

    amount of used CPU memory in KB.



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/process_observer/process.rb', line 191

def initialize(options)
  @comm = options[:comm].to_s
  @pid  = options[:pid].to_i
  @stat = options[:stat] ? options[:stat].to_s : nil
  @time = options[:time] ? options[:time].to_s : nil
  @rss  = options[:rss]  ? options[:rss].to_i  : nil

  super(
    name: @comm,
    pid: @pid,
    memory: @rss
  )
end

Instance Attribute Details

#commString (readonly)

Returns command which launched process.

Returns:

  • (String)

    command which launched process.



163
164
165
# File 'lib/process_observer/process.rb', line 163

def comm
  @comm
end

#pidInteger (readonly)

Returns process ID.

Returns:

  • (Integer)

    process ID.



167
168
169
# File 'lib/process_observer/process.rb', line 167

def pid
  @pid
end

#rssInteger? (readonly)

Returns amount of used CPU memory in KB.

Returns:

  • (Integer, nil)

    amount of used CPU memory in KB.



179
180
181
# File 'lib/process_observer/process.rb', line 179

def rss
  @rss
end

#statString? (readonly)

Returns process status.

Returns:

  • (String, nil)

    process status.



171
172
173
# File 'lib/process_observer/process.rb', line 171

def stat
  @stat
end

#timeString? (readonly)

Returns amount of time process is running.

Returns:

  • (String, nil)

    amount of time process is running.



175
176
177
# File 'lib/process_observer/process.rb', line 175

def time
  @time
end

Instance Method Details

#==(other) ⇒ Object

Compare with other process by PID.



226
227
228
# File 'lib/process_observer/process.rb', line 226

def ==(other)
  LinuxProcess === other && other.pid == @pid
end

#inspect(splitter = "; ") ⇒ String

Inspect all stored data.

Parameters:

  • splitter (String) (defaults to: "; ")

Returns:

  • (String)

    all accessable info in human-readable form.



217
218
219
220
221
222
# File 'lib/process_observer/process.rb', line 217

def inspect(splitter = "; ")
  to_s +
  (@stat ? "#{splitter}Status: #{@stat}" : "") +
  (@time ? "#{splitter}Time running: #{@time}" : "") +
  (@rss ? "#{splitter}Memory: #{@rss} KB" : "")
end

#to_sString

Returns PID and command of process.

Returns:

  • (String)

    PID and command of process.



207
208
209
# File 'lib/process_observer/process.rb', line 207

def to_s
  "Process ##{@pid} #{@comm}"
end