cluster-mail
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -11,8 +11,19 @@ | ||
var server = http.createServer(function(req, res){ | ||
if (Math.random() > 0.9) throw new Error('fail!'); | ||
res.end('Hello World'); | ||
// uncaught exceptions are automatically reported, however | ||
// for exceptions that we have caught, typically within | ||
// an error handler, we may report them with cluster.reportException() | ||
try { | ||
if (Math.random() > 0.9) throw new Error('fail!'); | ||
res.end('Hello World'); | ||
} catch (err) { | ||
// we can also optionally send an arbitrary json object | ||
// for use within our template | ||
var data = { method: req.method, url: req.url, headers: req.headers }; | ||
cluster.mailException(err, data); | ||
console.log('sent %s', err.message); | ||
} | ||
}); | ||
cluster(server) | ||
cluster = cluster(server) | ||
.use(mail('tjholowayhuk@gmail.com')) | ||
.listen(3000); |
0.1.0 / 2011-04-19 | ||
================== | ||
* Added arbitrary details display (request url, method, headers, etc) | ||
* Added `cluster.mailException()` for workers | ||
* Added `send()` helper function | ||
0.0.2 / 2011-04-05 | ||
@@ -3,0 +10,0 @@ ================== |
@@ -26,3 +26,3 @@ | ||
exports.version = '0.0.2'; | ||
exports.version = '0.1.0'; | ||
@@ -56,26 +56,59 @@ /** | ||
options.from = options.from || 'mail@cluster.com'; | ||
var subject = options.subject || 'cluster({worker}) exception: {message}'; | ||
options.subject = options.subject || 'cluster({worker}) exception: {message}'; | ||
options.template = options.template || template(); | ||
return function(master){ | ||
master.on('worker exception', function(worker, err){ | ||
// subject | ||
options.subject = subject | ||
.replace('{worker}', worker.id) | ||
.replace('{message}', err.message); | ||
mail.enableInWorker = true; | ||
// body | ||
options.bodyType = 'html'; | ||
options.body = options.template({ worker: worker, error: err }); | ||
function mail(master){ | ||
if (master.isWorker) { | ||
master.mailException = function(err, data){ | ||
master.call('mailException', err, data); | ||
}; | ||
} else { | ||
master.mailException = function(worker, err, data){ | ||
send(worker, err, options, data); | ||
}; | ||
// send | ||
var email = new Email(options); | ||
email.send(function(err){ | ||
if (err) console.error(err.stack || err.message); | ||
}); | ||
}); | ||
master.on('worker exception', function(worker, err){ | ||
send(worker, err, options); | ||
}); | ||
} | ||
}; | ||
return mail; | ||
}; | ||
/** | ||
* Send `err` notification for the given `worker`. | ||
* | ||
* @param {Worker} worker | ||
* @param {Error} err | ||
* @param {Object} options | ||
* @param {Object} data | ||
* @api private | ||
*/ | ||
function send(worker, err, options, data) { | ||
// subject | ||
options.subject = options.subject | ||
.replace('{worker}', worker.id) | ||
.replace('{message}', err.message); | ||
// body | ||
options.bodyType = 'html'; | ||
options.body = options.template({ | ||
worker: worker | ||
, error: err | ||
, data: data | ||
}); | ||
// send | ||
var email = new Email(options); | ||
email.send(function(err){ | ||
if (err) console.error(err.stack || err.message); | ||
}); | ||
} | ||
/** | ||
* Read / compile default template. | ||
@@ -82,0 +115,0 @@ */ |
{ | ||
"name": "cluster-mail" | ||
, "version": "0.0.2" | ||
, "version": "0.1.0" | ||
, "description": "Email notification plugin for Cluster" | ||
@@ -5,0 +5,0 @@ , "keywords": ["cluster", "email"] |
@@ -18,3 +18,3 @@ | ||
- `timeout` sendmail timeout in milliseconds | ||
- `subject` defaulting to " cluster({worker}) exception: {message}" | ||
- `subject` defaulting to "cluster({worker}) exception: {message}" | ||
- `template` function called with local variables (usually jade / ejs template etc) | ||
@@ -38,2 +38,17 @@ | ||
## Manual Reporting Example | ||
Often it is useful to report an exception within a worker that was caught, thus never terminating the process, in turn never notifying cluster's master process. An example of this would be within a connect or express error handler, where you simply responded with an error page, however you still want to be notified via mail. | ||
To do this we invoke the `mailException()` method with the error, as well as an optional object containing additional data to display in the email. This may include request information, user information etc. | ||
// somewhere in your application | ||
var data = { method: req.method, url: req.url }; | ||
cluster.mailException(err, data); | ||
// start the server | ||
cluster = cluster(server) | ||
.use(mail(['dev@learnboost.com', 'errors@learnboost.com'])) | ||
.listen(3000); | ||
## Screenshot | ||
@@ -40,0 +55,0 @@ |
Sorry, the diff of this file is not supported yet
7774
119
79