Created
September 8, 2012 04:22
-
-
Save nanha/3671768 to your computer and use it in GitHub Desktop.
Node.js 에서 실행할 수 있는 외부 솔루션을 어떻게 컨트롤 하는가?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Node.js 에서 실행되는 외부 솔루션을 사용하는 경우는 자주 발생합니다. 제가 사용하는 방법은 아래와 같습니다. | |
$ cat subprocess.js | |
/** | |
* subprocess module | |
* | |
* @author nanhapark | |
*/ | |
var spawn = require('child_process').spawn; | |
/** | |
* subprocess | |
* | |
* @param {String} processname | |
* @param {Array} arg | |
* @param {Object} cb | |
*/ | |
function subprocess(processname, arg, cb) { | |
var p = spawn(processname, arg); | |
p.on('exit', cb.exit); | |
p.stdout.on('data', cb.stdout || function (out) { | |
process.stdout.write(out); | |
}); | |
p.stderr.on('data', cb.stderr || function (err) { | |
process.stdout.write(err); | |
}); | |
}; | |
module.exports = subprocess; | |
이 모듈을 사용할려면 아래와 같이 호출합니다. | |
var subprocess = require('./subprocess'), | |
util = require('util'); | |
// | |
// spawn variables | |
// 예를 들어 pyhton 모듈을 실행하는 예제입니다. | |
// | |
var processname = 'python'; | |
var p = []; | |
p.push('/your/module/path/process.py'); | |
p.push(util.format('-a%s', argv1)); | |
p.push(util.format('-b%s', argv2)); | |
// | |
// subprocess : ssh | |
// | |
subprocess(processname, p, { | |
stdout: function(out) { | |
console.log(out); | |
}, | |
stderr: function(out) { | |
}, | |
exit: function(code) { | |
// | |
// 이 부분에서는 code 가 전달되는데 | |
// 모듈 스크립트에서 에러코드를 반환하는 부분입니다. | |
// 예를들어 0~127 으로 사용하는 bash errorcode 같은 형식입니다. | |
// pyhton; sys.exit(0), sys.exit(127) 이런식으로요. 이를 판단하여 에러로그를 처리합니다. | |
// | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment