class Rerun::Watcher

Constants

CREATED
DELETED
MODIFIED

Attributes

directories[R]
priority[RW]
sleep_time[RW]

Public Class Methods

new(&client_callback) click to toggle source
# File lib/rerun/watcher.rb, line 18
def initialize(&client_callback)
  @client_callback = client_callback

  @sleep_time = 1
  @priority = 0

  @directories = []
  @files = []

  @found = nil
  @first_time = true
  @thread = nil

end

Public Instance Methods

add_directory(dir, expression="**/*") click to toggle source

add a directory to be watched @param dir the directory to watch @param expression the glob pattern to search under the watched directory

# File lib/rerun/watcher.rb, line 36
def add_directory(dir, expression="**/*")
  if FileTest.exists?(dir) && FileTest.readable?(dir) then
    @directories << Directory.new(dir, expression)
  else
    raise InvalidDirectoryError, "Dir '#{dir}' either doesnt exist or isnt readable"
  end
end
add_file(file) click to toggle source

add a specific file to the watch list @param file the file to watch

# File lib/rerun/watcher.rb, line 50
def add_file(file)
  if FileTest.exists?(file) && FileTest.readable?(file) then
    @files << file
  else
    raise InvalidFileError, "File '#{file}' either doesnt exist or isnt readable"
  end
end
join() click to toggle source

wait for the filewatcher to finish

# File lib/rerun/watcher.rb, line 104
def join
  @thread.join() if @thread
rescue Interrupt => e
  # don't care
end
prime() click to toggle source
# File lib/rerun/watcher.rb, line 62
def prime
  @first_time = true
  @found = Hash.new()
  examine
  @first_time = false
end
remove_directory(dir) click to toggle source
# File lib/rerun/watcher.rb, line 44
def remove_directory(dir)
  @directories.delete(dir)
end
remove_file(file) click to toggle source
# File lib/rerun/watcher.rb, line 58
def remove_file(file)
  @files.delete(file)
end
start() click to toggle source
# File lib/rerun/watcher.rb, line 69
def start
  if @thread then
    raise RuntimeError, "already started"
  end

  prime

  @thread = Thread.new do
    while true do
      examine
      sleep(@sleep_time)
    end
  end

  @thread.priority = @priority

  at_exit { stop } #?

end
stop() click to toggle source

kill the filewatcher thread

# File lib/rerun/watcher.rb, line 90
def stop
  begin
    @thread.wakeup
  rescue ThreadError => e
    # ignore
  end
  begin
    @thread.kill
  rescue ThreadError => e
    # ignore
  end
end