Skip to content

Commit

Permalink
Added support for NPM package manager
Browse files Browse the repository at this point in the history
See new documentation in the README for info.
  • Loading branch information
TheBanHammer committed Apr 8, 2016
1 parent aaa50c8 commit 9791b8c
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "musiqpad",
"version": "0.5.0b",
"name": "mqp-server",
"version": "0.5.2",
"description": "musiqpad self-hosted server",
"main": "start.js",
"main": "server-package.js",
"author": "musiqpad Team <support@musiqpad.com>",
"private": true,
"private": false,
"scripts": {
"start": "node ./start.js"
},
Expand All @@ -23,6 +23,8 @@
"nodemailer": "^2.1.0",
"request": "^2.67.0",
"ws": "^1.0.1",
"xoauth2": "^1.1.0"
"xoauth2": "^1.1.0",
"forever": "^0.15.1",
"ps-tree": "^1.0.1"
}
}
97 changes: 97 additions & 0 deletions server-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var childProcess = require('child_process');
var psTree = require('ps-tree');
var forever = require('forever');
var log = new(require('basic-logger'))({
showTimestamp: true,
prefix: "ServerContainer"
});

var extend = require('extend');

var server = function (params) {
var that = this;
this.settings = {
forever: {
enabled: false,
options: {
root: './logs',
pidPath: './pids',
sockPath: './sock',
debug: false,
stream: false
}
}
}
extend(true, this.settings, params);

this.start = function() {
if (this.settings.forever.enabled) {
forever.load(this.settings.forever.options);
that.pid = forever.start('./start.js');
}
else {
that.proc = runScript('./start.js', function (err) {
if (err) throw err;
});
}
};

this.stop = function() {
stopServer();
};

function stopServer() {
if (that.settings.forever.enabled) {
forever.stop();
}
else {
that.proc.kill('SIGINT');
//kill(that.pid);
}
log.info('Stopping Server Container');
}
}

function runScript(scriptPath, callback) {
var invoked = false;
var proc = childProcess.fork(scriptPath);

proc.on('error', function (err) {
if (invoked) return;
invoked = true;
callback(err);
});

proc.on('exit', function (code) {
if (invoked) return;
invoked = true;
//var err = code === 0 ? null : new Error('exit code ' + code);
callback();
});
return proc;
}

function kill(pid, signal, callback) {
signal = signal || 'SIGKILL';
callback = callback || function () {};
var killTree = true;
if(killTree) {
psTree(pid, function (err, children) {
[pid].concat(
children.map(function (p) {
return p.PID;
})
).forEach(function (tpid) {
try { process.kill(tpid, signal) }
catch (ex) { }
});
callback();
});
} else {
try { process.kill(pid, signal) }
catch (ex) { }
callback();
}
};

module.exports = server;

0 comments on commit 9791b8c

Please sign in to comment.