watching a directory in ruby

不羁岁月 提交于 2019-12-29 03:34:53

问题


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


回答1:


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




回答2:


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|



回答3:


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



回答4:


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



回答5:


https://github.com/mynyml/watchr

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




回答6:


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://stackoverflow.com/questions/4747344/watching-a-directory-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!