Class: Console::Mux::Process

Inherits:
Object
  • Object
show all
Includes:
Events, Log4r
Defined in:
lib/console/mux/process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Events

#event_handlers, #on

Constructor Details

#initialize(command, name = nil) ⇒ Process

Returns a new instance of Process.



40
41
42
43
44
45
# File 'lib/console/mux/process.rb', line 40

def initialize(command, name = nil)
  @command = command
  @name = name
  @logger = Logger["process::#{name}"] || Logger.new("process::#{name}")
  start
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



34
35
36
# File 'lib/console/mux/process.rb', line 34

def command
  @command
end

#loggerObject (readonly)

Returns the value of attribute logger.



34
35
36
# File 'lib/console/mux/process.rb', line 34

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/console/mux/process.rb', line 34

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



34
35
36
# File 'lib/console/mux/process.rb', line 34

def pid
  @pid
end

#started_atObject (readonly)

Returns the value of attribute started_at.



34
35
36
# File 'lib/console/mux/process.rb', line 34

def started_at
  @started_at
end

Instance Method Details

#cputimeString

Returns cputime in.

Returns:



100
101
102
# File 'lib/console/mux/process.rb', line 100

def cputime
  ps1('cputime')
end

#etimeString

Returns elapsed time.

Returns:



90
91
92
# File 'lib/console/mux/process.rb', line 90

def etime
  ps1('etime')
end

#ps1(field) ⇒ Object

Capture the output of a single field using the ‘ps’ command.



85
86
87
# File 'lib/console/mux/process.rb', line 85

def ps1(field)
  `ps -o #{field}= -p #{pid}`.strip
end

#receive_line(line) ⇒ Object



75
76
77
# File 'lib/console/mux/process.rb', line 75

def receive_line(line)
  logger.info { line }
end

#rssInteger

Returns resident size in bytes.

Returns:

  • (Integer)

    resident size in bytes



95
96
97
# File 'lib/console/mux/process.rb', line 95

def rss
  ps1('rss').to_i * 1024
end

#running?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/console/mux/process.rb', line 53

def running?
  @handler != nil
end

#stop(&block) ⇒ Object



47
48
49
50
51
# File 'lib/console/mux/process.rb', line 47

def stop(&block)
  if @handler
    @handler.detach
  end
end

#to_sObject



61
62
63
# File 'lib/console/mux/process.rb', line 61

def to_s
  "#{command} (#{running? ? pid : 'stopped'})"
end

#unbindObject

Called by the child handler



66
67
68
69
70
71
72
73
# File 'lib/console/mux/process.rb', line 66

def unbind
  pid2, status = ::Process.waitpid2(pid, ::Process::WNOHANG)
  if pid2
    on_exit(status)
  else
    try_kill(['INT', 'TERM', 'KILL'])
  end
end

#uptimeInteger

Returns uptime in seconds.

Returns:

  • (Integer)

    uptime in seconds



80
81
82
# File 'lib/console/mux/process.rb', line 80

def uptime
  Time.now.to_i - started_at.to_i
end