How can I monitor individual files with Ruby listen gem?

I am trying to use listening sound ( https://github.com/guard/listen ) to listen to specific files, but so far they have managed to get the basic functionality to listen to entire directories correctly.

Here's an example of what I'm trying at the moment:

require "listen"

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

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

  listener.start
  sleep
end

      

I also tried just specifying one filename as a string for the parameter only:

, but that didn't work either.

My ultimate goal is to keep track of a list of custom files in different directories for changes. I only want to track each specific file, not the entire directory.

+3


source to share


2 answers


I was oblivious to what I was doing here. only:

explicitly accepts a regex, which I obviously couldn't build and provide. Hopefully this can help someone else who might need to use listen

to view or ignore certain files.

Here's 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

      

+4


source


Here are some tips you might find useful:

  • For quoting a regex correctly and using regex without capturing them:

    filenames = %w(foo.txt bar.js)
    listen_regex = /(?:#{file.map { |f| Regexp.quote(f) } * '|'})$
    
          

(Should be a little more reliable).

  1. More importantly, there are a few notes about Listen:

    • what listens physically for hours depends on the backend, for example. polling modes simply scan the given directories recursively (excluding files matching ignore regex), while for example rb-fsevent in macOSX watch directories and physically scans those directories for changes every time.

So there really is no such thing as "listening to files", because Listen works with directories and filters files (and sometimes directories) based on ignore rules.



This is important if you change files frequently, eg. log files or database files in the project directory because every time they change the changes are still triggered (except when polled) - you just won't see it because of ignoring.

  1. To see what's going on in Listen, there is a cool debug option: LISTEN_GEM_DEBUGGING

    (see README). Depending on the value ( 1

    or 2

    ) you see what system events are listening under the hood, what it ignores, and how long the stuff takes (see "Listen to the wiki" for more information).

Let me know if you have any feedback on the docs or the audition itself.

Have a nice day!

+1


source







All Articles