Skip to content

Instantly share code, notes, and snippets.

@ryandesign
Last active November 9, 2015 13:31
Show Gist options
  • Save ryandesign/c3be714c9590d3d1b6bc to your computer and use it in GitHub Desktop.
Save ryandesign/c3be714c9590d3d1b6bc to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>NODE_ENV</key>
<string>production</string>
</dict>
<key>Label</key>
<string>com.example.simple</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/node</string>
<string>master.js</string>
</array>
<key>Sockets</key>
<dict>
<key>HTTPSocket</key>
<dict>
<key>SockFamily</key>
<string>IPv4v6</string>
<key>SockServiceName</key>
<integer>3000</integer>
</dict>
</dict>
<key>WorkingDirectory</key>
<string>/path/to/simple</string>
</dict>
</plist>
var cluster = require('cluster');
cluster.setupMaster({
exec: 'server.js'
});
var env = {};
if (!process.env.PORT) {
env.LAUNCHD_SOCKET = require('node-launchd').getSocketFileDescriptorForName('HTTPSocket');
}
var numWorkers = require('os').cpus().length;
for (var i = 0; i < numWorkers; ++i) {
cluster.fork(env);
}
cluster.on('exit', exited);
function exited(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' exited');
}
var http = require('http');
function app(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('worker ' + process.pid + '\n');
}
var server = http.createServer(app);
var portOrHandle = process.env.PORT || {
fd: +process.env.LAUNCHD_SOCKET || require('node-launchd').getSocketFileDescriptorForName('HTTPSocket')
};
server.listen(portOrHandle, listening);
function listening() {
console.log('worker ' + process.pid + ' listening');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment