Class: FileWatch::Watch

Inherits:
Object
  • Object
show all
Includes:
LogStash::Util::Loggable
Defined in:
lib/filewatch/watch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discoverer, watched_files_collection, settings) ⇒ Watch

Returns a new instance of Watch.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/filewatch/watch.rb', line 11

def initialize(discoverer, watched_files_collection, settings)
  @settings = settings
  # watch and iterate_on_state can be called from different threads.
  @lock = Mutex.new
  # we need to be threadsafe about the quit mutation
  @quit = false
  @quit_lock = Mutex.new
  @lastwarn_max_files = 0
  @discoverer = discoverer
  @watched_files_collection = watched_files_collection
end

Instance Attribute Details

#discovererObject (readonly)

Returns the value of attribute discoverer.



9
10
11
# File 'lib/filewatch/watch.rb', line 9

def discoverer
  @discoverer
end

#lastwarn_max_filesObject

Returns the value of attribute lastwarn_max_files.



8
9
10
# File 'lib/filewatch/watch.rb', line 8

def lastwarn_max_files
  @lastwarn_max_files
end

#watched_files_collectionObject (readonly)

Returns the value of attribute watched_files_collection.



9
10
11
# File 'lib/filewatch/watch.rb', line 9

def watched_files_collection
  @watched_files_collection
end

Instance Method Details

#add_processor(processor) ⇒ Object



23
24
25
26
27
# File 'lib/filewatch/watch.rb', line 23

def add_processor(processor)
  @processor = processor
  @processor.add_watch(self)
  self
end

#discoverObject



37
38
39
40
41
42
43
# File 'lib/filewatch/watch.rb', line 37

def discover
  synchronized do
    @discoverer.discover
  end
  # don't return whatever @discoverer.discover returns
  return true
end

#iterate_on_stateObject

Read mode processor will handle watched_files in the closed, ignored, watched and active state differently from Tail mode - see the ReadMode::Processor and TailMode::Processor



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/filewatch/watch.rb', line 67

def iterate_on_state
  return if @watched_files_collection.empty?
  synchronized do
    begin
      # creates this snapshot of watched_file values just once
      watched_files = @watched_files_collection.values
      @processor.process_closed(watched_files)
      return if quit?
      @processor.process_ignored(watched_files)
      return if quit?
      @processor.process_watched(watched_files)
      return if quit?
      @processor.process_active(watched_files)
    ensure
      @watched_files_collection.delete(@processor.deletable_filepaths)
      @processor.deletable_filepaths.clear
    end
  end
end

#quitObject

def each



87
88
89
90
91
# File 'lib/filewatch/watch.rb', line 87

def quit
  @quit_lock.synchronize do
    @quit = true
  end
end

#quit?Boolean

def quit

Returns:

  • (Boolean)


93
94
95
# File 'lib/filewatch/watch.rb', line 93

def quit?
  @quit_lock.synchronize { @quit }
end

#subscribe(observer, sincedb_collection) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/filewatch/watch.rb', line 45

def subscribe(observer, sincedb_collection)
  @processor.initialize_handlers(sincedb_collection, observer)

  glob = 0
  interval = @settings.discover_interval
  reset_quit
  until quit?
    iterate_on_state
    break if quit?
    glob += 1
    if glob == interval
      discover
      glob = 0
    end
    break if quit?
    sleep(@settings.stat_interval)
  end
  @watched_files_collection.close_all
end

#watch(path) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/filewatch/watch.rb', line 29

def watch(path)
  synchronized do
    @discoverer.add_path(path)
  end
  # don't return whatever @discoverer.add_path returns
  return true
end