node-background-processor icon indicating copy to clipboard operation
node-background-processor copied to clipboard

Getting TypeError: this._longRunningTask is not a function error

Open SharedMocha opened this issue 7 years ago • 1 comments

Code - var timers = require("timers"), http = require("http"), ___backgroundTimer;

process.on('message',function(msg){ console.log("Started 2");

this._longRunningTask = function(data){
    var finalArray = [];
    console.log("Started 3");
    for(var url in data){
        //TODO do something here to create the 'result'
        finalArray.push("result");
        console.log("Started 4");

    }

    //Send the results back to index.js
    if(finalArray != []){
        var data = {
            "error":null,
            "content":finalArray
        };

        try{
            console.log("Started 3.1");
            console.log("datadatadata ",data)
            process.send(data);
        }
        catch(err){
            console.log("retriever.js: problem with process.send() " + err.message + ", " + err.stack);
        }
    }
    else{
        console.log("retriever.js: no data processed");
    }
};

this._startTimer = function(){
    var count = 0;
    console.log("Started 33.1");
    ___backgroundTimer = timers.setInterval(function(){
        console.log("Started 33.2");

        try{
            var date = new Date();
            console.log("retriever.js: datetime tick: " + date.toUTCString());
            this._longRunningTask(msg.content);
        }
        catch(err){
            count++;
            if(count == 3){
                console.log("retriever.js: shutdown timer...too many errors. " + err.message);
                clearInterval(___backgroundTimer);
                process.disconnect();
            }
            else{
                console.log("retriever.js error: " + err.message + "\n" + err.stack);
            }
        }
    },msg.interval);
}

this._init = function(){
    if(msg.content != null || msg.content != "" && msg.start == true){
        this._startTimer();
        console.log("msg.contentmsg.content"+msg.content);
        console.log("Started 33");

    }
    else{
        console.log("retriever.js: content empty. Unable to start timer.");
        console.log("Started 44")
    }
}.bind(this)()

})

process.on('uncaughtException',function(err){ console.log("retriever.js: " + err.message + "\n" + err.stack + "\n Stopping background timer"); clearInterval(___backgroundTimer); })

SharedMocha avatar Apr 22 '18 17:04 SharedMocha

I'm not surprised, this code was built for a 0.x version of node.

My initial guess is you have to declare _longRunningTask , _startTimer and _init as a private variables first up at the top of the file.

andygup avatar Apr 23 '18 19:04 andygup