Thursday 20 April 2017

number variables incrementation - unexpected numbers when running

given the following node\JavaScript code, for some reason the counters variables (failedCounter, successMatchedCounter, successUnmatchedCounter) are counted in an unexpectedly manner when running. I think that it's a matter of scopes and asynchronously but still can't spot the cause.

See the line of "//BUG: counted numbers are not as expected":

var MongoClient = require('mongodb').MongoClient;
var mysql = require('mysql2');
var fs = require('fs');

var dir = './logs';
if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
}

var testMode = false;

var mongoUrl = 'mongodb://xxx:27017/yy';
var mySqlconnString = {
    host: 'xxxxx',
    user: 'xxxx',
    password: 'xxxxx',
    database: 'xxxxx'
};

var connection = mysql.createConnection(mySqlconnString);

connection.connect(function(err) {
    if (err) {
        console.log('Error connecting to MySql DB');
        return;
    }

    console.log('Connection established to MySql DB');

    MongoClient.connect(mongoUrl, function(err, db) {

        if (err) {
            console.log('Error connecting to MongoDB');
            return;
        }

        console.log("Connection established to MongoDB");

        markSitesAsDeleted(db, function() {
            console.log('closing DBs connections..');
            connection.end(function(err) {});
            db.close();
        });
    });
});


var failedCounter = 0;
var successMatchedCounter = 0;
var successUnmatchedCounter = 0;
var totalCounter = 0;

var markSitesAsDeleted = function(db, closeConnectionsCallback) {
    console.log(`\nMigration process is starting..`);
    var cursor = db.collection('someCollection').find({
        "isDeleted": true
    });

    console.log(`Migrating data..\r\n`);

    cursor.each(function(err, siteDoc) {
        if (siteDoc != null) {
            var siteID = Math.trunc(siteDoc._id)

            if (testMode === false) {
                connection.query(`CALL MarkSiteAsDeleted_3(${siteID})`, function(error, rows) {
                    if (error) {
                        //TODO: Print error
                        failedCounter++;
                        fs.appendFileSync('logs/log.txt', `Error occured when calling MarkSiteAsDeleted_3 SP for SiteId=${siteID}. see error: ${JSON.stringify(error)}\n`);
                    } else {
                        if (rows.affectedRows === 1) { // Has match
                            successMatchedCounter++;
                        } else {
                            successUnmatchedCounter++;
                        }
                    }
                });
            }

            totalCounter++;

        } else {
            //BUG: counted numbers are not as expected
            fs.appendFileSync('logs/log.txt', `Total: ${totalCounter}, Success and Matched: ${successMatchedCounter}, Success but Unmatched: ${successUnmatchedCounter}, Failed: ${failedCounter}\r\n`);
            closeConnectionsCallback();
        }
    });
};



via Yair Nevet

No comments:

Post a Comment