Skip to content

Instantly share code, notes, and snippets.

@elliotttf
Last active February 23, 2016 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotttf/eebd0fac0b4e720c63bd to your computer and use it in GitHub Desktop.
Save elliotttf/eebd0fac0b4e720c63bd to your computer and use it in GitHub Desktop.
adios example
'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');
}
{
"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 <elliott@fourkitchens.com> (https://fourkitchens.com/)",
"license": "MIT",
"dependencies": {
"adios": "^1.1.2"
},
"engines": {
"node": ">= 4.2"
}
}
'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