Module: EM::ProcessBuffer

Defined in:
lib/eventmachine/process_buffer.rb,
lib/eventmachine/process_buffer/version.rb,
lib/eventmachine/process_buffer/watcher.rb

Defined Under Namespace

Classes: Watcher

Constant Summary collapse

BIN =
File.expand_path File.join(__FILE__, '../../../bin')
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.attach_to_process(pid, pipe_directory, working_directory, pid_file, command, handler, *args) {|watcher| ... } ⇒ Object

Yields:

  • (watcher)


56
57
58
59
60
61
62
63
# File 'lib/eventmachine/process_buffer.rb', line 56

def self.attach_to_process pid, pipe_directory, working_directory, pid_file, command, handler, *args
  c = Class.new(EM::ProcessBuffer::Watcher) { include handler }
  watcher = c.new pid_file, pipe_directory, working_directory, *args
  watcher.attach_to_process
  watcher.process_reattached

  yield watcher if block_given?
end

.spawn_detached(cmd, environment = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/eventmachine/process_buffer.rb', line 13

def self.spawn_detached cmd, environment = {}
  begin
    pid, stdin, stdout, stderr = POSIX::Spawn::popen4(environment, cmd)
    Process.detach pid
    return pid, stdin, stdout, stderr
  rescue Errno::EAGAIN # Resource temporarily unavailable
    retry
  end
end

.start_process(pipe_directory, working_directory, pid_file, command, environment, handler, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/eventmachine/process_buffer.rb', line 23

def self.start_process pipe_directory, working_directory, pid_file, command, environment, handler, *args, &block
  buffer_command = %Q{#{BIN}/buffer-process -d #{pipe_directory} -C #{working_directory} -p #{pid_file} "#{command}"}
  buffer_pid, stdin, stdout, stderr = spawn_detached buffer_command, environment
  Process.detach buffer_pid

  wait = EM.add_periodic_timer(0.5) do
    begin
      if Process.waitpid(buffer_pid, Process::WNOHANG)
        stdin.close
        stdout.gets
        wait.cancel

        puts "process exited pid=#{buffer_pid}"

      elsif File.exist?(pid_file)
        stdin.close
        stdout.gets
        wait.cancel

        c = Class.new(EM::ProcessBuffer::Watcher) { include handler }
        watcher = c.new pid_file, pipe_directory, working_directory, *args
        
        watcher.process_started
        watcher.attach_to_process
        
        block.call watcher if block_given?
      end
    rescue Errno::ECHILD
      # waiting...
    end
  end
end