Class: Processes::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/w-stdlib/processes.rb

Instance Method Summary collapse

Constructor Details

#initialize(snapshot, proc) ⇒ Process

Returns a new instance of Process.



37
38
39
40
41
# File 'lib/w-stdlib/processes.rb', line 37

def initialize(snapshot, proc)
  raise 'proc cant be nil' unless proc
  @snapshot = snapshot
  @proc = proc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object (private)



70
71
72
# File 'lib/w-stdlib/processes.rb', line 70

private def method_missing(symbol, *args)
  @proc.send symbol, *args
end

Instance Method Details

#childrenObject



66
67
68
# File 'lib/w-stdlib/processes.rb', line 66

def children
  @snapshot.all.select { _1.ppid == pid }.map { Process.new @snapshot, _1 }
end

#open_filesObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/w-stdlib/processes.rb', line 49

def open_files
  raise unless pid.is_numeric?
  `lsof -p #{pid}`
    .lines
    .drop(1)
    .map { _1.strip.scan(/(\/.*$)/).first&.first }
    .reject(&:nil?)
    .uniq
    .select { File.file? _1 }
    .map { File.new _1 }
end

#parentObject



43
44
45
46
47
# File 'lib/w-stdlib/processes.rb', line 43

def parent
  proc = @snapshot.for_pid(self.ppid)
  return nil unless proc
  Processes::Process.new @snapshot, proc
end

#sig_killObject



61
62
63
64
# File 'lib/w-stdlib/processes.rb', line 61

def sig_kill
  raise unless pid.is_numeric?
  `kill -9 #{pid}`
end