Seeking Guidance on troubleshooting node memory issue

87 Views Asked by At

I'm trying to implement a message queue using node, socket.io, and redis. I am attempting to follow the reliable queue pattern outlined Here. I am trying to read a logfile (60M in size) in line-by-line (will be changing this later) and pump the lines into the queue for processing later. However, I am running into a memory allocation issue. I'm not sure how to troubleshoot this and would like some guidance on where to start. I can't tell if the issue is in reading the file, or in the redis client. I have been able to add messages to the queue one by one like this :

socket.emit('message', 'some sort of log line here');

Therefore I know the listener is working, but when I run the socketClient.js file It will spin out for a bit and then ultimately fail with the following generic error message:

FATAL ERROR: JS Allocation failed - process out of memory

Is there some error handling, or profiling I can add to get more information on where this is failing?

Here is the code:

socketListener.js

var util = require("util"),
redis = require("redis"),
io = require('socket.io').listen(8088)

client = redis.createClient("7777", "localhost");

util.log("Established connection to redis");

io.sockets.on('connection', function(socket) {

    util.log("socket connection established for socket : " + socket);

    socket.on('message', function (data) {
         util.log("received the following data : ");
         util.log(JSON.stringify(data, 0, 3));
         client.on("error", function(err) {
            util.log("Error " + err);
        });

        try {
            // reliable queue pattern implementation
            util.log("queuing up the data in the list");
            client.rpush('logList', data);
            client.brpoplpush('logList', 'dequeueList', 10); 
        } catch (err) {
            util.log("An error occurred : ");
            util.log(JSON.stringify(err, 0, 3));
        }
    });

    socket.on('disconnect', function() {});

});

socketClient.js

var io = require("socket.io-client");
var socket = io.connect('http://localhost:8088');
var redis = require('redis');
var util = require('util');
var fs = require('fs');
var readline = require('readline');

socket.on('connect', function() {
client = redis.createClient("7777", "localhost");   
    var rd = readline.createInterface({
    input: fs.createReadStream('someLogFile.log'),
    terminal: false
    });

rd.on('line', function(line) {
    util.log("reading line " + line);
    socket.emit('message', line);
});

    client.lrange('dequeueList', 0, -1, function(err, results) {
        if (err) {
           util.log(err);
        } else {
            var multi = client.multi();
            for (var i=0; i < results.length; i++) {
                util.log('got : ' + results[i]);
                multi.hgetall(results[i]);
            }
            multi.exec(function(err, logs) {
                util.log("executing the multi commands");
                util.log(JSON.stringify(logs, 0 ,3));
            })
        };
    });

})

Thank you in advance for the help!

0

There are 0 best solutions below