Created
January 27, 2025 08:48
-
-
Save fazlurr/03382cacf5ac68b55c197db533de2b83 to your computer and use it in GitHub Desktop.
MongoDB - Kill Slow Operations
This file contains 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
var killLongRunningOps = function (table = 'submission', maxSecsRunning = 5000, kill = false) { | |
var ns = 'orderonline_db.' + table; | |
if (table === '*') { | |
ns = /^orderonline_db\./; | |
} | |
var currOp = db.currentOp({"active" : true,"secs_running" : { "$gt" : maxSecsRunning },"ns" : ns}); | |
var operations = currOp.inprog; | |
for (var i = 0; i < operations.length; i++) { | |
var operation = operations[i]; | |
print(i + ". Operation: " + operation.opid); | |
print("- NS: " + operation.ns); | |
print("- OP: " + operation.op); | |
print("- Secs Running: " + operation.secs_running); | |
if (kill) { | |
print("- Killing operation: " + operation.opid); | |
try { | |
db.killOp(operation.opid); | |
} catch (error) { | |
console.log('- Error Killing: ', error); | |
} | |
} | |
print("--------------------"); | |
} | |
} | |
killLongRunningOps('*', 1000, false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Login to your MongoDB cluster, then copy paste the code