Node js script TCP Socket server hangs or freezes

Viewed 173

I am receiving data from around 200 tracker devices and then insert that data into the database after doing some parsing. I am also using forever to keep the server running in the background. The issue is after a week or so the server hangs with no error. And I have to then restart the whole machine or stop the server and restart it again in order to make it work. I have a server on aws with t2.xlarge and 16 GB Ram. I have also tried to check forever logs but there is no error display over there. The server doesn't explode and no error occurs. For example, I have other node scripts and websites also running on the same server and those continuously work. Only This Gps tracker server hangs with no error. Sometimes it runs successfully for a month and sometimes just for two to three weeks

The code I am using is this

var net = require('net');
const cluster = require('cluster');
var numCPUs = require('os').cpus().length;
    if (cluster.isMaster) {
        // Fork workers.
        for (var i = 0; i < numCPUs; i++) {
            cluster.fork();
        }

        cluster.on('death', function(worker) {
            console.log('worker ' + worker.pid + ' died');
            cluster.fork();
        });
    } else {
        net.createServer(function(socket) {
            console.log('received connection...');

            socket.on("error", function(err) {
                console.log("socket error: ")
                console.log(err.stack);
                socket.destroy();
            });
            socket.on('data', function(data) {


                console.log(data.toString());
    });
    }

And The whole js file is here to check

https://codeshare.io/aYkkJN

No idea how to debug this or what causing this issue?

0 Answers
Related