Watching a Directory in Ruby

watching a directory in ruby

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

How to monitor individual files with Ruby listen gem?

I wasn't paying attention to what I was doing here. only: clearly takes a regex, which I was obviously failing to construct and provide. Hopefully this can help someone else out who may need to use listen to watch or ignore specific files.

Here is an example of my working solution:

module FileTracker
filedir = "/home/user/dir"
filenames = [ "file1", "file2" ]

listenRegex = filenames.join("$|").gsub!(".", "\.") + "$"

# Should only track 'file1' and 'file2' in this directory
listener = Listen.to(filedir, only: /#{listenRegex}/) do |modified, added, removed|
puts "Updated: " + modified.first
end

listener.start
sleep
end

Listen error: unable to monitor directories for changes

1000 is way too small, try with 524288 as explained in the wiki page: https://github.com/guard/listen/blob/master/README.md#increasing-the-amount-of-inotify-watchers

Listen uses inotify by default on Linux to monitor directories for
changes. It's not uncommon to encounter a system limit on the number
of files you can monitor. For example, Ubuntu Lucid's (64bit) inotify
limit is set to 8192.

and

If you are running Debian, RedHat, or another similar Linux
distribution, run the following in a terminal:

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

If you are running ArchLinux, run the following command instead

echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

Running filewatcher as separate process

This question should probably be split into two, one about running filewatcher as a background process, and another about managing processed files, but as far as filewatcher goes, a simple solution would be to use the foreman gem with a Procfile.

You can start your Rails app in one process and filewatcher in another with a Procfile in the root of your app, like this:

# Procfile
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
filewatcher: filewatcher "**/*.txt" "bundle exec rake process_txt_files"

and move whatever processing needs to be done into a rake task. With this you could just run foreman start locally to start both processes, and if your production server supports Procfiles (like Heroku, for example), this makes it easy to do the same in production.

Configuring guard to monitor controller sub-directories

Well, @rainkinz had it right. There was a typo in the specfile name that I couldn't see. I used the -d switch when running guard which printed debug statements that brought the error to my attention.



Related Topics



Leave a reply



Submit