Helping Hands
Helping Hands

Reputation: 5396

If condition directly goes to else always in my case in node js

I am counting list of process and based on process counter trying to perform something but if condition directly goes to ELSE regardless of process counter.

My code :

var express = require('express');
var app = express();
//var sleep = require('sleep');
var counter = 0;

app.get('/', function (req, res) {
   res.send(
    '<form action="/server" method="POST">' +
    '  <input type="submit" name="server" value="Run Script" />' +
    '</form>');
});

app.get('/counter', function (req, res) {
  res.end("Process Counter = " + counter);
});

app.post('/server', function (req, res) {
  var fork = require('child_process').fork;
  var child = fork('./client');
  counter++;

  child.on("close", function () { counter --; });

  if(counter > 2)
  { 
     app.get('/m', function (req, res) {
       res.end("Already multiple process running. = " + counter);
     });    
  }
  else
  {
     app.get('/m', function (req, res) {
       res.end("Single or No process running currently = " + counter);
     });     
  }

});

var server = app.listen(8081, function () {
   var host = server.address().address
   var port = server.address().port

   console.log("app listening at http://%s:%s", host, port)       
})

I want to pause / sleep server when process counter reach 10 and then process next queue requests once previous 10 completes so trying to setting like process list of processes in slots like 10,10,10 etc. I will try to manage it once if condition work proper based on counter. Counter seems return correct count.

Upvotes: 2

Views: 211

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

Few issues with your code. So I created a simple client.js

function test() {
    console.log("Running test");
    setTimeout(test, 10000);
}

test();

This is nothing but a infinite loop. Then I used app object for the global counter

if (!app.counter)
    app.counter = 0;

Next your post becomes below

app.post('/server', function (req, res) {

    var fork = require('child_process').fork;
    var child = fork('./client');
    app.counter++;
    child.on("close", function () {
        app.counter--;
    });

    res.send(200, "Current Processes - " + app.counter);
});

Also you were not ending the POST request, so I added res.send(200, "Current Processes - " + app.counter);

Next the /m endpoint is updated as below

app.get('/m', function (req, res) {
    if (app.counter > 2) {
        res.end("Already multiple process running. = " + app.counter);
    }
    else {
        res.end("Single or No process running currently = " + app.counter);
    }
});

So the final code is below

var express = require('express');
var app = express();
//var sleep = require('sleep');
if (!app.counter)
    app.counter = 0;


app.get('/', function (req, res) {
    res.send(
        '<form action="/server" method="POST">' +
        '  <input type="submit" name="server" value="Run Script" />' +
        '</form>');
});

app.get('/counter', function (req, res) {
    res.end("Process Counter = " + app.counter);
});

app.get('/m', function (req, res) {
    if (app.counter > 2) {
        res.end("Already multiple process running. = " + app.counter);
    }
    else {
        res.end("Single or No process running currently = " + app.counter);
    }
});


app.post('/server', function (req, res) {

    var fork = require('child_process').fork;
    var child = fork('./client');
    app.counter++;
    child.on("close", function () {
        app.counter--;
    });

    res.send(200, "Current Processes - " + app.counter);
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log("app listening at http://%s:%s", host, port)

});

If you run the server, it will work fine now

$ curl -X POST http://127.0.0.1:8081/server
Current Processes - 1
$ curl -X POST http://127.0.0.1:8081/server
Current Processes - 2
$ curl -X POST http://127.0.0.1:8081/server
Current Processes - 3
$ curl -X POST http://127.0.0.1:8081/server
Current Processes - 4

And the /m endpoint

$ curl http://127.0.0.1:8081/m 
Already multiple process running. = 4

One key thing for testing this is to make sure that you don't launch the script using debugging, you run it. If you look at the child process spawnArgs you will find it as below

spawnargs

And our main process was started as /usr/local/bin/node --inspect-brk=54160 ..../test.js

So the fork tries to launch another process with --inspect-brk=54160, but that port is already busy with our debugger and doesn't let the child process run. So that is the reason you should run it directly instead of debugging the main code

Upvotes: 3

Related Questions