Class: IO::Tail::Process

Inherits:
Tailable show all
Defined in:
lib/io/tail/process.rb

Overview

A Process that’s run and tailed

Instance Attribute Summary collapse

Attributes inherited from Tailable

#break_if_eof, #default_bufsize, #interval, #max_interval, #reopen_deleted, #reopen_suspicious, #return_if_eof, #suspicious_interval

Instance Method Summary collapse

Methods inherited from Tailable

#after_reopen, #tail

Constructor Details

#initialize(command = nil) ⇒ Process

Returns a new instance of Process.



11
12
13
14
15
16
17
# File 'lib/io/tail/process.rb', line 11

def initialize(command = nil)
  super()
  if command
    @_command = command
    self.reopen_tailable
  end
end

Instance Attribute Details

#_commandObject

Returns the value of attribute _command.



8
9
10
# File 'lib/io/tail/process.rb', line 8

def _command
  @_command
end

#_processObject (readonly)

Returns the value of attribute _process.



9
10
11
# File 'lib/io/tail/process.rb', line 9

def _process
  @_process
end

Instance Method Details

#closeObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/io/tail/process.rb', line 46

def close
  return if !self._process
  # We have to do that to stop the IO
  self.kill_inner
  begin
    self._process.close
  rescue Exception => e
    # Ignore
  end
end

#handle_EOFErrorObject

Taialble process should never have a EOF unless they are no longer tailable



20
21
22
23
24
25
# File 'lib/io/tail/process.rb', line 20

def handle_EOFError
  # Attempt to reopen
  if @reopen_suspicious
    raise ReopenException
  end
end

#kill_innerObject

Used for testing purposes



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/io/tail/process.rb', line 35

def kill_inner
  return if !self._process
  killable = self._process.pid
  $stdout.flush
  begin
    ::Process.kill 'INT', killable
    ::Process.kill 'KILL', killable
  rescue Exception => e
    # Already killed ? Fine.
  end
end

#readlineObject



31
32
33
# File 'lib/io/tail/process.rb', line 31

def readline
  self._process.readline
end

#reopen_tailable(mode = 'dummy') ⇒ Object

Ignore the mode



28
29
30
# File 'lib/io/tail/process.rb', line 28

def reopen_tailable(mode = 'dummy')
  @_process = IO.popen(@_command) if @_command
end