Module: RunLoop::Fifo

Defined in:
lib/run_loop/fifo.rb

Defined Under Namespace

Classes: NoReaderConfiguredError, WriteTimedOut

Constant Summary collapse

BUFFER_SIZE =
4096

Class Method Summary collapse

Class Method Details

.write(pipe, msg, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/run_loop/fifo.rb', line 12

def self.write(pipe, msg, options={})
  msg = "#{msg}\n"
  timeout = options[:timeout] || 10
  begin_at = Time.now
  begin
    open(pipe, File::WRONLY | File::NONBLOCK) do |pipe_io|
      bytes_written = 0
      bytes_to_write = msg.length
      until bytes_written >= bytes_to_write do
        begin
          wrote = pipe_io.write_nonblock msg
          bytes_written += wrote
          msg = msg[wrote..-1]
        rescue IO::WaitWritable, Errno::EINTR
          timeout_left = timeout - (Time.now - begin_at)
          raise WriteTimedOut if timeout_left <= 0
          IO.select nil, [pipe_io], nil, timeout_left
        end
      end
    end
  rescue Errno::ENXIO
    sleep(0.5)
    timeout_left = timeout - (Time.now - begin_at)
    raise NoReaderConfiguredError if timeout_left <= 0
    retry
  end
end