工作进程运行主进程代码

时间:2014-08-17 14:38:39

标签: node.js process

当我更改test.is时,console.log("Source code change, start to restart worker one by one")运行3次?我有一个主进程,所以它应该是一次

var cluster = require('cluster');
var fs      = require('fs');

if (cluster.isMaster) {

    for (var i = 0; i < 2; i++) {
        cluster.fork();
    }

    fs.watch('./config/test.js', function(curr, prev) {
        console.log("Source code change, start to restart worker one by one");
        delete require.cache[require.resolve('./config/config.js')];
    })

}else{
    var config = require('./config/test.js')
}

1 个答案:

答案 0 :(得分:0)

不知何故,你的看门狗(fs.watch)被触发了三次,可能是因为你写了三个数据块?也许当文件变大时会更频繁地触发? (也许WinSCP在写入之前截断文件?)

我假设您只想在文件完全上传时触发?

很遗憾,您无法从IN_CLOSE_WRITE抓取inotify(..)消息。

var old = to; to = setTimeout(function() { clearTimeout(old); /* insert real stuff here */ }, 1000)内执行fs.watch之类的操作。如果文件在1000毫秒内不断变化,那么该函数只会触发一次......