Facing difficulty to stop the stream in child process spawn in Node.js?

橙三吉。 提交于 2020-01-05 09:14:13

问题


I am trying to use Node.js Child process spawn. Below code will execute the certain shell commands and read the data as buffer streams listener provided by spawn process. Bluebird node promise module is used to wrap around the child process.

var execSpawn = require('child_process').spawn;
var Promise = require('bluebird');

spawnAction = function(path, cmd, cb){
return function(resolve, reject){
    cmdExec = execSpawn(path, cmd);
    var fileData = {}
    var count = 0;
    cmdExec.stdout.on('data', function(data){
        if(cb){
            fileData = data.toString('utf8');
        }else{
            if(data =='.'){
                count +=1;
            }else{
                console.log(data.toString('utf8'));
            }
        }
    });
    cmdExec.stderr.on('data', function(data){
        if(reject){
            reject("error in scrubbing "+cmd.slice(-1) + " "+data.toString('utf8'));
        }
    });
    cmdExec.on('exit', function(){
     console.log("reach the exit event");
    })
    cmdExec.on('close', function(){
        if(cb){
            resolve(cb(fileData));
        }else{
            resolve(count);
        }
    });
}

}

Above method is called multiple times for parallel execution using the following ways:

promises = []
cp = new Promise(spawnAction(Path, ['cmd'],parsingMethodHandler));
cp.then(function(data){
    data.forEach(function(disk){
       var handler = new Promise(self.spawnAction(Path, "next command"));
       promises.push(handler);
   }
}.then(function(){
    Promise.all(promises).then(function(result){
        //certain actions
    }.catch(function(err){
        //catch error specific to the failure of promise handler
    })
}).catch(function(err){
  //error specific to the first promise
    })

And my problem is if in case any error during the execution of anyone promises it should reach "cmdExec.stderr.on" and do the reject the promise. Even after the error am getting console logs placed inside "cmdExec.stdout.on" streams, I couldn't stop receiving the buffer streams.

I would like to know is there any specific way to stop the child process execution if it faces any error and stopped by throwing the error instead of proceeding in receive the streams.

Thanks in advance for any suggestions or ideas.


回答1:


Thanks for the comments and suggestions. I figured out the way to resolve the problem of stop the Eventlistener binded to child process spawn. In the above code, whenever promise.all is called and in turn fired multiple handler with spawn process created along with various listeners. If one error occurred in any one of the spawn process under the promise will need to throw an error and stop all the created spawn process immediately.

My solution is store all the spawn process during creation in an array. On facing error with any one spawn process will call interrupt "SIGINT" to kill all actions immediately. This stops the process execution immediately and throw the error. You can find code snippet below

var execSpawn = require('child_process').spawn;
var Promise = require('bluebird');

var child_process_list = []

var spawnAction = function(path, cmd, cb){
    return function(resolve, reject, onCancel){
        cmdExec = execSpawn(path, cmd);
        child_process_list.push(cmdExec);
        var fileData = {}
        var count = 0;

        cmdExec.stdout.setEncoding('utf8');
        cmdExec.stdout.on('data', function(data){
            //Certain actions with filedata and count;
            console.log(data);
        });
        cmdExec.stderr.on('data', function(data){
            while(child_process_list.length > 0){
                proc = child_process_list.pop();
                proc.kill('SIGINT');
            }
            if(reject){
                reject("error "+cmd.slice(-1) + " "+data.toString('utf8'));
            }
        });

        cmdExec.on('close', function(){
                if(cb){
                   resolve(cb(fileData));
                }else{
                    resolve(count);
                }
        });
       }
    }

var handle = function(path, cmd, cb) {
    var promises = []
    cp = new Promise(spawnAction(path, ['cmd'],parsingMethodHandler));
    cp.then(function(data){
        data.forEach(function(disk){
            cmd.push(disk);
            var handler = new Promise(self.spawnAction(path, cmd));
            promises.push(handler);
        });
    }).then(function(){
        Promise.all(promises).then(function(result){
            console.log("result => ",result);
            child_process_list = [];
            cb();
        }).catch(function(err){
            console.log("error call => ",err);
            cb();
        })
    }).catch(function(err){
        console.log("Failed in fetching drive path available ",err);
        cb()
    })     
}

I would like to validate my approach and let me know any better approach is available to address this situation. Thanks in advance.



来源:https://stackoverflow.com/questions/52747777/facing-difficulty-to-stop-the-stream-in-child-process-spawn-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!