Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

watching a directory in ruby

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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|

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...