Last active
September 12, 2018 11:41
-
-
Save coderofsalvation/ea468771cbb5db3c7c8b0374d4fb94ba to your computer and use it in GitHub Desktop.
scalable express 4 cluster with (worker)bus
This file has been truncated, but you can view the full file.
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
/* | |
* api-server which uses multiple cpucores: | |
* | |
* cpu #1 : for webrequests (express) | |
* cpu #2..999 : for workers (receive webhook from trello, push to gmail e.g.) | |
* | |
*/ | |
const cluster = require('cluster'); | |
const numCPUs = require('os').cpus().length; | |
var port = parseInt(process.env.PORT) || 3002 | |
var host = process.env.HOST || '127.0.0.1' | |
var simplebus = require('simplebus') | |
var workers = ['url2pdf','pdf2html'] | |
var startserver = () => { | |
// Create bus server (for workers) | |
var bus = simplebus.createBus(1000) | |
var server = simplebus.createServer(bus, port+1, host) | |
server.start() | |
// Create a new Express application. | |
var express = require('express') | |
var app = express() | |
var bodyParser = require('body-parser') | |
app.use( bodyParser.json() ) | |
app.use( function(req, res, next){ | |
if( req.method != "get" && !req.url.match(/\/(swagger-ui|console|favico|openapi)/) ) | |
console.log( req.method.toUpperCase()+' '+req.url+' {'+ Object.keys(req.body).join(',') +'}' ) | |
bus.post({ | |
type: "http", | |
method: req.method, | |
body: req.body, | |
url: req.url, | |
header:req.header | |
}) | |
if( req.url.match(/\/worker\//) ) return res.send({error:false, msg:"message queued for delivery"}).end() | |
next() | |
}) | |
app.listen(port, function(){ | |
console.log("express listening on "+host+':'+port) | |
console.log("bus listening on "+host+':'+(port+1)) | |
}) | |
// Fork workers. | |
for (let i = 0; i < numCPUs; i++) cluster.fork(); | |
cluster.on('exit', (worker, code, signal) => { | |
console.log(`worker ${worker.process.pid} died`); | |
}); | |
} | |
var startworker = () => { | |
process.config = config | |
var bus = simplebus.createClient(port+1, host) | |
bus.start( () => { | |
bus.subscribe({type:'http'}, () => { | |
// doAnalytics() | |
}) | |
workers.forEach( (worker, i) => { | |
console.log( `worker ${process.pid} <= /worker/${worker}` ) | |
bus.subscribe( {type:'http', url: `/worker/${worker}`}, function (worker,msg) { | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment