Skip to content

Commit

Permalink
Implmeent cli arguments and simple threading.
Browse files Browse the repository at this point in the history
Default to one thread per CPU, but we can limit this with something like: node app/server.js threads=1
  • Loading branch information
KevinGrandon committed Feb 11, 2012
1 parent 31c1958 commit b8e309a
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions src/all/server.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
var matador = require('matador')
var matador = require('matador'),
cluster = require('cluster');
http = require('http');
numCPUs = require('os').cpus().length,

app.configure(function () {
app.set('models', __dirname + '/app/models')
app.set('helpers', __dirname + '/app/helpers')
app.set('views', __dirname + '/app/views')
app.set('controllers', __dirname + '/app/controllers')
// Command line arguments
args = {}

app.set('view engine', 'html')
app.register('.html', matador.engine)

app.use(matador.cookieParser())
app.use(matador.bodyParser())
app.use(matador.methodOverride())
app.use(matador.static(__dirname + '/public'))
process.argv.forEach(function(val, index, array) {
if (val.indexOf('=') !== -1) {
var parts = val.split('=')
args[parts[0]] = parts[1]
}
})

app.configure('development', function () {
app.use(matador.errorHandler({ dumpExceptions: true, showStack: true }))
})
// Default threads to max possible
args.threads = args.threads || numCPUs

app.configure('production', function () {
app.use(matador.errorHandler())
})
app.set('viewPartials', matador.partials.build(app.set('views')))
matador.mount(require('./app/config/routes'))
app.listen(3000)
console.log('matador running on port 3000')
// Listen on multiple threads
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < args.threads; i++) {
cluster.fork()
}

cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died')
})
} else {
app.configure(function () {
app.set('models', __dirname + '/app/models')
app.set('helpers', __dirname + '/app/helpers')
app.set('views', __dirname + '/app/views')
app.set('controllers', __dirname + '/app/controllers')

app.set('view engine', 'html')
app.register('.html', matador.engine)

app.use(matador.cookieParser())
app.use(matador.bodyParser())
app.use(matador.methodOverride())
app.use(matador.static(__dirname + '/public'))
})

app.configure('development', function () {
app.use(matador.errorHandler({ dumpExceptions: true, showStack: true }))
})

app.configure('production', function () {
app.use(matador.errorHandler())
})
app.set('viewPartials', matador.partials.build(app.set('views')))
matador.mount(require('./app/config/routes'))
app.listen(3000)
console.log('matador running on port 3000')
}

0 comments on commit b8e309a

Please sign in to comment.