Class: Franz::Watch

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

Overview

Watch works in tandem with Discover to maintain a list of known files and their status. Events are generated when a file is created, destroyed, or modified (including appended, truncated, and replaced).

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Watch

Start a new Watch thread in the background.

Parameters:

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

    options for the watch

Options Hash (opts):

  • :discoveries (Queue) — default: []

    “input” queue of discovered paths

  • :deletions (Queue) — default: []

    “output” queue of deleted paths

  • :watch_events (Queue) — default: []

    “output” queue of file events

  • :watch_interval (Fixnum) — default: 1

    seconds between watch rounds

  • :stats (Hash<Path,State>) — default: []

    internal “stats” state

  • :logger (Logger) — default: Logger.new(STDOUT)

    logger to use



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/franz/watch.rb', line 21

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

  @play_catchup   = opts[:play_catchup?].nil? ? true : opts[:play_catchup?]
  @watch_interval = opts[:watch_interval] || 10
  @stats          = opts[:stats]          || Hash.new
  @logger         = opts[:logger]         || Logger.new(STDOUT)

  # Make sure we're up-to-date by rewinding our old stats to our cursors
  if @play_catchup
    log.debug event: 'play catchup'
    stats.keys.each do |path|
      stats[path][:size] = opts[:full_state][path][:cursor] || 0
    end
  end

  @stop = false
  @thread = Thread.new do
    until @stop
      until discoveries.empty?
        @stats[discoveries.shift] = nil
      end

      watch.each do |deleted|
        @stats.delete deleted
        deletions.push deleted
      end

      sleep watch_interval
    end
  end

  log.debug \
    event: 'watch started',
    discoveries: discoveries,
    deletions: deletions,
    watch_events: watch_events,
    watch_interval: watch_interval,
    opts: opts
end

Instance Method Details

#stateObject

Return the internal “stats” state



76
77
78
# File 'lib/franz/watch.rb', line 76

def state
  return @stats.dup
end

#stopHash

Stop the Watch thread. Effectively only once.

Returns:

  • (Hash)

    internal “stats” state



67
68
69
70
71
72
73
# File 'lib/franz/watch.rb', line 67

def stop
  return state if @stop
  @stop = true
  @thread.kill
  log.debug event: 'watch stopped'
  return state
end