Last active
February 23, 2016 14:56
-
-
Save elliotttf/eebd0fac0b4e720c63bd to your computer and use it in GitHub Desktop.
adios 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
'use strict'; | |
/** | |
* @file master.js | |
* Master process code for adios-example. | |
*/ | |
const cluster = require('cluster'); | |
const os = require('os'); | |
const path = require('path'); | |
const Adios = require('adios'); | |
// Define a custom socket to use for this example. | |
const DOMAIN_SOCKET = path.join(os.tmpDir(), 'adios-example.sock'); | |
if (cluster.isMaster) { | |
// Assign a process title to the application so we can stop it with | |
// npm stop | |
process.title = 'adios-example'; | |
Adios.master.init(DOMAIN_SOCKET) | |
.then(() => { | |
// Adios server is ready, child processes can be initiated with | |
// cluster.fork() | |
cluster.fork({type: 'web', DOMAIN_SOCKET: DOMAIN_SOCKET}); | |
}); | |
} | |
else if (process.env.type === 'web') { | |
require('./webWorker'); | |
} |
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
{ | |
"name": "adios-example", | |
"version": "1.0.0", | |
"description": "A simple example of using adios for process management", | |
"main": "master.js", | |
"scripts": { | |
"start": "node master.js", | |
"stop": "pkill -SIGINT -x adios-example" | |
}, | |
"author": "Elliott Foster <[email protected]> (https://fourkitchens.com/)", | |
"license": "MIT", | |
"dependencies": { | |
"adios": "^1.1.2" | |
}, | |
"engines": { | |
"node": ">= 4.2" | |
} | |
} |
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
'use strict'; | |
/** | |
* @file webWorker.js | |
* Web worker code for adios-example. | |
*/ | |
const http = require('http'); | |
const Adios = require('adios'); | |
const server = http.createServer(); | |
Adios.child.init(() => new Promise(resolve => { | |
console.log('shutting down web worker'); | |
server.close(() => { | |
console.log('web worker shut down'); | |
resolve(); | |
}); | |
}), process.env.DOMAIN_SOCKET) | |
.then(() => server.listen(3000, () => console.log('web worker listening'))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment