Node https callback not firing when loop exits
Asked Answered
A

1

6

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!

Awake answered 3/7, 2017 at 19:0 Comment(4)
If you add console.log(response) right after callback = function(response) { It logs something?Keneth
You're sure the callback sometimes fires? You don't appear to be calling req.end(), which is when your request actually gets sent. Your request appears to be perpetually waiting for you to write a body to it.Jangro
Why we have 2 end events in fetch data function?Exaltation
it's probably what @BrettBeatty noted, on top of this - what is the complete = hasSSL(cert, domain, id); it's not consistent with your function definition.Teirtza
T
6

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();
Teirtza answered 10/7, 2017 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.