Created
September 29, 2016 15:53
-
-
Save imlucas/2527c76bde20afa7db18a79e2014c778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {mongodb#Database} db | |
* @param {String} ns `${db}.${collection}` See http://npm.im/mongodb-ns | |
* @param {String} name - The index name to get progress on. | |
* | |
* @see https://docs.mongodb.com/v3.0/reference/method/db.currentOp/#active-indexing-operations | |
*/ | |
function getIndexBuildProgress(db, ns, name) { | |
/** | |
* TODO (imlucas) Update query to constrain on `name`. | |
*/ | |
const query = { | |
ns: ns, | |
$or: [ | |
{op:"query", "query.createIndexes":{$exists: true}}, | |
{op: "insert", ns: /\.system\.indexes\b/} | |
] | |
}; | |
/** | |
* Result struct: | |
* | |
* const res = { | |
* state: 'UNKNOWN', | |
* message: '', | |
* completed: 0, | |
* total: 0, | |
* indeterminate: false | |
* }; | |
*/ | |
const onNoResults = () => { | |
getIndexDetails(ns, name, (err, index) => { | |
if (err) { | |
return done(err); | |
} | |
if (index) { | |
return done(null, { | |
state: 'COMPLETE', | |
message: `${name} on ${ns} has been built successfully!` | |
}); | |
} | |
return done(null, { | |
state: 'UNKNOWN', | |
indeterminate: true, | |
message: `The current status of building ${name} on ${ns} could not be determined` | |
}); | |
}); | |
}; | |
db.currentOp(query, (err, ops) => { | |
if (err) { | |
if (isNotAuthorized(err)) { | |
return done(null, { | |
state: 'UNAUTHORIZED', | |
indeterminate: true, | |
message: `Building ${name} on ${ns}` | |
}); | |
} | |
return done(err); | |
} | |
if (ops.length === 0) { | |
return onNoResults(); | |
} | |
if (ops.length > 1) { | |
warn(`Expected to get single result from currentOp but got ${ops.length}`); | |
} | |
const op = _.first(ops); | |
/** | |
* @see also | |
* - https://www.npmjs.com/package/request-progress | |
* - https://www.npmjs.com/package/progress | |
*/ | |
return done(null, { | |
state: 'PROGRESS', | |
completed: op.done, | |
total: op.total, | |
message: op.msg | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment