Error handling, domains, and crash-only design
The Node community has embraced a crash-only design pattern, which simply means this: if you get an uncaught exception, catch it, log it, and restart the process. Crash-only design and domains work quite well as a pattern, particularly if your application is using cluster
. Let's make a change to our cluster
module, ./lib/cluster/index.js
, on vision-core
. Here, we include the domain
module; instead of simply including our module to run in a cluster, we create a domain and call the run
method. We then include a domain-based error
handler that logs and then closes the process via process.exit(1)
. The cluster exit
handler will pick this up and fork
a new process:
var cluster = require('cluster') , http = require('http') , numCPUs = require('os').cpus().length , logger = require('../logger') , domain = require('domain'); function Cluster() {} Cluster.prototype.run = function(module) { if (cluster.isMaster) { for (var i = 0; i < numCPUs...