Class: Franz::Tail

Inherits:
Object
  • Object
show all
Defined in:
lib/franz/tail.rb

Overview

Tail receives low-level file events from a Watch and handles the actual reading of files, providing a stream of lines.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Tail

Start a new Tail thread in the background.

Parameters:

  • opts (Hash) (defaults to: {})

    a complex Hash for tail configuration



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/franz/tail.rb', line 16

def initialize opts={}
  @watch_events = opts[:watch_events] || []
  @tail_events  = opts[:tail_events]  || []

  @read_limit = opts[:read_limit] || 10_240 # 100 KiB
  @line_limit = opts[:line_limit] || 10_240 # 10 KiB
  @block_size = opts[:block_size] || 32_768 # 32 KiB
  @cursors    = opts[:cursors]    || Hash.new
  @logger     = opts[:logger]     || Logger.new(STDOUT)

  @buffer = Hash.new { |h, k| h[k] = BufferedTokenizer.new("\n", @line_limit) }
  @stop   = false

  @tail_thread = Thread.new do
    handle(watch_events.shift) until @stop
  end

  @last_checkin = Time.new 0
  @checkin_interval = 60

  log.debug \
    event: 'tail started',
    watch_events: watch_events,
    tail_events: tail_events,
    block_size: block_size,
    opts: opts
end

Instance Method Details

#stateObject

Return the internal “cursors” state



57
58
59
# File 'lib/franz/tail.rb', line 57

def state
  return @cursors.dup
end

#stopHash

Stop the Tail thread. Effectively only once.

Returns:

  • (Hash)

    internal “cursors” state



47
48
49
50
51
52
53
54
# File 'lib/franz/tail.rb', line 47

def stop
  return state if @stop
  @stop = true
  @watch_thread.kill rescue nil
  @tail_thread.kill  rescue nil
  log.debug event: 'tail stopped'
  return state
end