Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Working on the jade compilations

  • Loading branch information...
commit 51fae1629de5e43da8c854ab29e40a15fae96741 1 parent 3a07a21
@3rd-Eden authored
View
2  Makefile
@@ -1,4 +1,4 @@
test:
- expresso -I lib $(TESTFLAGS) tests/*.js
+ expresso -I lib $(TESTFLAGS) tests/*.test.js
.PHONY: test
View
64 lib/cluster.exception.js
@@ -18,7 +18,16 @@ var os = require('os')
, Email = require('email').Email;
/**
- * Expose the plugin
+ * Cluster.exception allows you to receive emails of uncaught exceptions in your
+ * node.js applications. Not only the error is send but also a history of the
+ * `console` statements, memory, cpu, etc, etc, the whole chabang. This might
+ * give you a better context on why the error was occuring in the first place.
+ *
+ * @param {Object} options The configurable options for the cluster.exceptions plugin.
+ * @param {String} options.from The `from` address field for the e-mail.
+ * @param {String} options.template The template that is send in the e-mail.
+ * @returns {Function} the configured plugin for the `cluster.use` method.
+ * @api public
*/
exports = module.exports = function exception(options){
options = options || {};
@@ -28,6 +37,7 @@ exports = module.exports = function exception(options){
, from = options.from || 'cluster@dev.null'
, subject = options.subject || 'Cluster.exception {date}'
, methods = options.methods || ['log','info','warn','error']
+ , template = options.template || '../views/basic.jade'
, history;
if (!options.to) throw Error("Please specify a e-mail address for the cluster.exception plugin");
@@ -82,6 +92,9 @@ exports = module.exports = function exception(options){
, cluster: {
}
+
+ // When did the exception occure
+ , date: timestamp()
};
// if we have log interception enabled, we are going
@@ -143,6 +156,19 @@ exports = module.exports = function exception(options){
});
/**
+ * Pre-compile the template
+ */
+ template = jade.compile(
+ fs.readFileSync(
+ require('path').join(
+ __dirname
+ , template
+ )
+ , 'utf-8'
+ )
+ );
+
+ /**
* The actual plugin
*
* @param {Cluster} cluster A worker or client instance
@@ -154,7 +180,41 @@ exports = module.exports = function exception(options){
// Add a listener to the process
process.on("uncaughtException", function captureException(error){
- var details = exception(error, instance);
+ var details = exception(error, instance)
+ , message = {
+ subject: replace(options.subject, details)
+ , from: options.from
+ , to: options.to
+ , bodyType: 'html'
+ , body: template(details)
+ };
+
+ // send the e-mail
+ var sendmail = new Email(message);
+ sendmail.send(function(err){
+ if (err){
+ console.error(err.message);
+ console.log(timesamp() + 'Failed to send cluster.exception mail, outputting details to stdout:');
+ console.dir(details);
+ } else {
+ console.log(timesamp() + ' Great success! Cluster.exception mail send.');
+ }
+ });
+
+ // mimic the default uncaught exception handling for workers
+ // see https://github.com/LearnBoost/cluster/blob/master/lib/worker.js#L95
+ if (instance.isWorker()){
+ // stderr for logs
+ console.error( error.stack || error.message);
+
+ // report exception
+ instance.master.call('workerException', error)
+
+ // exit
+ process.nextTick(function(){
+ instance.destroy();
+ })
+ }
});
};
View
5 views/basic.jade
@@ -0,0 +1,5 @@
+html
+ body(style="background:#fafafa; border-top:2px solid #3B88D8; font: 14px/1.4 'helvetica neue', helvetica, arial, sans-serif;")
+ div(style="margin-top:25px")
+ h1(style="font-size:28px") Cluster exception
+ p ping
View
7 views/default.jade.txt
@@ -1,7 +0,0 @@
-html
- body(style='background:#fafafa; border-top:2px solid #3B88D8; font: 14px/1.4 'helvetica neue', helvetica, arial, sans-serif;')
- div(style='margin-top:25px')
- h1(style='font-size:28px')
- | Cluster exception
- p
- | On #{date} a exception occured
Please sign in to comment.
Something went wrong with that request. Please try again.