Skip to content

Commit

Permalink
chore: add unique id for each process
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Apr 23, 2018
1 parent 33984b6 commit 85a5ee0
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/God.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ God.handleExit = function handleExit(clu, exit_code, kill_signal) {
* Init new process
*/
God.prepare = function prepare (env, cb) {
// generate a new unique id for each processes
env.env.unique_id = Utility.generateUUID()

// if the app is standalone, no multiple instance
if (typeof env.instances === 'undefined') {
env.vizion_running = false;
Expand Down
5 changes: 5 additions & 0 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,13 @@ module.exports = function(God) {

var proc = Utility.clone(God.clusters_db[id].pm2_env);


delete proc.created_at;
delete proc.pm_id;
delete proc.unique_id;

// generate a new unique id for new process
proc.unique_id = Utility.generateUUID()

God.injectVariables(proc, function inject (_err, proc) {
return God.executeApp(Utility.clone(proc), function (err, clu) {
Expand Down
12 changes: 12 additions & 0 deletions lib/Utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ var Utility = module.exports = {

checkPathIsNull: function(path) {
return path === 'NULL' || path === '/dev/null';
},

generateUUID: function () {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4";
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
s[8] = s[13] = s[18] = s[23] = "-";
return s.join("");
}

};
94 changes: 94 additions & 0 deletions test/programmatic/id.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

process.chdir(__dirname);

var PM2 = require('../..');
var should = require('should');
var assert = require('assert')

describe('Unique ID verification', function() {
describe('when starting', function() {
var _id = null

before(function(done) {
PM2.kill(done);
});

before(function(done) {
PM2.delete('all', function() { done() });
});

it('should connect to PM2', function(done) {
PM2.connect(done);
});

it('should start a script', function(done) {
PM2.start('../fixtures/child.js', function(err) {
should(err).be.null();
PM2.list(function(err, list) {
should(err).be.null();
assert(list.length > 0)
assert(typeof list[0].pm2_env.unique_id === 'string')
_id = list[0].pm2_env.unique_id
done();
});
});
});

it('should stop app by id', function(done) {
PM2.stop(0, done);
});

it('should restart and not changed unique id', function(done) {
PM2.restart(0, (err) => {
should(err).be.null();
PM2.list(function(err, list) {
should(err).be.null();
assert(list.length > 0)
assert(typeof list[0].pm2_env.unique_id === 'string')
assert( list[0].pm2_env.unique_id === _id)
done();
});
});
});


it('should generate another unique id for new process', function(done) {
PM2.start('./../fixtures/child.js', { name: 'toto' }, function(err) {
assert(!err);
PM2.list(function(err, list) {
should(err).be.null();
assert(list.length === 2)
assert(typeof list[0].pm2_env.unique_id === 'string')
assert(typeof list[1].pm2_env.unique_id === 'string')
assert(list[0].pm2_env.unique_id !== typeof list[1].pm2_env.unique_id)
done();
});
});
});

it('should duplicate a process and have a new id', function(done) {
PM2.scale('child', 2, function(err) {
assert(!err);
PM2.list(function(err, list) {
should(err).be.null();
should(list.length).eql(3);
assert(typeof list[0].pm2_env.unique_id === 'string')
assert(typeof list[1].pm2_env.unique_id === 'string')
assert(typeof list[2].pm2_env.unique_id === 'string')
assert(list[0].pm2_env.unique_id !== typeof list[1].pm2_env.unique_id)
assert(list[1].pm2_env.unique_id !== typeof list[2].pm2_env.unique_id)
assert(list[0].pm2_env.unique_id !== typeof list[2].pm2_env.unique_id)
done();
});
});
});

after(function(done) {
PM2.delete('all', done);
});

after(function(done) {
PM2.kill(done);
});
});
});

0 comments on commit 85a5ee0

Please sign in to comment.