Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated to 0.0.3.1 - added in mkclusterman which creates init scripts…

… that should be the primary interface to this
  • Loading branch information...
commit 01b38a7778b16516b84005b90d3cf88db50097e1 1 parent b9e0fd2
@felix-ordrin felix-ordrin authored
View
4 Changelog
@@ -1,3 +1,7 @@
+v0.0.3.1 Sun Mar 25 15:44:31 UTC 2012
+- added bin/mkclusterman to create easy init scripts - these should pretty
+much hide most of the implementation details
+
v0.0.3 Sat Mar 24 13:01:12 UTC 2012
- added bin/clusterman for global installation
- graceful restart now starts new child process immediately as soon as we
View
10 README.md
@@ -1,14 +1,16 @@
# clusterman
-clusterman hopes to provide some basic utility around using node 0.6.x's native clustering. It uses forever to keep the cluster master running. Right now it's a naive, generally untested implementation that hopes to allow for term and restarts.
+clusterman hopes to provide some basic utility around using node 0.6.x's native clustering. It provides a quick script to generate init scripts for your service that will make it super easy to start, stop and restart. It uses forever to keep the cluster master running. Right now it's a naive, generally untested implementation that hopes to allow for term and restarts.
It's meant to be installed globally:
```npm install -g https://nodeload.github.com/ordrin/clusterman/tarball/HEAD```
-You now have access to a binary called "clusterman." It expects a config file somewhere, i.e. /etc, and you could run it like:
+You now have access to two binaries called "clusterman" and "mkclusterman" It expects a config file somewhere, i.e. /etc, and you could run it like:
-```clusterman /etc/my_cluster_app.json```
+```mkclusterman /etc/my_cluster_app.json > /etc/init.d/my_cluster_app```
+
+mkclusterman reads all the bits of your config file and creates an init script that you can easily simply call ```/etc/init.d/my_cluster_app stop|start|restart``` and it will do the right thing.
It's can also be installed per app as a node_module, so:
@@ -53,6 +55,8 @@ module.exports = app;
## Managing the app
+### The Easy Way
+
The key bit is the pidFile you specify in the configuration. clusterman expects one of two signals:
- graceful restart
View
3  bin/clusterman
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('clusterman');
View
97 bin/mkclusterman
@@ -0,0 +1,97 @@
+#!/usr/bin/env node
+(function() {
+var _usage = function __usage() {
+ console.log( "Usage: mkclusterman /full/path/to/appconfig.json" );
+};
+
+if( process.argv.length === 3 ) { configfile = process.argv[2] }
+else { return _usage() }
+
+var config = require('nconf').argv().env().file({ file: configfile }),
+ pidfile = config.get('pidFile'),
+ rootdir = config.get('rootDir'),
+ script = config.get('script'),
+ fullscript = rootdir + '/' + script,
+ appname = rootdir.match(/\/([^/]+)$/)[1];
+
+var out = [
+ '#!/bin/bash',
+ '# chkconfig: 345 90 20',
+ '# description: ' + appname + ' a clusterman-aged app',
+ '# processname: ' + script,
+ '# pidfile: ' + pidfile,
+ '',
+ '. /etc/init.d/functions',
+ '',
+ 'NAME=' + appname,
+ '',
+ 'user=nobody',
+ 'pidfile=' + pidfile,
+ 'configfile=' + configfile,
+ 'start() {',
+ ' echo "Starting $NAME (clusterman): "',
+ ' ',
+ ' if [ "$pid" == "" ]; then',
+ ' # Launch the application',
+ ' daemon --user=root clusterman $configfile',
+ ' RETVAL=$?',
+ ' else',
+ ' echo "$NAME is already running"',
+ ' RETVAL=0',
+ ' fi',
+ ' echo',
+ '}',
+ ' ',
+ 'stop() {',
+ ' echo -n "Shutting down $NAME (clusterman): "',
+ ' if [ "$pid" != "" ]; then',
+ ' kill $pid',
+ ' RETVAL=$?',
+ ' else',
+ ' echo "$NAME is not running";',
+ ' RETVAL=0',
+ ' fi',
+ ' echo RV: $RETVAL',
+ ' echo',
+ '}',
+ '',
+ 'restart() {',
+ ' echo -n "Restarting down $NAME (clusterman): "',
+ ' if [ "$pid" != "" ]; then',
+ ' kill -s HUP $pid',
+ ' RETVAL=$?',
+ ' else',
+ ' echo "$NAME is not running";',
+ ' RETVAL=0',
+ ' fi',
+ ' echo RV: $RETVAL',
+ ' echo',
+ '}',
+ ' ',
+ 'if [ -f $pidfile ]; then',
+ ' read pid < $pidfile',
+ 'else',
+ ' pid=""',
+ 'fi',
+ ' ',
+ 'case "$1" in',
+ ' start)',
+ ' start',
+ ' ;;',
+ ' stop)',
+ ' stop',
+ ' ;;',
+ ' restart)',
+ ' restart',
+ ' ;;',
+ ' *)',
+ ' echo "Usage: {start|stop|restart}"',
+ ' exit 1',
+ ' ;;',
+ 'esac',
+ 'exit $RETVAL'
+];
+
+ console.log( out.join("\n") );
+
+}());
View
6 clusterize.js
@@ -60,7 +60,11 @@ var clusterMan = function cluster_man( port, server, minchild ) {
else {
delete _end[ pid ];
}
- })
+ });
+
+ process.on( 'exit', function() {
+ fs.unlinkSync( pidfile );
+ });
}
else {
server.on('request', function(req,res) {
View
13 index.js
@@ -1,6 +1,5 @@
(function() {
var configfile = process.argv.length === 3 ? process.argv[2] : 'clusterman.json';
-
var fs = require('fs'),
forever = require('forever'),
spawn = require('child_process').spawn;
@@ -26,13 +25,23 @@ console.log("FOREVER: ", execCluster, foreverOpts );
forever.startDaemon( execCluster, foreverOpts);
+process.on( 'exit', function() {
+ fs.unlinkSync( config.get( 'pidFile' ) );
+});
+
process.on('SIGTERM', function() {
var pid = fs.readFileSync( childPid );
process.kill( pid );
process.exit(0);
});
+process.on('SIGINT', function() {
+ var pid = fs.readFileSync( childPid );
+ process.kill( pid );
+ process.exit(0);
+});
process.on('SIGHUP', function() {
var pid = fs.readFileSync( childPid );
process.kill( pid, 'SIGHUP' );
});
-}())
+
+}());
View
1  lib/signals.js
@@ -29,6 +29,7 @@ var _terminate = function __terminate( _live, _end ) {
var signals = function _signals( _live, _end, _forkChild ) {
process.on( 'SIGHUP', function() { _restart( _live, _end, _forkChild ) } );
process.on( 'SIGTERM', function() { _terminate( _live, _end ) } );
+ process.on( 'SIGINT', function() { _terminate( _live, _end ) } );
}
module.exports = signals;
View
5 package.json
@@ -1,7 +1,7 @@
{
"name" : "clusterman",
"description" : "A package to provide some basic utility around cluster - notably graceful restart",
- "version" : "0.0.3",
+ "version" : "0.0.3.1",
"author": {
"name" : "Ordr.in",
@@ -16,7 +16,8 @@
"main" : "index.js",
"bin" : {
- "clusterman" : "./bin/clusterman"
+ "clusterman" : "./bin/clusterman",
+ "mkclusterman" : "./bin/mkclusterman"
},
"dependencies": {
Please sign in to comment.
Something went wrong with that request. Please try again.