Skip to content

Commit

Permalink
Merge pull request #1 from Unitech/development
Browse files Browse the repository at this point in the history
merge dev-branch from Unitech/PM2
  • Loading branch information
Tjatse committed Nov 22, 2014
2 parents bf32318 + c2e3581 commit 126fecf
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 90 deletions.
13 changes: 10 additions & 3 deletions bin/pm2
Expand Up @@ -515,10 +515,17 @@ commander.command('kill')
//
// Update repository for a given app
//
commander.command('pull <name>')

commander.command('pull <name> [commit_id]')
.description('updates repository for a given app')
.action(function(pm2_name) {
CLI.pullAndRestart(pm2_name);
.action(function(pm2_name, commit_id) {
if (commit_id !== undefined) {
CLI.pullCommitId(
{pm2_name: pm2_name,
commit_id: commit_id});
}
else
CLI.pullAndRestart(pm2_name);
});

//
Expand Down
5 changes: 3 additions & 2 deletions doc/PULL.md
Expand Up @@ -26,9 +26,10 @@ Switches your local repository to the next (more recent) commit if there is one.


```bash
$ pm2 pull <app name>
$ pm2 pull <app name> [commit ID]
```
Updates your local repository to the most recent remote commit for the current branch.
Updates your local repository to the most recent remote commit for the current branch
or to the optional specified commit ID.



Expand Down
12 changes: 11 additions & 1 deletion lib/CLI.js
Expand Up @@ -1393,7 +1393,7 @@ CLI.infoInteract = function(cb) {
});
};

var Version = require('./tools/VersionManagement');
var Version = require('./tools/VersionManagement.js');

/**
* CLI method for updating a repository
Expand Down Expand Up @@ -1425,6 +1425,16 @@ CLI.pullAndGracefulReload = function (process_name, cb) {
Version._pull({process_name: process_name, action: 'gracefulReload'}, cb);
}

/**
* CLI method for updating a repository to a specific commit id
* @method pullCommitId
* @param {object} opts
* @return
*/
CLI.pullCommitId = function (opts, cb) {
Version.pullCommitId(opts.pm2_name, opts.commit_id);
}

/**
* CLI method for downgrading a repository to the previous commit (older)
* @method backward
Expand Down
56 changes: 1 addition & 55 deletions lib/Common.js
Expand Up @@ -18,67 +18,13 @@ var Stringify = require('json-stringify-safe');
var Satan = require('./Satan.js');

var InteractorDaemonizer = require('./Interactor/InteractorDaemonizer.js');

/**
* Common methods (used by CLI and God)
*/

var Common = module.exports;

/**
* Start log outgoing messages
* @method startLogging
* @param {} callback
* @return
*/
Common.startLogging = function(stds, callback) {
// Make sure directories of `logs` and `pids` exist.
try {
['logs', 'pids'].forEach(function(n){
(function(_path){
!fs.existsSync(_path) && fs.mkdirSync(_path, '0755');
})(path.resolve(cst.PM2_ROOT_PATH, n))
});
}catch(err){
return callback(new Error('can not create directories (logs/pids):' + err.message));
}

// waterfall.
var flows = [];
// types of stdio, should be sorted as `std(entire log)`, `out`, `err`.
var types = Object.keys(stds).sort(function(x, y){
return -x.charCodeAt(0) + y.charCodeAt(0);
});

// Create write streams.
(function createWS(io){
if(io.length != 1){
return;
}
io = io[0];

// If `std` is a Stream type, try next `std`.
// compatible with `pm2 reloadLogs`
if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
return createWS(types.splice(0, 1));
}

flows.push(function(next){
var file = stds[io];
stds[io] = fs.createWriteStream(file, {flags: 'a'})
.on('error', function(err){
next(err);
})
.on('open', function(){
next();
});
stds[io]._file = file;
});
createWS(types.splice(0, 1));
})(types.splice(0, 1));

async.waterfall(flows, callback);
}

/**
* Resolve app paths and replace missing values with defaults.
* @method resolveAppPaths
Expand Down
27 changes: 17 additions & 10 deletions lib/God/ActionMethods.js
Expand Up @@ -226,6 +226,7 @@ module.exports = function(God) {
proc.disconnect();
} catch (e) {
// Fallback on disconnect method fail
console.log('Could not disconnect process', e.stack || e);
clearTimeout(timeout);
proc.removeListener('disconnect', onDisconnect);
if (proc && proc.process && proc.process.pid) {
Expand Down Expand Up @@ -491,18 +492,24 @@ module.exports = function(God) {
processIds.forEach(function (id) {
var cluster = God.clusters_db[id];

if (cluster.pm2_env.exec_mode == 'cluster_mode')
cluster.send({type:'log:reload'});
else // Fork mode
cluster._reloadLogs(function(err) {
if (err) {
God.logAndGenerateError(err);
return cb(new Error(err));
};
return false;
});
console.log('Reloading logs for process id %d', id);

if (cluster &&
cluster.pm2_env) {
if (cluster.send &&
cluster.pm2_env.exec_mode == 'cluster_mode') {
cluster.send({
type:'log:reload'
});
}
else if (cluster._reloadLogs) {
cluster._reloadLogs(function(err) {
if (err) God.logAndGenerateError(err);
});
}
}
});

return cb(null, {});
};

Expand Down
5 changes: 2 additions & 3 deletions lib/God/ForkMode.js
Expand Up @@ -11,7 +11,6 @@ var fs = require('fs');
var cst = require('../../constants.js');
var moment = require('moment');
var Common = require('../Common');

var Utility = require('../Utility.js');

/**
Expand Down Expand Up @@ -73,7 +72,7 @@ module.exports = function ForkMode(God) {
stds.std = pm2_env.pm_log_path;
}

Common.startLogging(stds, function(err, result) {
Utility.startLogging(stds, function(err, result) {
if (err) {
God.logAndGenerateError(err);
return cb(err);
Expand Down Expand Up @@ -169,7 +168,7 @@ module.exports = function ForkMode(God) {
stds[k] = stds[k]._file;
}
cspr.removeAllListeners();
Common.startLogging(stds, cb);
Utility.startLogging(stds, cb);
};

cspr.unref();
Expand Down
14 changes: 7 additions & 7 deletions lib/ProcessContainer.js
Expand Up @@ -6,11 +6,11 @@
if (process.env.name != null)
process.title = 'PM2 v' + process.env._pm2_version + ': ' + process.env.name;

var fs = require('fs');
var p = require('path');
var cst = require('../constants');
var Common = require('./Common');
var axm = require('axm');
var fs = require('fs');
var p = require('path');
var cst = require('../constants');
var Utility = require('./Utility.js');
var axm = require('axm');

/**
* Main entrance to wrap the desired code
Expand Down Expand Up @@ -88,7 +88,7 @@ function exec(script, stds) {
stds[k].close();
stds[k] = stds[k]._file;
}
Common.startLogging(stds, function (err) {
Utility.startLogging(stds, function (err) {
if(err){
console.error('Failed to reload logs:', err.stack);
}else {
Expand All @@ -103,7 +103,7 @@ function exec(script, stds) {
if (process.env.log_date_format)
moment = require('moment');

Common.startLogging(stds, function (err) {
Utility.startLogging(stds, function (err) {
if (err) {
process.send({
type : 'process:exception',
Expand Down
59 changes: 59 additions & 0 deletions lib/Utility.js
@@ -1,11 +1,70 @@

var Stringify = require('json-stringify-safe');
var fs = require('fs');
var path = require('path');
var cst = require('../constants.js');
var async = require('async');

var Utility = module.exports = {
getDate : function() {
return Math.round(Date.now() / 1000);
},
serialize : function(data) {
return JSON.parse(Stringify(data));
},
startLogging : function(stds, callback) {
/**
* Start log outgoing messages
* @method startLogging
* @param {} callback
* @return
*/
// Make sure directories of `logs` and `pids` exist.
try {
['logs', 'pids'].forEach(function(n){
(function(_path){
!fs.existsSync(_path) && fs.mkdirSync(_path, '0755');
})(path.resolve(cst.PM2_ROOT_PATH, n));
});
}catch(err){
return callback(new Error('can not create directories (logs/pids):' + err.message));
}

// waterfall.
var flows = [];
// types of stdio, should be sorted as `std(entire log)`, `out`, `err`.
var types = Object.keys(stds).sort(function(x, y){
return -x.charCodeAt(0) + y.charCodeAt(0);
});

// Create write streams.
(function createWS(io){
if(io.length != 1){
return false;
}
io = io[0];

// If `std` is a Stream type, try next `std`.
// compatible with `pm2 reloadLogs`
if(typeof stds[io] == 'object' && !isNaN(stds[io].fd)){
return createWS(types.splice(0, 1));
}

flows.push(function(next){
var file = stds[io];

stds[io] = fs.createWriteStream(file, {flags: 'a'})
.on('error', function(err){
next(err);
})
.on('open', function(){
next();
});
stds[io]._file = file;
});
return createWS(types.splice(0, 1));
})(types.splice(0, 1));

async.waterfall(flows, callback);
}
};
26 changes: 21 additions & 5 deletions lib/Worker.js
Expand Up @@ -20,12 +20,12 @@ module.exports = function(God) {

if (!(proc &&
proc.pm2_env &&
proc_key.monit &&
proc_key.monit.memory !== undefined &&
proc.pm2_env.max_memory_restart !== undefined))
proc_key.monit))
return cb();

if (proc.pm2_env.max_memory_restart < (proc_key.monit.memory / (1024*1024))) {
if (proc_key.monit.memory !== undefined &&
proc.pm2_env.max_memory_restart !== undefined &&
proc.pm2_env.max_memory_restart < (proc_key.monit.memory / (1024*1024))) {
console.log('[PM2][WORKER] Process %s restarted because it exceeds --max-memory-restart value',
proc.pm2_env.pm_id);
God.restartProcessId({
Expand All @@ -37,8 +37,24 @@ module.exports = function(God) {
return cb();
});
}
else
else if (proc.pm2_env.status !== undefined &&
proc_key.monit.memory !== undefined &&
proc.pm2_env.status === cst.ONLINE_STATUS &&
proc_key.monit.memory === 0) {
console.log('[PM2][WORKER] Process %s restarted because it uses 0 memory and has ONLINE status',
proc.pm2_env.pm_id);
God.restartProcessId({
id: proc.pm2_env.pm_id,
env: proc.pm2_env.env
}, function(err, data) {
if (err)
console.error(err.stack || err);
return cb();
});
}
else {
return cb();
}
};

var versioningRefresh = function(proc_key, cb) {
Expand Down

0 comments on commit 126fecf

Please sign in to comment.