Watch events open,change,unlink on all files that are refrenced or become refrenced by path
provide events for any file descriptors that are referenced by a watched path, or were referenced by a watched path for as long as they are still changing. active is defined by a timeout since last event. file descriptors that become inactive are removed.
npm install watchfd
var watchfd = require('watchfd').watch;
watchfd('/some.log',function(cur,prev){
console.log(prev.size,' changed to ',cur.size);
});
an issue with log/file forwarding utilities currently available in npm is that they only watch the file descriptor under the filename. when a log is rotated and a new log is created the server may not stop writing to the old file descriptor immediately. Any data written to that descriptor in this state ends up in /dev/null
watchfd.watch(filename, [options], listener)
filename its really intended that this be a regular file or non existant. i dont know what would happen right now if its a directory.
options. supported custom options are
{
"timeout": 60*60*1000, //defaults to one hour
//how long an inactive file descriptor can remain inactive
"timeoutInterval":60*5*1000 //every five minutes
// how often to check for inactive file descriptors
}
//the options object is also passed directly to watch and watchFile so you may configure
{
"persistent":true, //defaults to true
//persistent indicates whether the process should continue to run as long as files are being watched
"interval":0, //defaults 0
//interval indicates how often the target should be polled, in milliseconds. (On Linux systems with inotify, interval is ignored.)
}
callback this is bound to the change event of the watcher. its required
callback(cur,prev)
cur and prev are instances of fs.Stats
@returns an instance of Watcher
Watcher.pause()
Watcher.resume()
Watcher.paused
Watcher.on(event name,call back);
I noticed distinct differences in watchFile vs watch api fs.watchFile will issue events for a file that is currently referenced by a path fs.watch will take a path but issue events whenever that file descriptor is changed even after it's unlinked
We should probably design servers to listen to SIGHUP and grab new file descriptors for all loggers but even if you used logrotate with copytruncate mode as to not change the file referenced by a path the chance that you will loose data is still there. I feel safer waiting for a file descriptor to be quiet so i know its out of use before i close it in a process that has the ability to read data out of it.