Skip to content

Commit

Permalink
Reduction to open file counts: No longer fs.watch()ing ignored files,…
Browse files Browse the repository at this point in the history
… no longer attempting to watch the same files multiple times, and outputing error when ulimit is too low.
  • Loading branch information
Bryan Kaplan committed Jun 5, 2012
1 parent 0447ff5 commit 469c7d2
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions nodemon.js
Expand Up @@ -36,7 +36,8 @@ var fs = require('fs'),
// Flag to distinguish an app crash from intentional killing (used on Windows only for now) // Flag to distinguish an app crash from intentional killing (used on Windows only for now)
killedAfterChange = false, killedAfterChange = false,
// Make this the last call so it can use the variables defined above (specifically isWindows) // Make this the last call so it can use the variables defined above (specifically isWindows)
program = getNodemonArgs(); program = getNodemonArgs(),
watched = [];


// test to see if the version of find being run supports searching by seconds (-mtime -1s -print) // test to see if the version of find being run supports searching by seconds (-mtime -1s -print)
if (noWatch) { if (noWatch) {
Expand Down Expand Up @@ -239,19 +240,27 @@ function startMonitor() {


fs.readdir(dir, function (err, files) { fs.readdir(dir, function (err, files) {
if (!err) { if (!err) {
files = files.
map(function (file ) { return path.join(dir, file); }).
filter(ignoredFilter);
files.forEach(function (file) { files.forEach(function (file) {
var filename = path.join(dir, file); if (-1 === watched.indexOf(file)) {
fs.stat(filename, function (err, stat) { watched.push(file);
if (!err && stat) { fs.stat(file, function (err, stat) {
if (stat.isDirectory()) { if (!err && stat) {
fs.realpath(filename, watch); if (stat.isDirectory()) {
fs.realpath(file, watch);
}
} }
} });
}); }
}); });
} }
}); });
} catch (e) { } catch (e) {
if ('EMFILE' === e.code) {
console.error('EMFILE: Watching too many files.');
}
// ignoring this directory, likely it's "My Music" // ignoring this directory, likely it's "My Music"
// or some such windows fangled stuff // or some such windows fangled stuff
} }
Expand All @@ -267,45 +276,41 @@ function startMonitor() {
changeFunction = function() { util.error("Nodemon error: changeFunction called when it shouldn't be.") } changeFunction = function() { util.error("Nodemon error: changeFunction called when it shouldn't be.") }
} }


// filter ignored files
var ignoredFilter = function (file) {
// If we are in a Windows machine
if (isWindows) {
// Break up the file by slashes
var fileParts = file.split(/\\/g);

// Remove the first piece (C:)
fileParts.shift();

// Join the parts together with Unix slashes
file = '/' + fileParts.join('/');
}
return !reIgnoreFiles.test(file);
};

var isWindows = process.platform === 'win32'; var isWindows = process.platform === 'win32';
if ((noWatch || watchWorks) && !program.options.forceLegacyWatch) { if ((noWatch || watchWorks) && !program.options.forceLegacyWatch) {
changeFunction(lastStarted, function (files) { changeFunction(lastStarted, function (files) {
if (files.length) { if (files.length) {
// filter ignored files if (restartTimer !== null) clearTimeout(restartTimer);
if (ignoreFiles.length) { restartTimer = setTimeout(function () {
files = files.filter(function(file) { if (program.options.verbose) util.log('[nodemon] restarting due to changes...');
// If we are in a Windows machine files.forEach(function (file) {
if (isWindows) { if (program.options.verbose) util.log('[nodemon] ' + file);
// Break up the file by slashes
var fileParts = file.split(/\\/g);

// Remove the first piece (C:)
fileParts.shift();

// Join the parts together with Unix slashes
file = '/' + fileParts.join('/');
}
return !reIgnoreFiles.test(file);
}); });
} if (program.options.verbose) util.print('\n\n');


if (files.length) { if (child !== null) {
if (restartTimer !== null) clearTimeout(restartTimer); child.kill(isWindows ? '' : 'SIGUSR2');
restartTimer = setTimeout(function () { } else {
if (program.options.verbose) util.log('[nodemon] restarting due to changes...'); startNode();
files.forEach(function (file) { }
if (program.options.verbose) util.log('[nodemon] ' + file); }, restartDelay);
}); return;
if (program.options.verbose) util.print('\n\n');

if (child !== null) {
child.kill(isWindows ? '' : 'SIGUSR2');
} else {
startNode();
}
}, restartDelay);
return;
}
} }


if (noWatch) setTimeout(startMonitor, timeout); if (noWatch) setTimeout(startMonitor, timeout);
Expand Down

0 comments on commit 469c7d2

Please sign in to comment.