我尝试创建在mongodb上查询currentOps的nodejs应用程序
如果操作的运行时间>我预期的话,它将终止该操作
Mongodb版本:3.2
节点版本:11.3.0
Mongo连接器:MongoClient
function connectMongo(server){
return new Promise(resolve => {
url = "mongodb://" + server + "/"
MongoClient.connect(url,
{auth:
{
user:process.env.user,
password:process.env.password,
}
},
function(err, database) {
if (err) throw err;
const db = database.db('admin')
db.command({currentOp:1},function (err, data) {
if (err) throw err;
for(var i = 0; i<data.inprog.length;i++) {
if (data.inprog[i].secs_running > process.env.timechecker && data.inprog[i].op == "query" && ! data.inprog[i].ns.startsWith("local")){
console.log(textMessage(data.inprog[i],server))
sendSlack(data.inprog[i],server);
if (data.inprog[i].secs_running > process.env.timeKillOps && IgnoreListChecker(JSON.stringify(data.inprog[i].client)) == false){
console.log("Start Killing process")
console.log(data.inprog[i].opid)
db.command({killOp:1,op:Number(data.inprog[i].opid)}).catch( err => {
console.log(err)
})
}
}
}
})
database.close()
});
setTimeout(() => {
resolve('resolved');
}, 2000);
})
}
输出:
检测长期查询
服务器:mongoServer
查询:
{"find":"student","filter":{"student_id":"5388279"}}
运行秒数:279
FROM客户端:“ 192.168.0.5”
opid:1365911264
请检查
开始杀死进程 1365911264
{ MongoError: topology was destroyed at executeCommand (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/operations/db_ops.js:475:21) at /Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/utils.js:437:24 at new Promise (anonymous) at executeOperation (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/utils.js:432:10) at Db.command (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/db.js:263:10) at /Users/kant/work/mongo-longrun-query/app/index.js:49:18 at result (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/utils.js:414:17) at executeCallback (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/utils.js:406:9) at handleCallback (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/utils.js:128:55) at db.s.topology.command (/Users/kant/work/mongo-longrun-query/node_modules/mongodb/lib/operations/db_ops.js:496:5) name: 'MongoError', [Symbol(mongoErrorContextSymbol)]: {} }