watching a directory in ruby

后端 未结 6 1481
半阙折子戏
半阙折子戏 2020-12-14 01:04

We have an application that needs to process incoming files that are dropped into a directory. I am looking for the best way to do this.

We have been using a loopin

相关标签:
6条回答
  • 2020-12-14 01:42

    Thanks @emerge, as a relative newbie to rails I wanted to watch for files in my Rails app and not from the command line. Compared to the other options here, found that Listen was an incredibly simple 2 steps:

    1. Added this to the gem file:

      gem 'listen', '~> 2.0'
      
    2. Then added this in Application.rb to execute on app startup:

      listener = Listen.to('public/json_import') do |added| 
        puts "added absolute path: #{added}"
      end
      listener.start # not blocking
      

    We can also listen to multiple dirs, and also modify/add/remove:

    listener = Listen.to('dir/to/listen', 'dir/to/listen2') do |modified, added, removed|
    
    0 讨论(0)
  • 2020-12-14 01:42

    There's also the tiny filewatcher rubygem. The gem has no dependencies, contains no platform specific code and simply detects updates, delitions and additions by polling.

    require 'filewatcher'
    
    FileWatcher.new(["directory"]).watch() do |filename, event|
      if(event == :changed)
        puts "File updated: " + filename
      end
      if(event == :delete)
        puts "File deleted: " + filename
      end
      if(event == :new)
        puts "Added file: " + filename
      end
    end
    
    0 讨论(0)
  • 2020-12-14 01:51

    Three old-school options that I know of:

    Ara T. Howard's DirWatch:

    • Docs: http://codeforpeople.com/lib/ruby/dirwatch/dirwatch-0.9.0/README
    • Download: http://codeforpeople.com/lib/ruby/dirwatch/dirwatch-0.9.0.tgz

    My own DirectoryWatcher:

    • Docs: http://phrogz.net/RubyLibs/rdoc/files/DirectoryWatcher_rb.html
    • Download: http://phrogz.net/RubyLibs/DirectoryWatcher.rb

    Paul Horman's FileSystemWatcher:

    • Docs: http://paulhorman.com/filesystemwatcher/
    • Download: http://paulhorman.com/filesystemwatcher/FileSystemWatcher.1.0.0.zip
    0 讨论(0)
  • 2020-12-14 01:56

    And there's also guard:

    Guard automates various tasks by running custom rules whenever file or directories are modified.

    It's frequently used by software developers, web designers, writers and other specialists to avoid mundane, repetitive actions and commands such as "relaunching" tools after changing source files or configurations.

    Common use cases include: an IDE replacement, web development tools, designing "smart" and "responsive" build systems/workflows, automating various project tasks and installing/monitoring various system services...

    0 讨论(0)
  • 2020-12-14 01:57

    https://github.com/mynyml/watchr

    That's typically used for running unit test automatically but should suit your needs too.

    0 讨论(0)
  • 2020-12-14 02:00

    I think https://github.com/nex3/rb-inotify should work for you. An example to use this gem

    require 'rb-inotify'
    notifier = INotify::Notifier.new
    notifier.watch("/tmp", :moved_to, :create) do |event|
        puts "#{event.absolute_name} is now in path /tmp!"
    end
    notifier.run
    
    0 讨论(0)
提交回复
热议问题