Skip to content

Commit

Permalink
Crashlogger: include whether the process had an uncaught exception in…
Browse files Browse the repository at this point in the history
… emails

This is relevant information as some crashes may only happen after some other crash
has created inconsistent state either in PS itself or in Node.js internals.
  • Loading branch information
Slayer95 committed Sep 11, 2015
1 parent 50966cc commit 8cbfea7
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
3 changes: 1 addition & 2 deletions app.js
Expand Up @@ -345,7 +345,7 @@ if (Config.crashguard) {
var lastCrash = 0;
process.on('uncaughtException', function (err) {
var dateNow = Date.now();
var quietCrash = require('./crashlogger.js')(err, 'The main process');
var quietCrash = require('./crashlogger.js')(err, 'The main process', true);
quietCrash = quietCrash || ((dateNow - lastCrash) <= 1000 * 60 * 5);
lastCrash = Date.now();
if (quietCrash) return;
Expand All @@ -354,7 +354,6 @@ if (Config.crashguard) {
Rooms.lobby.addRaw('<div class="broadcast-red"><b>THE SERVER HAS CRASHED:</b> ' + stack + '<br />Please restart the server.</div>');
Rooms.lobby.addRaw('<div class="broadcast-red">You will not be able to talk in the lobby or start new battles until the server restarts.</div>');
}
Config.modchat = 'crash';
Rooms.global.lockdown = true;
});
}
Expand Down
3 changes: 1 addition & 2 deletions battle-engine.js
Expand Up @@ -18,13 +18,12 @@ global.Config = require('./config/config.js');
if (Config.crashguard) {
// graceful crash - allow current battles to finish before restarting
process.on('uncaughtException', function (err) {
require('./crashlogger.js')(err, 'A simulator process');
require('./crashlogger.js')(err, 'A simulator process', true);
/* var stack = ("" + err.stack).escapeHTML().split("\n").slice(0, 2).join("<br />");
if (Rooms.lobby) {
Rooms.lobby.addRaw('<div><b>THE SERVER HAS CRASHED:</b> ' + stack + '<br />Please restart the server.</div>');
Rooms.lobby.addRaw('<div>You will not be able to talk in the lobby or start new battles until the server restarts.</div>');
}
Config.modchat = 'crash';
Rooms.global.lockdown = true; */
});
}
Expand Down
9 changes: 6 additions & 3 deletions crashlogger.js
Expand Up @@ -8,11 +8,11 @@
* @license MIT license
*/

module.exports = (function () {
exports = module.exports = (function () {
var lastCrashLog = 0;
var transport;
var logPath = require('path').resolve(__dirname, 'logs/errors.txt');
return function (err, description) {
return function (err, description, isException) {
console.log("\nCRASH: " + (err.stack || err) + "\n");
require('fs').createWriteStream(logPath, {'flags': 'a'}).on("open", function (fd) {
this.write("\n" + err.stack + "\n");
Expand All @@ -33,12 +33,15 @@ module.exports = (function () {
from: Config.crashguardemail.from,
to: Config.crashguardemail.to,
subject: Config.crashguardemail.subject,
text: description + " crashed with this stack trace:\n" + (err.stack || err)
text: description + " crashed " + (exports.hadException ? "again " : "") + "with this stack trace:\n" + (err.stack || err)
}, function (err) {
if (err) console.log("Error sending email: " + err);
});
}
}
if (isException) {
exports.hadException = true;
}
if (process.uptime() > 60 * 60) {
// no need to lock down the server
return true;
Expand Down
2 changes: 1 addition & 1 deletion sockets.js
Expand Up @@ -136,7 +136,7 @@ if (cluster.isMaster) {
if (Config.crashguard) {
// graceful crash
process.on('uncaughtException', function (err) {
require('./crashlogger.js')(err, 'Socket process ' + cluster.worker.id + ' (' + process.pid + ')');
require('./crashlogger.js')(err, 'Socket process ' + cluster.worker.id + ' (' + process.pid + ')', true);
});
}

Expand Down
2 changes: 1 addition & 1 deletion team-validator.js
Expand Up @@ -105,7 +105,7 @@ if (!process.send) {

if (Config.crashguard) {
process.on('uncaughtException', function (err) {
require('./crashlogger.js')(err, 'A team validator process');
require('./crashlogger.js')(err, 'A team validator process', true);
});
}

Expand Down

0 comments on commit 8cbfea7

Please sign in to comment.