class Nite::Owl::NiteOwl

Public Class Methods

new() click to toggle source
Calls superclass method Nite::Owl::Action::new
# File lib/nite/owl/niteowl.rb, line 441
def initialize
  super()
  @workers_thread = nil
  @queue = Queue.new
end

Public Instance Methods

eval_watch(dir, code) click to toggle source
# File lib/nite/owl/niteowl.rb, line 464
def eval_watch(dir, code)
  Dir.chdir dir
  eval(code)
  if @actions.empty?
    puts "No actions configured in STDIN input stream"
    exit 1
  end
end
start() click to toggle source
# File lib/nite/owl/niteowl.rb, line 477
def start
  @workers_thread = Thread.new {
    begin
      interval = 0.1
      event_interval = 0.5
      last_event_time = nil
      next_time = Time.now.to_f+interval
      events = {}
      while true
        until @queue.empty?
          event = @queue.pop(true) rescue nil
          if event
            name = event[0]
            flags = event[1]
            if events[name]
              new_flags = events[name] + flags
              if new_flags == [:delete, :create, :modify]
                new_flags = [:modify]
              end
              events[name] = new_flags
            else
              events[name] = flags
            end
            last_event_time = Time.now.to_f
          end
        end
        if last_event_time && Time.now.to_f >= (last_event_time+event_interval)
          events.each do |name,flags|
            begin
              Nite::Owl::NiteOwl.instance.call(name,flags.uniq)
            rescue Exception => e
              puts e.message
              puts e.backtrace
            end
          end
          events.clear
          last_event_time = nil
        end
        Action.call_all_deferred_actions()
        delay = next_time - Time.now.to_f
        if delay > 0
          sleep(delay)
        end
        next_time = Time.now.to_f+interval
      end
    rescue Exception => e
      puts e.message
      puts e.backtrace
    end
  }
  # start platform specific notifier
  pwd = Dir.pwd
  if RUBY_PLATFORM =~ /linux/
    require 'rb-inotify'
    puts "Nite Owl watching: #{pwd}"
    notifier = INotify::Notifier.new
    notifier.watch(pwd, :recursive, :modify, :create, :delete, :move) do |event|
      name = event.absolute_name.slice(pwd.size+1,event.absolute_name.size-pwd.size)
      flags = event.flags do |f|
        if f == :moved_to or f == :moved_from
          :rename
        else
          f
        end
      end
      @queue << [name,flags]
    end
    begin
      notifier.run
    rescue Interrupt => e
    end
  elsif RUBY_PLATFORM =~ /darwin/
    require 'rb-fsevent'
    puts "Nite Owl watching: #{pwd}"
    fsevent = FSEvent.new
    fsevent.watch pwd,{:file_events => true} do |paths, event_meta|
      event_meta['events'].each do |event|
        name = event['path']
        name = name.slice(pwd.size+1,name.size-pwd.size)
        flags = event['flags'].map do |f|
          if f == 'ItemCreated'
            :create
          elsif f == 'ItemModified'
            :modify
          elsif f == 'ItemRemoved'
            :delete
          elsif f == 'ItemRenamed'
            :rename
          end
        end.keep_if {|f| f}
        @queue << [name,flags]
      end
    end
    fsevent.run
  else
    puts "Platform unsupported"
    exit(1)
  end
end
watch(dir) click to toggle source
# File lib/nite/owl/niteowl.rb, line 447
def watch(dir)
  Dir.chdir dir
  file = "Niteowl"
  unless File.file?(file) && File.readable?(file)
    file = "Niteowl.rb"
  end
  unless File.file?(file) && File.readable?(file)
    puts "No Niteowl[.rb] file found in: #{dir}"
    exit 1
  end
  load file
  if @actions.empty?
    puts "No actions configured in Niteowl file: #{dir}/#{file}"
    exit 1
  end
end
whenever(files) click to toggle source
# File lib/nite/owl/niteowl.rb, line 473
def whenever(files)
  add(NameIs.new(files))
end