开发者

watching a directory in ruby

开发者 https://www.devze.com 2023-02-05 23:26 出处:网络
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 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 looping Backgroundrb process, but, to be honest Backgroundrb is unreliable and we'd like to move away from it if possible.

Delayed_job doesn't seem to be for ongoing tasks but for one offs.

I've found DirectoryWatcher http://codeforpeople.rubyforge.org/directory_watcher/ which looks promising, but ideally we want to have some control over this and also be able to monitor if it is up or not.

So the requirements are:

  • run fo开发者_如何转开发rever
  • process files in order
  • be monitorable
  • have some sort of way of restarting it and ensuring it is up (God?)

Thanks for any input! This shouldn't be difficult and I am surprised I can't find someone else talking about this on the web as I would have thought that in business applications this was not uncommon.


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|


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...


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


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


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


https://github.com/mynyml/watchr

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

0

精彩评论

暂无评论...
验证码 换一张
取 消