Skip to content

Instantly share code, notes, and snippets.

@jarthod
Created November 8, 2019 08:31
Show Gist options
  • Save jarthod/ffe9d88bbab13bebafba12265be7c1aa to your computer and use it in GitHub Desktop.
Save jarthod/ffe9d88bbab13bebafba12265be7c1aa to your computer and use it in GitHub Desktop.
Ruby script which tries to detect when mongo is up but super slow (for example due to IO issue) and stop it to allow fallback to secondary
#!/usr/bin/env ruby
# Tries to detect when mongo is up but super slow (ex: IO issue)
THRESHOLD = 20_000 # ms
def test_mongo tries: 5
out = `echo -e "db.isMaster()\ndb.getReplicationInfo()" | mongo mongodb://localhost/?socketTimeoutMS=#{THRESHOLD} 2>&1`
res = $?
if res != 0 && !out['Connection refused']
puts out
if tries > 0
sleep 20
test_mongo tries: tries-1
else
puts "Mongo looks is dead, trying to stop it.."
system("sudo service mongod stop")
system("sudo service mongod status")
end
end
end
test_mongo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment