写了段多线程的同步数据的 rake 任务。但是启动的时候,不定时的会报错,之前写单线程的时候是没啥问题的,改成多线程就出问题了。
代码如下:
task :move_user => [:environment] do
TempSettings::MOVE_KIND = 'batch'
tread_num = 10
last_id = User.with_phone.last.id
interval = (last_id - 10000)/tread_num
(1..tread_num).to_a.map do |i|
Thread.new do
puts "开始thread #{i} 同步用户------------------"
relation = User.with_phone.where(['id > ? and id <= ?', 10000 + (i-1)*interval, 10000+i*interval])
relation.find_in_batches do |group|
group.each do |user|
user.async_self
end
end
end
end.map(&:join)
(1..tread_num).to_a.map do |i|
Thread.new do
puts "开始thread #{i} 同步用户其他信息------------------"
relation = User.with_phone.where(['id > ? and id <= ?', 10000 + (i-1)*interval, 10000+i*interval])
relation.find_in_batches do |group|
group.each do |user|
next if user.dm_user.is_success?
user.async_other
end
end
end
end.map(&:join)
end
报错如下:
Circular dependency detected while autoloading constant UserKid
RAILS_ENV=production 这样的参数也设置过,甚至还加过 config.allow_concurrency = false 这样的设置,但是还是时好时坏,请问怎么破?