Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose some pm2 cli interface via hubot command. #21

Open
chitacan opened this issue Jan 18, 2015 · 3 comments
Open

expose some pm2 cli interface via hubot command. #21

chitacan opened this issue Jan 18, 2015 · 3 comments

Comments

@chitacan
Copy link
Contributor

if we can restart hubot on chat room (like hubot restart), it could be nice.

we can control pm2 on script.

@imazine
Copy link
Contributor

imazine commented Jan 29, 2015

💯

@mooyoul
Copy link

mooyoul commented Apr 20, 2015

You can use pm2.restart via PM2 API. It's already exposed.

This is an example snippet which handle pm2 via Express.

  • Note: hubot, or express will NOT send any responses after restart/start command was executed. because pm2 immediately kills process.
/** Module depdendencies. */
var pm2 = require('pm2'),
    Promise = require('bluebird'),
    fs = require('fs'),
    _ = require('underscore');

/** PM2 Promises */
var pm2connect = Promise.promisify(pm2.connect),
    pm2list = Promise.promisify(pm2.list),
    pm2restart = Promise.promisify(pm2.restart),
    pm2stop = Promise.promisify(pm2.stop);


var DevOps = module.exports = exports = {};

DevOps.getPM2List = function (req, res) {
    pm2connect().then(function () {
        return pm2list();
    }).then(function (list) {
        res.send(list);
    }).catch(function (e) {
        res.sendException(500, {reason: e.message});
    });
};

DevOps.PM2Restart = function (req, res) {
    pm2connect().then(function () {
        return pm2restart(process.env.PM2_APP_NAME);
    }).then(function () {
        return pm2.list();
    }).then(function (list) {
        res.send(list);
    }).catch(function (e) {
        res.sendException(500, {reason: e.message});
    });
};

DevOps.PM2Stop = function (req, res) {
    pm2connect().then(function () {
        return pm2stop(process.env.PM2_APP_NAME);
    }).then(function () {
        return pm2.list();
    }).then(function (list) {
        res.send(list);
    }).catch(function (e) {
        res.sendException(500, {reason: e.message});
    });
};

DevOps.getPM2StdoutLog = function (req, res) {
    pm2connect().then(function () {
        return pm2list();
    }).then(function (list) {
        fs.createReadStream(_.pluck(list, 'pm2_env')[0]['pm_out_log_path'])
            .pipe(res);
    }).catch(function (e) {
        res.sendException(500, {reason: e.message});
    });
};

DevOps.getPM2StderrLog = function (req, res) {
    pm2connect().then(function () {
        return pm2list();
    }).then(function (list) {
        fs.createReadStream(_.pluck(list, 'pm2_env')[0]['pm_err_log_path'])
            .pipe(res);
    }).catch(function (e) {
        res.sendException(500, {reason: e.message});
    });
}; 

@chitacan
Copy link
Contributor Author

@mooyoul cool !! that really can help us 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants