我有我认为必须是并发问题.我使用的是乘客,导轨2.3.5,mongoid 1.9.2和mongo ruby驱动程序1.0.9.我正在使用jQuery来请求从MongoDB中提取的数据,然后在浏览器中呈现.在我开始同时提出两个这样的请求之前,一切都很顺利.在模型中,这些是请求执行的方法:
Class Visit include Mongoid::Document ... def self.cancellations_and_visits_by_therapist_graph(clinic_id) visits = collection.group("function(x){ return { resource_id : x.resource_id } }", {:clinic_id => clinic_id, :visit_date => { "$gte" => Time.now - 6.months, "$lte" => Time.now}}, {:visits => 0, :cancel_no_shows => 0}, 'function(obj, count) { if (obj.visit_status == "NO SHOW" || obj.visits_status == "CANCELLED") { count.cancel_no_shows += 1; } else { count.visits += 1; } }') visits = visits.group_by {|g| g['resource_id']} Resource.any_in(:mysql_id => visits.keys).order_by([:last_name, :asc]).order_by([:first_name, :asc]).inject({ 'visits' => [], 'cancel_no_shows' => [], 'xlabels' => []}) do |formatted_visits, resource| formatted_visits['visits'] << visits[resource.mysql_id.to_f].first['visits'] formatted_visits['cancel_no_shows'] << visits[resource.mysql_id.to_f].first['cancel_no_shows'] formatted_visits['xlabels'] << resource.last_name + ", " + resource.first_name formatted_visits end end def self.total_visits_for_graph(practice_id) visits = collection.group("function(x) { return { clinic_id : x.clinic_id } }", {:practice_id => practice_id, :visit_status => 'COMPLETE', :visit_date => { "$gte" => Time.now - 6.months, "$lte" => Time.now}}, {:visits => 0}, "function(obj, count) { count.visits += 1; }") visits = visits.group_by {|g| g['clinic_id']} Clinic.any_in(:mysql_id => visits.keys).order_by([:name, :asc]).inject({ 'data' => [], 'xlabels' => []}) do |formatted_visits, clinic| formatted_visits['data'] << visits[clinic.mysql_id.to_f].first['visits'] formatted_visits['xlabels'] << clinic.name formatted_visits end end end
描述问题的最好方法是将Mongo的结果传递给错误的对象.我看了一个例子:
当我调用CLinic.any_in时(这是其中一个组的结果)返回了这个:
{"group"=>{"$keyf"=>"function(x){ return { resource_id : x.resource_id } }", "cond"=>{:clinic_id=>101, :visit_date=>{"$gte"=>Tue Apr 20 15:34:37 +0800 2010, "$lte"=>Wed Oct 20 15:34:37 +0800 2010}}, "ns"=>"visits", "initial"=>{:visits=>0, :cancel_no_shows=>0}, "$reduce"=>"function(obj, count) {\n if (obj.visit_status == \"NO SHOW\" || obj.visits_status == \"CANCELLED\") {\n count.cancel_no_shows += 1;\n } else {\n count.visits += 1;\n }\n\n }"}}
这个(诊所对象)由collection.group调用返回:
{"_id"=>BSON::ObjectId('4cb7d72b3bc5457800ce2e6f'), "name"=>"Corona", "practice_id"=>39, "mysql_id"=>101}
与所有良好的经济问题一样,结果是randome,有时它工作正常,有时它会爆炸.我是mongo和mongoid的新手,所以我实际上不确定这是mongoid或mongo驱动程序的问题,但我认为它与Mongoid有关.我正在使用我用于在rails中加载Mongoid的初始化程序.任何想法甚至只是额外的调试想法都非常感激.
连接
mongoid_conf = YAML::load_file(Rails.root.join('config/mongoid.yml'))[Rails.env] Mongoid.configure do |config| config.master = Mongo::Connection.new(mongoid_conf['host'], 27017, :pool_size => 5, :timeout => 5).db(mongoid_conf['database']) end
Josh Moore.. 6
我找到了解决这个问题的方法.它实际上不是Mongo司机或Mongoid,而是乘客.当Passenger跨越rails进程时,它会分叉当前实例,因此文件描述符(包括TCP描述符在应用程序实例之间共享).这意味着mongo正在被同一个套接字写入和读取,这就是导致并发问题的原因.解决方案是在它分叉时使mongo重新连接.这是我找到的解决方案:
# Handle the creation of new processes by Phusion Passenger if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| if forked # We're in smart spawning mode. # Reset the connection to MongoDB Mongoid.config.master.connection.close load File.join(RAILS_ROOT, 'config/initializers/mongoid_init.rb') else # We're in conservative spawning mode. We don't need to do anything. end end end
参考http://groups.google.com/group/mongodb-user/browse_thread/thread/f31e2d23de38136a进行原始答案发布和讨论.
我找到了解决这个问题的方法.它实际上不是Mongo司机或Mongoid,而是乘客.当Passenger跨越rails进程时,它会分叉当前实例,因此文件描述符(包括TCP描述符在应用程序实例之间共享).这意味着mongo正在被同一个套接字写入和读取,这就是导致并发问题的原因.解决方案是在它分叉时使mongo重新连接.这是我找到的解决方案:
# Handle the creation of new processes by Phusion Passenger if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| if forked # We're in smart spawning mode. # Reset the connection to MongoDB Mongoid.config.master.connection.close load File.join(RAILS_ROOT, 'config/initializers/mongoid_init.rb') else # We're in conservative spawning mode. We don't need to do anything. end end end
参考http://groups.google.com/group/mongodb-user/browse_thread/thread/f31e2d23de38136a进行原始答案发布和讨论.