Skip to content

Commit

Permalink
Unitech#2968 pm2 attach <pm_id>
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Jun 29, 2017
1 parent b71ec58 commit 90f43d2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.5.1

- #2968 pm2 attach <pm_id> allows to attach to process stdin / stdout
- #2951 pm2 reload command locker via timestamped lock file
- #2977 pm2 reloadLogs protected
- Alias pm2.link and pm2.unlink to pm2.interact and pm2._pre_interact
Expand Down
10 changes: 10 additions & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ commander.command('send <pm_id> <line>')
pm2.sendLineToStdin(pm_id, line);
});

//
// Attach to stdin/stdout
// Not TTY ready
//
commander.command('attach <pm_id> [command separator]')
.description('attach stdin/stdout to application identified by <pm_id>')
.action(function(pm_id, separator) {
pm2.attach(pm_id, separator);
});

//
// Resurrect
//
Expand Down
3 changes: 2 additions & 1 deletion examples/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ rl.on('line', (data) =>{
});

setInterval(() => {
}, 100);
//console.log('Yes');
}, 2000);
59 changes: 50 additions & 9 deletions lib/API/Extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,70 @@ module.exports = function(CLI) {

/**
* Description
* @method sendDataToProcessId
* @method sendLineToStdin
*/
CLI.prototype.sendLineToStdin = function(pm_id, line, cb) {
CLI.prototype.sendLineToStdin = function(pm_id, line, separator, cb) {
var that = this;

if (!cb && typeof(separator) == 'function') {
cb = separator;
separator = null;
}

var packet = {
id : pm_id,
line : line
pm_id : pm_id,
line : line + (separator || '\n')
};

that.Client.executeRemote('sendLineToStdin', {
pm_id : pm_id,
line : line
}, function(err, res) {
that.Client.executeRemote('sendLineToStdin', packet, function(err, res) {
if (err) {
Common.printError(cst.PREFIX_MSG_ERR + err);
return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
}
Common.printOut(cst.PREFIX_MSG + 'Line "%s" sent to process id "%s"', line, pm_id);
return cb ? cb(null, res) : that.speedList();
});
};

/**
* Description
* @method attachToProcess
*/
CLI.prototype.attach = function(pm_id, separator, cb) {
var that = this;
var readline = require('readline');

if (isNaN(pm_id)) {
Common.printError('pm_id must be a process number (not a process name)');
return cb ? cb(Common.retErr('pm_id must be number')) : that.exitCli(cst.ERROR_EXIT);
}

process.on('SIGINT', function() {
return cb ? cb() : that.exitCli(cst.SUCCESS_EXIT);
});

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

that.Client.launchBus(function(err, bus, socket) {
if (err) {
Common.printError(err);
return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
}

bus.on('log:*', function(type, packet) {
if (packet.process.pm_id != pm_id)
return;
process.stdout.write(packet.data);
});
});

rl.on('line', (line) =>{
that.sendLineToStdin(pm_id, line, separator, function() {});
});
};

/**
* Description
* @method sendDataToProcessId
Expand Down
5 changes: 3 additions & 2 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,11 @@ module.exports = function(God) {
*/
God.sendLineToStdin = function(packet, cb) {
if (typeof(packet.pm_id) == undefined || !packet.line)
return cb(God.logAndGenerateError('pm_id or line field is missing'), {});
return cb(God.logAndGenerateError('pm_id or line field missing'), {});

var pm_id = packet.pm_id;
var line = packet.line;

var proc = God.clusters_db[pm_id];

if (!proc)
Expand All @@ -620,7 +621,7 @@ module.exports = function(God) {
return cb(God.logAndGenerateError('Process with ID <' + pm_id + '> offline.'), {});

try {
proc.stdin.write(line + '\n', function() {
proc.stdin.write(line, function() {
return cb(null, {
pm_id : pm_id,
line : line
Expand Down
20 changes: 20 additions & 0 deletions test/bash/attach.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

SRC=$(cd $(dirname "$0"); pwd)
source "${SRC}/include.sh"

echo -e "\033[1mRunning tests:\033[0m"

cd $file_path/stdin

$pm2 start stdin.js -o out-rel.log --merge-logs
>out-rel.log

# Send LINE\n to stdin application
$pm2 send 0 "LINE"

cat out-rel.log
grep "LINE" out-rel.log
spec "Should have reveived line"

$pm2 delete all
2 changes: 2 additions & 0 deletions test/pm2_behavior_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ bash ./test/bash/increment-var.sh
spec "Increment env variables"
bash ./test/bash/instance-number.sh
spec "Negative instance number spawn one worker"
bash ./test/bash/attach.sh
spec "pm2 attach method"

# Issues related
bash ./test/bash/issues/2337.sh
Expand Down

0 comments on commit 90f43d2

Please sign in to comment.