Class: MogilefsdLogTailer::TailHandler

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/mogilefsd_log_tailer/tail_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostname, port, file) ⇒ TailHandler

Returns a new instance of TailHandler.



6
7
8
9
10
11
12
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 6

def initialize(hostname, port, file)
  @hostname = hostname
  @port = port.to_i
  @received_data = ''
  @file = file
  @reconnect_wait = 1
end

Instance Method Details

#connection_completedObject



27
28
29
30
31
32
33
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 27

def connection_completed
  send_data("!watch\r\n")
  @reconnect_wait = 1
rescue
  $stderr.puts "Exception in connection_completed: %s (%p)\n\t%s" %
    [ $!.message, $!.class, $!.backtrace.join("\n\t") ]
end

#post_initObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 14

def post_init
  @received_data = ''
  # pn = get_peername
  # if pn.nil?
  #   @port, @ip = [ "unknown", -1 ]
  # else
  #   @port, @ip = Socket.unpack_sockaddr_in(get_peername)
  # end
rescue
  $stderr.puts "Exception in post_init: %s (%p)\n\t%s" %
    [ $!.message, $!.class, $!.backtrace.join("\n\t") ]
end

#print_log_entry(line) ⇒ Object



80
81
82
83
84
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 80

def print_log_entry(line)
  msg = "#{Time.now.to_s}: #{@hostname}: #{line}"
  @file.puts(msg)
  @file.flush
end

#receive_data(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 35

def receive_data(data)
  # puts(" - Received #{data.inspect} from #{@hostname}")
  if data =~ /\r\n/
    lines = data.split("\r\n", -1)
    lines.each_with_index do |line, i|
      if i == 0
        print_log_entry(@received_data + line)
        @received_data = ''
      elsif i == lines.size - 1
        @received_data << line
      else
        print_log_entry(line)
      end
    end
  else
    @received_data << data
  end
rescue
  $stderr.puts "Exception in receive_data: %s (%p)\n\t%s" %
    [ $!.message, $!.class, $!.backtrace.join("\n\t") ]
end

#unbindObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mogilefsd_log_tailer/tail_handler.rb', line 57

def unbind
  print_log_entry "[mogilefsd_log_tailer] Connection closed to #{@hostname}:#{@port}, reconnecting after #{@reconnect_wait} seconds."
  EventMachine::Timer.new(@reconnect_wait) do
    print_log_entry "[mogilefsd_log_tailer] Reconnecting to #{@hostname}:#{@port}..."
    reconnect(@hostname, @port)
  end

  @reconnect_wait *= 2
  if @reconnect_wait > 120
    @reconnect_wait = 120
  end

  # print_log_entry "[mogilefsd_log_tailer] Giving up on #{@hostname}:#{@port}"
  # EventMachine::Timer.new(0.5) do
  #   if EventMachine.connection_count == 0
  #     EventMachine.stop_event_loop
  #   end
  # end
rescue
  $stderr.puts "Exception in unbind: %s (%p)\n\t%s" %
    [ $!.message, $!.class, $!.backtrace.join("\n\t") ]
end