Class: Franz::Discover

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

Overview

Discover performs half of file existence detection by expanding globs and keeping track of files known to Franz. Discover requires a deletions Queue to maintain this state, so it’s fairly useless without a Watch.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Discover

Start a new Discover thread in the background.

Parameters:

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

    options for the discovery

Options Hash (opts):

  • :input_config (InputConfig)

    shared Franz configuration

  • :discoveries (Queue) — default: []

    “output” queue of discovered paths

  • :deletions (Queue) — default: []

    “input” queue of deleted paths

  • :discover_interval (Integer) — default: 5

    seconds between discover rounds

  • :known (Array<Path>) — default: []

    internal “known” state

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

    logger to use



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
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/franz/discover.rb', line 22

def initialize opts={}
  @ic = opts[:input_config] || raise('No input_config specified')

  @discoveries = opts[:discoveries] || []
  @deletions   = opts[:deletions]   || []

  @discover_interval = opts[:discover_interval] || 30
  @known             = opts[:known]             || []
  @logger            = opts[:logger]            || Logger.new(STDOUT)

  @known = Set.new(@known)

  @configs = @ic.configs.map do |config|
    config[:includes] ||= []
    config[:excludes] ||= []
    config
  end

  @statz = opts[:statz] || Franz::Stats.new
  @statz.create :num_discovered, 0
  @statz.create :num_deleted, 0

  @stop = false

  @thread = Thread.new do
    until @stop
      until deletions.empty?
        d = deletions.pop
        @known.delete d
        @statz.inc :num_deleted
        log.debug \
          event: 'discover deleted',
          file: d
      end

      discover.each do |discovery|
        discoveries.push discovery
        @known.add discovery
        @statz.inc :num_discovered
        log.debug \
          event: 'discover discovered',
          file: discovery
      end
      sleep discover_interval
    end
  end

  log.debug \
    event: 'discover started',
    discoveries: discoveries,
    deletions: deletions,
    discover_interval: discover_interval
end

Instance Method Details

#stateObject

Return the internal “known” state



88
89
90
# File 'lib/franz/discover.rb', line 88

def state
  return @known.to_a
end

#stopArray

Stop the Discover thread. Effectively only once.

Returns:

  • (Array)

    internal “known” state



79
80
81
82
83
84
85
# File 'lib/franz/discover.rb', line 79

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