Add a domain handler to prevent service outages from requiring a server restart

This commit is contained in:
calzoneman 2013-08-16 21:35:21 -05:00
parent 8776ae15fc
commit 05dc2a3119

View file

@ -11,13 +11,23 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var http = require("http"); var http = require("http");
var https = require("https"); var https = require("https");
var domain = require("domain");
var Logger = require("./logger.js"); var Logger = require("./logger.js");
var Media = require("./media.js").Media; var Media = require("./media.js").Media;
var CustomEmbedFilter = require("./customembed").filter; var CustomEmbedFilter = require("./customembed").filter;
module.exports = function (Server) { module.exports = function (Server) {
function urlRetrieve(transport, options, callback) { var urlRetrieve = function (transport, options, callback) {
try { // Catch any errors that crop up along the way of the request
// in order to prevent them from reaching the global handler.
// This should cut down on needing to restart the server
var d = domain.create();
d.on("error", function (err) {
Logger.errlog.log("urlRetrieve failed: " + err);
Logger.errlog.log("Request was: " + options.host + options.path);
callback(503, err);
});
d.run(function () {
var req = transport.request(options, function (res) { var req = transport.request(options, function (res) {
var buffer = ""; var buffer = "";
res.setEncoding("utf-8"); res.setEncoding("utf-8");
@ -30,10 +40,8 @@ module.exports = function (Server) {
}); });
req.end(); req.end();
} catch(e) { });
callback(503, ""); };
}
}
var Getters = { var Getters = {
/* youtube.com */ /* youtube.com */
@ -65,6 +73,9 @@ module.exports = function (Server) {
} else if(status === 403) { } else if(status === 403) {
callback("Private video", null); callback("Private video", null);
return; return;
} else if(status === 503) {
callback("API failure", null);
return;
} else if(status !== 200) { } else if(status !== 200) {
callback(true, null); callback(true, null);
return; return;
@ -177,6 +188,9 @@ module.exports = function (Server) {
} else if(status === 403) { } else if(status === 403) {
callback("Playlist is private", null); callback("Playlist is private", null);
return; return;
} else if(status === 503) {
callback("API failure", null);
return;
} else if(status !== 200) { } else if(status !== 200) {
callback(true, null); callback(true, null);
} }
@ -278,6 +292,9 @@ module.exports = function (Server) {
} else if(status === 403) { } else if(status === 403) {
callback("Private video", null); callback("Private video", null);
return; return;
} else if(status === 503) {
callback("API failure", null);
return;
} else if(status !== 200) { } else if(status !== 200) {
callback(true, null); callback(true, null);
return; return;
@ -362,6 +379,9 @@ module.exports = function (Server) {
if(status === 404) { if(status === 404) {
callback("Sound not found", null); callback("Sound not found", null);
return; return;
} else if(status === 503) {
callback("API failure", null);
return;
} else if(status !== 302) { } else if(status !== 302) {
callback(true, null); callback(true, null);
return; return;