成功后退出节点进程fs.appendFile

Exit Node Process After Successful fs.appendFile

我在创建与 Node 并行的进程时遇到问题,当它们完成一个简单的 HTTP GET 请求时退出。我注意到,如果我在 appendFile 的回调中触发 process.exit(),某些文件将不会在 Node 集群设置中创建或附加。理想情况下,下面的方式是我希望如何触发事件,因为一旦工作完成就退出进程:

var rp = require("request-promise");
    config = require("./config"),
    cluster = require("cluster"),
    os = require("os"),
    fs = require("fs");

var keywordArray = [
    'keyword1',
    'keyword2',
    ...
];

if (cluster.isMaster) {

    var numCPUs = os.cpus().length;
    var clusterDivision = Math.ceil(keywordArray.length/numCPUs);

    // Reset the json if previously set
    keywordArray.forEach(function(arrayItem) {
        fs.unlink(config.dataDirectory + arrayItem + '.json', function(err) {
            if (err) console.error(err);
            console.log('successfully unlinked ' + arrayItem + '.json from ' + config.dataDirectory);
        });
    });

    // Create a worker for each CPU
    // Seperate the array out evenly for each worker
    for (var j=1;j<=numCPUs;j++) {
        var tempArray = [];
        var removed = keywordArray.splice(0, clusterDivision);
        if (removed.length > 0) {
            // The array contains something so let's do something with the keyword
            console.log('creating a worker');
            cluster.fork().send(removed);
        } else {
            // We don't need a cluster here
        }
    }

    process.on('exit', function() {
        console.log('exited');
    });

} else if (cluster.isWorker) {
    //  Code to run if we're in a worker process

    // Send the object we created above from variables so they're available to the workers
    process.on('message', function(seperatedArrayItem) {

        seperatedArrayItem.forEach(function(arrayItem) {
            function radarRequest(err, response, body) {
                var responseBody = JSON.parse(body);
                console.log(arrayItem); 
                fs.appendFileSync(config.dataDirectory + arrayItem + '.json', JSON.stringify(responseBody.results, null, '\t'), function (err) {
                    if (err) console.err(err);
                    console.log('success writing file');
                });
            }

            rp({
                url: config.radarSearchURI + 
                '?key='+ config.apiKey + 
                '&location=' + config.latitude + ',' + config.longitude + 
                '&radius=' + config.searchRadius + 
                '&keyword=' + arrayItem, headers: config.headers
            }, radarRequest);
        });

        setTimeout(function() {
            process.exit(0);
        }, 5000);
    });
}

我可以确保所有文件都正确附加的唯一方法是使用超时,这正是我不想 - 也不应该 - 做的。有没有另一种方法可以确保 appendFile 成功发生并且 then 终止节点进程?这是一种可行的方法(假设该过程不会超过 5 秒):

    process.on('message', function(seperatedArrayItem) {

    seperatedArrayItem.forEach(function(arrayItem) {
        function radarRequest(err, response, body) {
            var responseBody = JSON.parse(body);
            console.log(arrayItem); 
            fs.appendFile(config.dataDirectory + arrayItem + '.json', JSON.stringify(responseBody.results, null, '\t'), function (err) {
                if (err) console.err(err)
                console.log('success writing file');
            });
        }

        rp({
            url: config.radarSearchURI + 
            '?key='+ config.apiKey + 
            '&location=' + config.latitude + ',' + config.longitude + 
            '&radius=' + config.searchRadius + 
            '&keyword=' + arrayItem, headers: config.headers
        }, radarRequest);
    });

    setTimeout(function() {
        process.exit(0);
    }, 5000);
});

您可以使用像async这样的异步流控制模块在所有文件写入后终止进程。我还建议 cluster.worker.disconnect() 以便节点进程可以简单地优雅地退出,但这不是必需的。

async.forEach(seperatedArrayItem, function(item, done){
    // append file and call 'done' when it is written.

}, function(){
    // Will be called when all item 'done' functions have been called.
    cluster.worker.disconnect();
});

节点fs.appendFile( ... )是一个异步函数。所以它希望我们传递一个 回调 因为我们知道它已经完成了它的主要操作,以通知我们发生了一些错误或其他目的。

这意味着我们需要在提供的回调范围内调用 Node process.exit( ... )。我写了这段代码来测试:

'use strict';

var fs = require('fs');

function jsonValue(obj) {
    return JSON.stringify(obj, null, '\t');
}

fs.appendFile('file.json', jsonValue(['t', 'e', 's', 't']), function(error) {
    if (error) {
        throw error;
    }

    console.log('success writing file');  // no error, so log...
    process.exit();                       // and exit right now
    console.log('exited?');               // this will not be printed
});

好吧,它按定义运行。

它的其他工作方式是使用 fs.appendFile( ... ) 的同步版本并按顺序调用 process.exit()

fs.appendFileSync('file.json', jsonValue(['t', 'e', 's', 't']));

console.log('success writing file'); // no error (I hope so =), so log...
process.exit(); // and exit right now
console.log('exited?'); // this will not be printed

这是干净的代码并且可以工作,但是您失去了通过回调获得的稳健性和便利性...