From e560a4c30ff79ed38bccf2eb8ecaca3dc3c13683 Mon Sep 17 00:00:00 2001 From: tknew2 Date: Fri, 14 Nov 2014 15:32:44 +0100 Subject: [PATCH] refactor kill interact --- lib/CLI.js | 5 +-- lib/Interactor/InteractorDaemonizer.js | 50 ++++++++++++---------- test/fixtures/ecosystem.json5 | 58 ++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 test/fixtures/ecosystem.json5 diff --git a/lib/CLI.js b/lib/CLI.js index 67c31389..763c9079 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -1386,17 +1386,16 @@ CLI.ilogs = function() { CLI.killDaemon = function(cb) { printOut(cst.PREFIX_MSG + 'Stopping PM2...'); - this.delete('all', function(err, list) { + CLI._operate('deleteProcessId', 'all', function(err, list) { printOut(cst.PREFIX_MSG + 'All processes has been stopped and deleted'); - InteractorDaemonizer.killDaemon(function() { + InteractorDaemonizer.killDaemon(function(err, data) { Satan.killDaemon(function(err, res) { if (err) printError(err); printOut(cst.PREFIX_MSG + 'PM2 stopped'); return cb ? cb(null) : exitCli(cst.SUCCESS_EXIT); }); }); - return false; }); }; diff --git a/lib/Interactor/InteractorDaemonizer.js b/lib/Interactor/InteractorDaemonizer.js index c8702124..cfbb25de 100644 --- a/lib/Interactor/InteractorDaemonizer.js +++ b/lib/Interactor/InteractorDaemonizer.js @@ -49,24 +49,32 @@ InteractorDaemonizer.ping = function(cb) { InteractorDaemonizer.killDaemon = function(cb) { - debug('Killing interactor #1 ping'); - InteractorDaemonizer.ping(function(online) { - debug('Interactor online', online); - if (!online) { - if (!cb) Common.printError('Interactor not launched'); - return cb ? cb({msg:'Interactor not launched'}) : Common.exitCli(cst.SUCCESS_EXIT); - } + // debug('Killing interactor #1 ping'); + // InteractorDaemonizer.ping(function(online) { + // debug('Interactor online', online); - InteractorDaemonizer.launchRPC(function() { - InteractorDaemonizer.rpc.kill(function(err) { - if (err) Common.printError(err); - setTimeout(function() { - InteractorDaemonizer.disconnectRPC(cb); - }, 100); - }); + // if (!online) { + // if (!cb) Common.printError('Interactor not launched'); + // return cb ? cb({msg:'Interactor not launched'}) : Common.exitCli(cst.SUCCESS_EXIT); + // } + + InteractorDaemonizer.launchRPC(function(err, data) { + if (err) { + setTimeout(function() { + InteractorDaemonizer.disconnectRPC(cb); + }, 100); + return false; + } + InteractorDaemonizer.rpc.kill(function(err) { + if (err) Common.printError(err); + setTimeout(function() { + InteractorDaemonizer.disconnectRPC(cb); + }, 100); }); - return false; }); + + //return false; + //}); }; /** @@ -112,7 +120,11 @@ InteractorDaemonizer.launchRPC = function(cb) { }); }; - this.client.sock.on('error', function(e) { + this.client.sock.once('reconnect attempt', function(e) { + return cb({success:false, msg:'reconnect attempt'}); + }); + + this.client.sock.once('error', function(e) { console.error('Error in error catch all on Interactor'); console.error(e.stack || e); }); @@ -120,15 +132,11 @@ InteractorDaemonizer.launchRPC = function(cb) { this.client.sock.once('connect', function() { generateMethods(function() { debug('Methods generated'); - cb(); + cb(null, {success:true}); }); }); this.client_sock = req.connect(cst.INTERACTOR_RPC_PORT); - - this.client_sock.on('error', function() { - console.log('Got errorrororo'); - }); }; /** diff --git a/test/fixtures/ecosystem.json5 b/test/fixtures/ecosystem.json5 new file mode 100644 index 00000000..d5882c42 --- /dev/null +++ b/test/fixtures/ecosystem.json5 @@ -0,0 +1,58 @@ +{ + /** + * This is a sample configuration file for PM2 + */ + + /** + * Here we declare the apps that must be managed by PM2 + * All options are listed here: + * https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#json-app-declaration + * + */ + apps : [ + + // First application + { + name : "API", + script : "app.js", + env: { + COMMON_VARIABLE: "true" + }, + env_production : { + NODE_ENV: "production" + } + }, + + // Second application + { + name : "WEB", + script : "web.js" + } + + ], + + + /** + * PM2 help you to deploy apps over your servers + * For more help go to : + * https://github.com/Unitech/PM2/blob/master/ADVANCED_README.md#deployment-pm2--090 + */ + deploy : { + production : { + user : "node", + host : "212.83.163.1", + ref : "origin/master", + repo : "git@github.com:repo.git", + path : "/var/www/production", + "post-deploy" : "pm2 startOrRestart ecosystem.json --env production" + }, + dev : { + user : "node", + host : "212.83.163.1", + ref : "origin/master", + repo : "git@github.com:repo.git", + path : "/var/www/development", + "post-deploy" : "pm2 startOrRestart ecosystem.json --env dev" + } + } +}