lucagrulla/node-tail — pull request #17 by LeoIannacone (2014-03-25 18:04:57+00:00)
Title: Check if file exists

Hi all,

this is a version of Tail that does not crash if file does not exists at startup or is truncated during execution.

I added two new options:

@existsMaxChecks -> max number of times to chek if does not exist (default 360 times)
@existsInterval -> timeout between checks (default 500 millis)

I tested everything with this little script:

```
var Tail = require('tail').Tail,
exec = require('child_process').exec

// delete file if exists
exec ('text -f file.txt && rm file.txt')

// start tail with a file does not yet exist
setTimeout(function() {
tail = new Tail('file.txt', null, {}, {'existsInterval': 100, 'existsMaxChecks': 20}) // check for max 2 seconds

tail.on("line", function(data) {
console.log("line:", data);
})

tail.on("error", function(data) {
console.error("ERROR: ", data)
})
}, 500)

// perfrom some append on file and
// remove it on the 5th and on 10th hit
setTimeout(function() {
//exec('echo hello world -1 >> file.txt')
for (i = 0; i <= 10; i++)
if (i === 5) {
exec('sleep ' + i + ' && rm file.txt')
}
else if (i === 10) {
exec('sleep ' + i + ' && rm file.txt')
}
else {
exec('sleep ' + i + ' && echo hello world ' + (i>5?i-(5+1):i) + ' >> file.txt')
}

}, 1000)
```

I also created a TailError class to specify error si related to the Tail module.

Everything seems working great ...