Class: VagrantPlugins::GatlingRsync::ListenOSX

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-gatling-rsync/listen/listenosx.rb

Instance Method Summary collapse

Constructor Details

#initialize(paths, ignores, latency, logger, callback) ⇒ ListenOSX

Returns a new instance of ListenOSX.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vagrant-gatling-rsync/listen/listenosx.rb', line 6

def initialize(paths, ignores, latency, logger, callback)
  @paths = paths
  @ignores = ignores
  @latency = latency
  @options = {
    # We set this to a small value to ensure that we can coalesce the
    # events together to prevent rsyncing too often under heavy write
    # load.
    :latency => 0.1,
    :no_defer => false,
  }
  @logger = logger
  @callback = callback
end

Instance Method Details

#runObject



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
# File 'lib/vagrant-gatling-rsync/listen/listenosx.rb', line 21

def run
  @logger.info("Listening via: rb-fsevent on Mac OS X.")
  changes = Queue.new

  fsevent = FSEvent.new
  fsevent.watch @paths.keys, @options do |directories|
    directories.each { |d| changes << d }
  end
  Thread.new { fsevent.run }

  loop do
    directories = Set.new
    begin
      loop do
        @logger.info("Starting the timeout at #{Time.now.to_s}.")
        change = Timeout::timeout(@latency) {
          changes.pop
        }
        directories << change unless change.nil?
      end
    rescue Timeout::Error, ThreadError
      @logger.info("Breaking out of the loop at #{Time.now.to_s}.")
    end

    @logger.info("Detected changes to #{directories.inspect}.") unless directories.empty?

    @callback.call(@paths, @ignores, directories) unless directories.empty?
  end
end