问题
Mongoid don't have timeout option.
http://mongoid.org/en/mongoid/docs/installation.html
I want Mongoid to kill long time queries. How can I set Mongoid query timeout?
If I do nothing, Mongoid wait a long time like the below.
mongo > db.currentOp()
{
"opid" : 34973,
"active" : true,
"secs_running" : 1317, // <- too long!
"op" : "query",
"ns" : "db_name.collection_name",
"query" : {
"$msg" : "query not recording (too large)"
},
"client" : "123.456.789.123:46529",
"desc" : "conn42",
"threadId" : "0x7ff5fb95c700",
"connectionId" : 42,
"locks" : {
"^db_name" : "R"
},
"waitingForLock" : true,
"numYields" : 431282,
"lockStats" : {
"timeLockedMicros" : {
"r" : NumberLong(514304267),
"w" : NumberLong(0)
},
"timeAcquiringMicros" : {
"r" : NumberLong(1315865170),
"w" : NumberLong(0)
}
}
}
回答1:
Actually, all queries have a timeout by default. You can set a no_timeout
option to tell the query to never timeout.Take a look here
You can configure the timeout period using a initializer, like this
Mongoid.configure do |config|
config.master = Mongo::Connection.new(
"localhost", 27017, :op_timeout => 3, :connect_timeout => 3
).db("mongoid_database")
end
回答2:
The default query timeout is typically 60 seconds for Mongoids but due to Rubys threading there tend to be issues when it comes to shutting down processes properly.
Hopefully none of your requests will be putting that strain on the Mongrels but if you keep running into this issue I would consider some optimization changes to your application or possibly consider adopting God or Monit. If you aren't into that and want to change your request handing I might recommend Unicorn if you are already on nginx (github has made this transition and you can read about it more here)
回答3:
Try this solution:
ModelName.all.no_timeout.each do |m|
"do something with model"
end
https://stackoverflow.com/a/19987744/706022
来源:https://stackoverflow.com/questions/17962254/can-i-set-mongoid-query-timeout-mongoid-dont-kill-long-time-query