Class: Guard::Processing::ProcessController

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/processing.rb

Instance Method Summary collapse

Constructor Details

#initialize(pid_dir, path) ⇒ ProcessController

Returns a new instance of ProcessController.



23
24
25
26
# File 'lib/guard/processing.rb', line 23

def initialize(pid_dir, path)
  @pid_dir = pid_dir
  @path = path
end

Instance Method Details

#killObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/guard/processing.rb', line 38

def kill
  if pid
    begin
      Process.kill('KILL', -Process.getpgid(pid))
    rescue Errno::ESRCH
      # Process does not exist
    ensure
      File.delete(pid_file_path)
    end
  end
end

#pidObject



32
33
34
35
36
# File 'lib/guard/processing.rb', line 32

def pid
  if File.exists?(pid_file_path)
    File.read(pid_file_path).to_i
  end
end

#pid_file_pathObject



28
29
30
# File 'lib/guard/processing.rb', line 28

def pid_file_path
  @pid_fiie_path ||= File.join(@pid_dir, @path.basename)
end

#run(command) ⇒ Object



58
59
60
61
62
# File 'lib/guard/processing.rb', line 58

def run(command)
  kill
  pid = spawn(command)
  save(pid)
end

#save(pid) ⇒ Object



50
51
52
# File 'lib/guard/processing.rb', line 50

def save(pid)
  open(pid_file_path, 'w') {|f| f.print Process.getpgid(pid) }
end

#spawn(command) ⇒ Object



54
55
56
# File 'lib/guard/processing.rb', line 54

def spawn(command)
  Process.spawn({}, command, :pgroup => true)
end