开发者问题收集

node.js fs.createWriteStream 创建文件,但无法无错误地写入

2018-06-22
2486

我试图捕获“未捕获异常”并将其写入服务器上的文件中。我的代码是:

var cluster = require('cluster');
// The master's job to spawn workers initially and when they die

if (cluster.isMaster) {
    // Get the number of processor cores
    var cpuCount = require('os').cpus().length;

    // Create a worker for each CPU
    for (var i = 0; i < cpuCount; i += 1) {
        cluster.fork();
    }

    // When a worker exits, fork a new one
    cluster.on('exit', function(worker) {
        console.log('Worker %d died', worker.id);
        cluster.fork();
    });
} else if (cluster.isWorker) {
    const _express = require('express');
    const _bodyParser = require('body-parser');
    const _cors = require('cors');
    const _fs = require('fs');
    const _util = require('util');
    const _app = _express();
    const _http = require('http');
    const _server = _http.createServer(_app);
    const _port = 80;

    process.on('uncaughtException', function(_err) {
        var _error = _fs.createWriteStream(__dirname + '/node.error.log', { flags: 'a' });

        _error.write(_err.message + '\n', (err) => {
            if (err) {
                console.log('Error:', err.message);
            }else{
                console.log('Error Written');
            }
        });

        _error.end();

        console.log('Caught exception: ' + _err);
    });var _listener = _server.listen(_port, () => {
        console.log('Server started on port ' + _port);
    });

    const _io = require('socket.io')(_listener);

    // parse application/json
    _app.use(_bodyParser.json());

    // parse application/x-www-form-urlencoded
    _app.use(_bodyParser.urlencoded({ extended: true }));

    _app.use(_express.static(__dirname + '/node_modules'));

    // Allow cross-origin requests
    var corsOptions = {
        origin: function(_origin, _callback){
            _callback(null, _origin);
        },
        credentials: true
    };

    _app.use(_cors(corsOptions));

    // html render engine
    _app.engine('html', require('ejs').renderFile);
    _app.set('view engine', 'html');

    _io.on('connection', function (_socket) {
        // Sends to the initiator
        _socket.emit("connectionSuccessful", "Connected on socket " + _socket.id);

        _socket.on('registerUser', function(_args) {
            console.log('registerUser', _args);
            fakeFunction();
        });
    });
}

registerUser 事件不是我的完整事件,而是为了抛出错误而缩短的事件。

原始

底部的控制台日志打印到命令行( caught exception 行)。将创建错误文件(如果尚未创建)。但是, _error.write 区域内的任何内容均未执行。没有显示任何控制台日志,并且文件保持为空。

这不是权限问题,因为我已授予所有人(666)读取和写入文件的权限(用于测试)。所以我不确定为什么这不起作用。我见过的所有示例都表明这对他们有效。我不知道为什么它对我不起作用。

编辑

这是我在控制台中看到的内容:

Server started on port 80
Connected
Caught exception: TypeError: Cannot read property 'multi_id' of undefined
TypeError: Cannot read property 'multi_id' of undefined
    at Query._callback (/var/www/nodejs/models/auctions.js:41:18)
    at Query.Sequence.end (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/sequences/Sequence.js:85:24)
    at Query._handleFinalResultPacket (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/sequences/Query.js:144:8)
    at Query.EofPacket (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/sequences/Query.js:128:8)
    at Protocol._parsePacket (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/Protocol.js:280:23)
    at Parser.write (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/Parser.js:74:12)
    at Protocol.write (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/var/www/nodejs/node_modules/mysql-clusterfarm/lib/Connection.js:109:28)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7) 'Uncaught Exception thrown'
Worker 1 died

已解决

我发现我实际上两次捕获了 uncaughtExceptions...

process
    .on('unhandledRejection', (reason, p) => {
        console.error(reason, 'Unhandled Rejection at Promise', p);
        console.log(reason, 'Unhandled Rejection at Promise', p);
        process.exit(1);
    })
    .on('uncaughtException', _err => {
        console.error(_err, 'Uncaught Exception thrown');
        console.log(_err, 'Uncaught Exception thrown');
        process.exit(1);
    });

这导致先前的方法在实际将错误写入文件之前就终止(由于 process.exit(1) )。

2个回答

我发现我实际上捕获了两次 uncaughtExceptions...

process
    .on('unhandledRejection', (reason, p) => {
        console.error(reason, 'Unhandled Rejection at Promise', p);
        console.log(reason, 'Unhandled Rejection at Promise', p);
        process.exit(1);
    })
    .on('uncaughtException', _err => {
        console.error(_err, 'Uncaught Exception thrown');
        console.log(_err, 'Uncaught Exception thrown');
        process.exit(1);
    });

这导致先前的方法在实际将错误写入文件之前就终止(由于 process.exit(1) )。

James
2018-06-24

我可以请您尝试的是,

也许您不止一次捕获了未捕获的异常,这可能是之前的方法在实际将错误写入文件之前终止的原因。

在“uncaughtException”之后恢复正常操作是不安全的,因为系统已损坏。 'uncaughtException' 的正确用法是在关闭进程之前对分配的资源(例如文件描述符、句柄等)执行同步清理。

我强烈建议您阅读此内容 -> https://www.joyent.com/node-js/production/design/errors

可以查看此示例

 var cluster = require('cluster');
 var numCPUs = require('os').cpus().length;
 if (cluster.isMaster) {
 for (var i = 0; i < numCPUs; ++i) {
 cluster.fork();
  }
cluster.on('exit', (worker, code, signal) => {
console.log('worker ${worker.process.pid} died');
cluster.fork();
});
} else {
var http = require('http');
var httpServer = http.createServer(app).listen(httpPort, function () {
console.log('process id local', process.pid)
console.log("http server started at port " + httpPort);
});
}
process.on('uncaughtException', function (err) {
console.error((new Date).toUTCString() + ' uncaughtException:', err.message)
console.error(err.stack)
process.exit(1)
})`
Rohit Jaiswal
2019-05-05