Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 598
phil Avatar asked Jan 20 '11 12:01

phil


People also ask

What is filewatcher?

File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

What is directory in Ruby?

A directory is a location where files can be stored. For Ruby, the Dir class and the FileUtils module manages directories and the File class handles the files. Double dot (..) refers to the parent directory for directories and single dot(.) refers to the directory itself.

How do I find the path of a file in Ruby?

Pass a string to File. expand_path to generate the path to that file or directory. Relative paths will reference your current working directory, and paths prepended with ~ will use the owner's home directory.

How do I change directory in Ruby?

chdir : To change the current working directory, chdir method is used. In this method, you can simply pass the path to the directory where you want to move. The string parameter used in the chdir method is the absolute or relative path.


1 Answers

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| 
like image 138
msanjay Avatar answered Sep 21 '22 15:09

msanjay