Skip to content

Instantly share code, notes, and snippets.

@undoZen
Created January 12, 2015 08:50
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 undoZen/51033b69cd0397034b8f to your computer and use it in GitHub Desktop.
Save undoZen/51033b69cd0397034b8f to your computer and use it in GitHub Desktop.
function isMasterRunning() {
var pidFilePath = path.join(__dirname, 'ds.pid');
var strpid;
var isRunning;
if (fs.existsSync(pidFilePath)) {
strpid = fs.readFileSync(pidFilePath, 'utf-8');
isRunning = require('is-running')(parseInt(strpid, 10));
}
if (isRunning) {
return strpid;
}
return false;
}
gulp.task('start', ['build-and-clean'], function () {
var strpid = isMasterRunning();
if (strpid) {
console.log('pm master is running. sending USR1 signal...');
exec('kill -s USR1 ' + strpid);
} else {
console.log('pm master is not running. spawning one...');
spawn(process.execPath, [path.join(__dirname, 'master.js')], {
cwd: __dirname,
env: assign({}, process.env, {
NODE_ENV: 'production'
}),
silent: true,
detached: true,
stdio: ['ignore', 'ignore', 'ignore']
})
.unref();
}
});
gulp.task('stop', function () {
var strpid = isMasterRunning();
if (strpid) {
console.log('pm master is running. terminating...');
exec('kill -s TERM ' + strpid);
} else {
console.log('pm master is not running. exiting...');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment