forked from Medium/matador
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implmeent cli arguments and simple threading.
Default to one thread per CPU, but we can limit this with something like: node app/server.js threads=1
- Loading branch information
1 parent
31c1958
commit b8e309a
Showing
1 changed file
with
51 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |