Giter VIP home page Giter VIP logo

node-watchfd's Introduction

Build Status

watchfd

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.

install

npm install watchfd

use

var watchfd = require('watchfd').watch;
watchfd('/some.log',function(cur,prev){
	console.log(prev.size,' changed to ',cur.size);
});

a use case

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

argument structure

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 methods

Watcher.pause()

  • paused, changed and last state is kept for each file descriptor
    • stops file descriptors from timing out.
    • all events except error are paused.
    • unlink, open, change etc will be fired in the correct order after resume. no events will be missed but change events will be combined

Watcher.resume()

  • resumed
    • for each file descriptor pending events are fired in the corect order open,change,unlink
    • the change event has the stat from first change event while paused and the most recent so no change is missed.

Watcher.paused

  • is paused
  • readonly please.

Watcher events

Watcher.on(event name,call back);

  • change fs.Stats cur, fs.Stats prev
  • open fs.Stats cur,{fd:file descriptor,stat:fs.Stats cur}
  • unlink fs.Stats cur,{fd:file descriptor,stat:fs.Stats cur}
  • timeout fs.Stats cur,{fd:file descriptor,stat:fs.Stats cur}

windows support problems

  • It uses file inode as a unique id for each descriptor. I know there is a way to get a unique id for a file in windows i just don't know if that would be passed to stat as stat.ino.
  • I use watchFile which is not supported at all on windows but this would be easier to overcome considering i can use a configured polling interval as a stat polling fall back on windows.
  • I also don't know windows very well and don't know if windows has the problem this module solves...but i imagine it would

notes

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.

node-watchfd's People

Contributors

lovell avatar soldair avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

pehlert

node-watchfd's Issues

Node v0.12 / io.js - Non-existent file generates "Cannot read property 'fd' of undefined" error

Hi Ryan, I'm using tailfd 0.1.6 with the watchfd 0.0.14 dependency (so includes the fix for #1 that may have exposed this problem) but get the following when tailing a not-yet-existent file:

self.emit('unlink',self.fds[ino].fd,self.fds[ino].getData());
^
TypeError: Cannot read property 'fd' of undefined
at StatWatcher.<anonymous> (node_modules/tailfd/node_modules/watchfd/watch.js:176:41)
at emitTwo (events.js:87:13)
at StatWatcher.emit (events.js:169:7)
at StatWatcher._handle.onchange (fs.js:1282:10)

I've only been able to reproduce this with io.js so far and have yet to work out which recent fs.js change may be the culprit.

Adding an if (ino) { ... } wrapper around the self.emit('unlink', ...) statement seems to fix this, but perhaps the if(!cur.ino && prev.ino || cur.nlink === 0) conditional should prevent it getting that far.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.