Donald A Subert Donald A Subert - 2 years ago 138
Javascript Question

Node.js watch a directory for incoming archive by scp

I have a task to create a node.js script that listens in a directory for new archive files to arrive and process them.

I see that I can do this with

fs.watch
.

The files are
tar.gz
archives, that arrive via
scp
.

The problem is that the arrival of a new archive file seems to create multiple (the exact number is unpredictable) events in the file-system. The first is a
rename
, followed by some number of
change
events.

I need to reliably trigger my processing logic only once, when the archive is finished being transferred. How can this be done?

Additional notes:


  • I am not able to make changes to the system sending the archive, only
    the system receiving it.

  • I am not considering using elapsed time to
    guess that the scp event has concluded. That is not reliable.


Answer Source

Using the watch library you can do:

var watch = require('watch')
watch.createMonitor('/home/path', function (monitor) {
   monitor.on("created", function (file, stat) {
       // do work with new file
   })
}

I recommend taking advantage of the rich library ecosystem available to you because you're using the node platform. Some of these problems have already been solved for you!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download