Class: Tidings::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/tidings/watcher.rb

Overview

Starts a file watcher in a separate process.

Instance Method Summary collapse

Constructor Details

#initialize(path, processor) ⇒ Watcher



6
7
8
9
10
# File 'lib/tidings/watcher.rb', line 6

def initialize(path, processor)
  @path = path
  @processor = processor
  @pid = nil
end

Instance Method Details

#startObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tidings/watcher.rb', line 12

def start
  Signal.trap('HUP') { stop }
  Signal.trap('SIGINT') { stop }
  @pid = fork do
    begin
      watch
    rescue Exception => e
      Tidings.log(
        "Tidings encountered an uncaught exeption: #{e.message}"
      )
      exit
    end
  end
  Tidings.log("Tidings running on PID: #{@pid}")
  Process.waitpid(@pid)
  Tidings.log('Tidings stopped running')
end

#stopObject



30
31
32
33
# File 'lib/tidings/watcher.rb', line 30

def stop
  Process.kill('KILL', @pid)
rescue Errno::ESRCH
end

#watchObject



36
37
38
# File 'lib/tidings/watcher.rb', line 36

def watch
  FSEvent.watch(@path, @processor)
end