Friday 19 May 2017

How to run and handle errors in child process spawn in node

I'm using node child_process to spawn processes in the system. I can control the data output from stdin, stderr, etc, using callbacks fired on the data, error, end and exit events.

The problem is that when I load the executable like here:

/**
 * Load model in memory
 */
Executable.prototype.load = function() {
    var self=this;
    this.onExitCallback=null;
    this.onDataCallback=null;
    this.onErrorCallback=null;
    return new Promise(function(resolve, reject) {
        self.emitDeferred(EVENT_LOAD_START);
        if( self.child ) {
            self.emitDeferred(EVENT_LOAD_DONE);
            return resolve(true);
        }
        self.onErrorCallback = function(error) {
            return reject(error);
        }
        var args =[
            '-input',
            '-'
        ];
        if( self._options.debug) console.log("load",args);
        self.child=self.exec('runner', self._options.bin, args, self._options.child);
        if(self._options.debug) self.child.stdout.pipe(process.stdout);
        self.emitDeferred(EVENT_LOAD_DONE);
        return resolve(true);
    });
}//load

and I get an error on the error callback, I will have the resolve and the rejectcalled, since the latter will be fired by the onErrorCallback, while the first will be fired immediately after the spawn, while I need to avoid the resolve while keepeing the onErrorCallback.

This is my exec code:

    /**
     * Execute child process
     */
    this.exec = function(name,command,params,options) {
        var self=this;
        var _options = { detached: false };
        for (var attrname in options) { _options[attrname] = options[attrname]; }

        var created_time= ( new Date() ).toISOString();
        var task = require('child_process').spawn(command, params, _options);
        task.stdout.on('data', function(_data) {
            if( self.onDataCallback ) self.onDataCallback.apply(self,[ new Buffer(_data,'utf-8').toString() ] );
        });
        task.on('error', function(error) {
            if(self._options.debug) console.error("exec:%s pid:%d error:\n",name,task.pid,error);
            if( self.onErrorCallback ) self.onErrorCallback.apply(self,[error]);
        });
        task.on('uncaughtException', function (error) {
            if(self._options.debug) console.error("exec:%s pid:%s uncaughtException:\n",name,task.pid,error);
            if( self.onErrorCallback ) self.onErrorCallback.apply(self,[error]);
        });
        task.stdout.on('end', function(data) {
            if(self._options.debug) console.log("exec:%s end.",name);
        });
        task.stderr.on('data', function(data) {
            //if(self._options.debug) console.log("exec:%s stderr:%s",name,data);
            //if(!data) task.kill('SIGINT');
        });
        task.on('close', (code, signal) => {
            if(self._options.debug) console.warn('task:%s pid:%s terminated due to receipt of signal:%s',name,task.pid,signal);
        });
        task.on('exit', function(code) {
            if(self._options.debug) console.log("exec:%s exit.",name);
            if (code != null && code != 0) {
                var error=new Error();
                error.description= command + ' ' + params.join(' ');
                error.code=code;
                if(self._options.debug) console.error("exec:%s pid:%d error:\n",name,task.pid,error);
                task.kill('SIGINT');
                if( self.onExitCallback ) self.onExitCallback.apply(self,[error]);
            } else {
                // at exit explicitly kill exited task
                task.kill('SIGINT');
                if( self.onExitCallback ) self.onExitCallback.apply(self);
            }
        });
        return task;
    }//exec

and the send code to send raw data to the running child process:

    /**
     * Send data to child process
     */
    this.send = function(data) {
        this.child.stdin.setEncoding('utf-8');
        this.child.stdin.write( data + '\r\n' );
    }//send

So to test it just do like

myExecutable.load()
.then(done => {
    console.log("loaded %d", done);
})
.catch(error => {
    console.error("error",error);
});



via loretoparisi

No comments:

Post a Comment