Node cluster example
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
#!/usr/bin/env node | |
/** | |
* External dependencies | |
*/ | |
const cluster = require( 'cluster' ) | |
const WORKERS = process.env.WORKERS || require( 'os' ).cpus().length | |
const SHUTDOWN_TIMEOUT = process.env.SHUTDOWN_TIMEOUT || 5000 | |
if ( cluster.isMaster ) { | |
for ( let i = 0; i < WORKERS; i++ ) { | |
cluster.fork() | |
} | |
cluster.on( 'listening', ( worker, address ) => { | |
console.log( 'Worker %d (pid %d) listening on http://%s:%d', | |
worker.id, | |
worker.process.pid, | |
address.address || '127.0.0.1', | |
address.port | |
) | |
} ) | |
cluster.on( 'exit', ( worker, code, signal ) => { | |
if ( ! worker.exitedAfterDisconnect ) { | |
console.log( 'Worker %d (pid %d) died with code %d and signal %s, restarting', worker.id, worker.process.pid, code, signal ) | |
cluster.fork() | |
} | |
} ) | |
const gracefulShutdown = worker => { | |
worker.send( 'shutdown' ) | |
worker.disconnect() | |
const shutdown = setTimeout( () => { | |
worker.process.kill() | |
}, SHUTDOWN_TIMEOUT ) | |
worker.on( 'exit', () => clearTimeout( shutdown ) ) | |
} | |
process.on( 'SIGHUP', () => { | |
console.log( 'Caught SIGHUP, reloading workers' ) | |
for ( const id in cluster.workers ) { | |
cluster.fork().on( 'listening', () => { | |
gracefulShutdown( cluster.workers[ id ] ) | |
} ) | |
} | |
} ) | |
process.on( 'SIGINT', () => { | |
console.log( 'Caught SIGINT, initiating graceful shutdown' ) | |
for ( const id in cluster.workers ) { | |
gracefulShutdown( cluster.workers[ id ] ) | |
} | |
} ) | |
} else { | |
const server = require( './server' ) | |
process.on( 'message', message => { | |
switch( message ) { | |
case 'shutdown': | |
server.close( () => process.exit( 0 ) ) | |
return | |
} | |
} ) | |
process.on( 'SIGINT', () => { | |
// Ignore first SIGINT from parent | |
process.on( 'SIGINT', () => { | |
process.exit( 1 ) | |
} ) | |
} ) | |
process.on( 'uncaughtException', error => { | |
console.log( error.stack ) | |
// Stop accepting connections and exit | |
server.close( () => process.exit( 1 ) ) | |
// Force shutdown after timeout | |
setTimeout( () => { | |
process.exit( 1 ) | |
}, SHUTDOWN_TIMEOUT ) | |
} ) | |
} |
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
/** | |
* External dependencies | |
*/ | |
const { createServer } = require( 'http' ) | |
module.exports = createServer( ( req, res ) => { | |
if ( Math.random() > 0.99999 ) { | |
// Randomly throws an uncaught error 0.001% of the time | |
throw Error( '0.001% error' ) | |
} | |
res.end( 'Hello World!\n' ) | |
} ).listen( process.env.port || 3000 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment