Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node https callback not firing when loop exits

My problem is probably straight forward, but I can't figure out what's happening behind the scenes. I'm looping through a series of domains in a database table, calling out to them, grabbing the SSL certificate and storing information about it back into the database.

For the most part, it's working - except when the loop exits any calls that haven't get completed just stop dead.

Database retrieval that begins check:

function queryRows() {
    complete = false;
    var query = c.query("SELECT * FROM domains LIMIT 100 OFFSET " + offset);
    query.on('result', function(res) {
        res.on('data', function(row) {
        checkUrl(row)
        }).on('end', function() {
            complete = true;
        });
    }).on('end', function() {
        console.log(complete);
        offset += 100;
        if(offset <= (parseInt(rows) + 400)){
            queryRows();
        } else {
            console.log("Done, waiting");
            setTimeoutPromise(600000, 'foobar').then((value) => {
                console.log("restarting")
                offset = 0;
                getTotal();
            });
        }
    });
}

And the code that checks the SSL:

function checkSSL(id, domain){
    complete = false 
    var options = {
        host: domain,
        rejectUnauthorized: false
    };

    callback = function(response) {
        var str = '';
        try {
            if(domain == "arstechnica.com"){
                console.log("Found ars - savingCertificate");
            }
            cert = response.connection.getPeerCertificate(true);
            complete = hasSSL(cert, domain, id);
            // updateDomainRecord(cert, domain, id)
        } catch (error){
            console.log(error);
            complete = true;
            noSSLRecord(domain, id);
        }
    }
    const req = https.request(options, callback);
    req.on('error', (e) => {
        // console.error(e);
    });
}

It's worth noting that if I put a console.log before https.request, I see it in my console. However any logs within the callback fail to trigger (because the callback itself never fires).

Again, some of the time the callback does. It is only near the end of the database loop where it appears to stop working. Any advice would be appreciated!

like image 269
Mblake Avatar asked Jul 03 '17 19:07

Mblake


Video Answer


1 Answers

Looks like your request is never being sent, so the callback will never get fired. Make sure your request is actually being sent, so you have to add one line in the end:

req.end();
like image 79
Shimon Brandsdorfer Avatar answered Oct 23 '22 04:10

Shimon Brandsdorfer