Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.currentPrograms. I would change the names to prevent confusion.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda