Class: EM::ProcessBuffer::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/eventmachine/process_buffer/watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid_file, pipe_directory, working_directory, *args) ⇒ Watcher

Returns a new instance of Watcher.



7
8
9
10
11
12
# File 'lib/eventmachine/process_buffer/watcher.rb', line 7

def initialize pid_file, pipe_directory, working_directory, *args
  @pid_file = pid_file
  @pipe_directory = pipe_directory
  @working_directory = working_directory
  @pid = File.read(pid_file).strip.to_i
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



5
6
7
# File 'lib/eventmachine/process_buffer/watcher.rb', line 5

def pid
  @pid
end

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



5
6
7
# File 'lib/eventmachine/process_buffer/watcher.rb', line 5

def pid_file
  @pid_file
end

#pipe_directoryObject (readonly)

Returns the value of attribute pipe_directory.



5
6
7
# File 'lib/eventmachine/process_buffer/watcher.rb', line 5

def pipe_directory
  @pipe_directory
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



5
6
7
# File 'lib/eventmachine/process_buffer/watcher.rb', line 5

def working_directory
  @working_directory
end

Instance Method Details

#attach_to_processObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eventmachine/process_buffer/watcher.rb', line 40

def attach_to_process
  EM.attach(File.open(pipe_stdout, File::NONBLOCK + File::RDONLY), LineProcessor) do |lp|
    lp.on_line = method(:receive_stdout)
  end

  EM::PidPoller.on_exit pid, method(:process_exited)

  EM.next_tick {
    post_init
    process_watched
  }
end

#pipe_stdinObject



32
33
34
# File 'lib/eventmachine/process_buffer/watcher.rb', line 32

def pipe_stdin
  File.join pipe_directory, 'pipe_stdin'
end

#pipe_stdoutObject



36
37
38
# File 'lib/eventmachine/process_buffer/watcher.rb', line 36

def pipe_stdout
  File.join pipe_directory, 'pipe_stdout'
end

#post_initObject



14
15
# File 'lib/eventmachine/process_buffer/watcher.rb', line 14

def post_init
end

#process_exitedObject



28
29
30
# File 'lib/eventmachine/process_buffer/watcher.rb', line 28

def process_exited
  puts "watched process exited"
end

#process_startedObject



21
22
23
# File 'lib/eventmachine/process_buffer/watcher.rb', line 21

def process_started
  puts "watched process started"
end

#process_watchedObject



25
26
# File 'lib/eventmachine/process_buffer/watcher.rb', line 25

def process_watched
end

#receive_stdout(line) ⇒ Object



17
18
19
# File 'lib/eventmachine/process_buffer/watcher.rb', line 17

def receive_stdout line
  puts ">> #{line}"
end

#send_stdin(line) ⇒ Object



53
54
55
56
57
# File 'lib/eventmachine/process_buffer/watcher.rb', line 53

def send_stdin line
  File.open(pipe_stdin, 'w+') do |f|
    f.write "#{line}\n"
  end
end