Module: Vidibus::WatchFolder

Extended by:
WatchFolder
Included in:
WatchFolder
Defined in:
lib/vidibus/watch_folder.rb,
lib/vidibus/watch_folder/job.rb,
lib/vidibus/watch_folder/base.rb,
lib/vidibus/watch_folder/daemon.rb,
lib/vidibus/watch_folder/railtie.rb,
lib/vidibus/watch_folder/version.rb,
lib/vidibus/watch_folder/util/directory.rb

Defined Under Namespace

Modules: Util Classes: Base, Daemon, Error, Job, NoRootsError, Railtie

Constant Summary collapse

EVENTS =
%w[added modified removed]
VERSION =
'0.1.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#autoload_pathsObject

Returns the value of attribute autoload_paths.



17
18
19
# File 'lib/vidibus/watch_folder.rb', line 17

def autoload_paths
  @autoload_paths
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/vidibus/watch_folder.rb', line 17

def logger
  @logger
end

#path_mappingObject

Returns the value of attribute path_mapping.



17
18
19
# File 'lib/vidibus/watch_folder.rb', line 17

def path_mapping
  @path_mapping
end

#rootsObject

Returns the value of attribute roots.



17
18
19
# File 'lib/vidibus/watch_folder.rb', line 17

def roots
  @roots
end

Instance Method Details

#autoloadObject

Constantize all watch folder class names to trigger autoloading.



58
59
60
61
62
63
# File 'lib/vidibus/watch_folder.rb', line 58

def autoload
  return unless autoload_paths.any?
  list = Dir[*autoload_paths].map do |f|
    File.read(f)[/class ([^<]+) < Vidibus::WatchFolder::Base/, 1]
  end.compact.map { |k| k.constantize }
end

#checksum(path) ⇒ Object

Calculate checksum of given file path



24
25
26
# File 'lib/vidibus/watch_folder.rb', line 24

def checksum(path)
  Digest::SHA2.file(path).hexdigest
end

#listenObject

Listen for changes within all roots



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vidibus/watch_folder.rb', line 29

def listen
  autoload
  unless roots.any?
    raise NoRootsError, 'No folders to watch!'
  end
  roots.uniq!
  logger.debug("[#{Time.now.utc}] - Listening to #{roots.join(',')}")
  args = roots + [{:latency => 0.1}]
  Listen.to(*args) do |modified, added, removed|
    EVENTS.each do |event|
      eval(event).each do |path|
        logger.debug %([#{Time.now.utc}] - #{event}: #{path})
        begin
          uuid = path[/^#{roots_regex}\/([^\/]+)\/.+$/, 1] || next
          begin
            base = Base.find_by_uuid(uuid)
            base.handle(event, path)
          rescue Mongoid::Errors::DocumentNotFound
            logger.error %([#{Time.now.utc}] - Can't find Vidibus::WatchFolder::Base #{uuid})
          end
        rescue => e
          logger.error("[#{Time.now.utc}] - ERROR in Vidibus::WatchFolder.listen:\n#{e.inspect}\n---\n#{e.backtrace.join("\n")}")
        end
      end
    end
  end
end