简体   繁体   中英

NodeJS Fork can't get childprocess to kill

I'm running against a wall here, maybe it's just a small problem where I can't see the solution due to my inexperience with NodeJS.

Right now I'm constructing a BT device which will be controlled by a master application and I have settled for the prototyping on a Raspberry PI 3 with NodeJS using the Bleno module.

So far everything worked fine, the device gets found and I can set and get values over Bluetooth. But to separate the different "programs" which the device could execute from the Bluetooth logic (because of loops etc.) I have opted to extract these into external NodeJS files.

The idea here was to use the NodeJS fork module and control the starting and stoppping of those processes through the main process.

But herein my problems start. I can fork the different JavaScript files without problem and these get executed, but I can't get them to stop and I don't know how to fix it.

Here's the code (simplified):

var util = require('util');
var events = require('events');
var cp = require('child_process');
...
var ProgramTypeOne = {
NONE:    0,
ProgramOne: 1,
...
};
...
var currentProgram=null;
...

function BLEDevice() {
  events.EventEmitter.call(this);
  ...
  this.currentProgram=null;
  ...
  }

util.inherits(BLELamp, events.EventEmitter);

BLELamp.prototype.setProgram = function(programType, programNumber) {
  var self = this;
  var result=0;

  if(programType=="ProgramTypeOne "){
    if(programNumber==1){
      killProgram();
      this.currentProgram=cp.fork('./programs/programOne');
      result=1;
    }else if(programNumber==2){
...
  }

  self.emit('ready', result);
};

...
module.exports.currentProgram = currentProgram;
...
function killProgram(){
  if(this.currentProgram!=null){
          this.currentProgram.kill('SIGTERM');
      }
}

There seems to be a problem with the variable currentProgram which, seemingly, never gets the childprocess from the fork call. As I have never worked extensivley with JavaScript, except some small scripts on websites, I have no idea where exactly my error lies. I think it has something to do with the handling of class variables.

The starting point for me was the Pizza example of Bleno.

Hope the information is enough and that someone can help me out.
Thanks in advance!

Since killProgram() is a standalone function outside of the scope of BLELamp , you need to call killProgram with the correct scope by binding BLELamp as this . Using apply ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply ) should resolve it. The following I would expect would fix it (the only line change is the one invoking killProgram ):

BLELamp.prototype.setProgram = function(programType, programNumber) {
  var self = this;
  var result=0;

  if(programType=="ProgramTypeOne "){
    if(programNumber==1){
      killProgram.apply(this);
      this.currentProgram=cp.fork('./programs/programOne');
      result=1;
    }else if(programNumber==2){
...
  }

  self.emit('ready', result);
};

As a side note, your code is kind of confusing because you have a standalone var currentProgram then a couple prototypes with their own bound this.currentProgram s. I would change the names to prevent confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM