我有一个 ruby 脚本,它连接到远程服务器上的 mongo db 并执行 find_by 查询。有时执行需要不到 1 秒的时间,有时需要大约 15 分钟才能执行。这种行为的原因可能是什么?
脚本.rb
require '/home/ubuntu/praneetha/check_mongo/publisher_models.rb'
$stdout.sync = true
begin
count = 1
user_id = '53ce011f9937d923a4000001'
while(true)
puts "count: #{count}"
t1 = Time.now.utc
puts "Time.now is ========= #{t1}"
p = Puser.find_by(:puid => user_id)
puts "puser is ====#{p.class}"
t2 = Time.now.utc
t3 = t2-t1
puts "time taken =====> #{t3}"
if t3>2
puts "I have an error at time #{t1} and total time is #{t3} "
end
count = count + 1
sleep(300)
end
end
发布者模型.rb
require 'mongoid'
require 'em-synchrony'
class Puser
include Mongoid::Document
field :email, type: String
#custom user id for the user. This should be indexed at the time of creation
field :puid, type: String
end