<commit_before>var util = require('util'),
    flags = require('flags'),
    MongoClient = require('mongodb').MongoClient,
    WebSocketServer = require('ws').Server,
    debug = util.debuglog('mongodb-replica-maintainer');

flags.defineString('ip', '0.0.0.0', 'listen ip');
flags.defineInteger('port', 27018, 'listen port');
flags.defineString('mongo', 'mongodb://127.0.0.1:27017', 'mongodb connect string');
flags.defineBoolean('clean', true, 'clean up dead replicas');
flags.parse();

function cleanupDeadReplicas(db) {
    var adminDB = db.admin();
    adminDB.command({replSetGetStatus: 1}, function(err, status) {
        var i = 0,
            idsToRemove = [];
        for (; i < status.members.length; i++) {
            //state of 1 means primary
            if (status.members[i].state !== 1 && !status.members[i].health && status.date - status.members[i].lastHeartbeatRecv > (3600 * 1000)) {
                idsToRemove.push(status.members[i]._id);
            }
        }
        if (idsToRemove.length < 1) {
            return;
        }
        db.collection('system.replset').findOne({}, function(err, doc) {
            if (err) {
                cb(err);
                return;
            }
            var changed = false;
            for (i = 0; i < doc.members.length; i++) {
                if (!idsToRemove.indexOf(doc.members[i]._id) !== -1) {
                    debug('Removing dead replica', doc.members[i].host);
                    doc.members.splice(i, 1);
                    i--;
                    changed = true;
                }
            }
            if (!changed) {
                return;
            }
            doc.version++;
            db.admin().command({replSetReconfig: doc}, function(err, res) {
                if (err) {
                    debug('Error removing dead replica', doc);
                }
            });
        });
    });
}

function addNewReplicaMember(db, host, hidden, cb) {
    db.collection('system.replset').findOne({}, function(err, doc) {
        if (err) {
            cb(err);
            return;
        }
        if (!doc || !Array.isArray(doc.members)) {<commit_msg>Fixed typo with removing replicas<commit_after>var util = require('util'),
    flags = require('flags'),
    MongoClient = require('mongodb').MongoClient,
    WebSocketServer = require('ws').Server,
    debug = util.debuglog('mongodb-replica-maintainer');

flags.defineString('ip', '0.0.0.0', 'listen ip');
flags.defineInteger('port', 27018, 'listen port');
flags.defineString('mongo', 'mongodb://127.0.0.1:27017', 'mongodb connect string');
flags.defineBoolean('clean', true, 'clean up dead replicas');
flags.parse();

function cleanupDeadReplicas(db) {
    var adminDB = db.admin();
    adminDB.command({replSetGetStatus: 1}, function(err, status) {
        var i = 0,
            idsToRemove = [];
        for (; i < status.members.length; i++) {
            //state of 1 means primary
            if (status.members[i].state !== 1 && !status.members[i].health && status.date - status.members[i].lastHeartbeatRecv > (3600 * 1000)) {
                idsToRemove.push(status.members[i]._id);
            }
        }
        if (idsToRemove.length < 1) {
            return;
        }
        db.collection('system.replset').findOne({}, function(err, doc) {
            if (err) {
                cb(err);
                return;
            }
            var changed = false;
            for (i = 0; i < doc.members.length; i++) {
                if (idsToRemove.indexOf(doc.members[i]._id) !== -1) {
                    debug('Removing dead replica', doc.members[i].host);
                    doc.members.splice(i, 1);
                    i--;
                    changed = true;
                }
            }
            if (!changed) {
                return;
            }
            doc.version++;
            db.admin().command({replSetReconfig: doc}, function(err, res) {
                if (err) {
                    debug('Error removing dead replica', doc);
                }
            });
        });
    });
}

function addNewReplicaMember(db, host, hidden, cb) {
    db.collection('system.replset').findOne({}, function(err, doc) {
        if (err) {
            cb(err);
            return;
        }
        if (!doc || !Array.isArray(doc.members)) {
{'fraction_non_alphanumeric': 0.1546820809248555, 'fraction_numerical': 0.016647398843930634, 'mean_word_length': 1.75717017208413, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4382291', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1248, 'n_words': 252}