睡眠后RoR线程消失?

时间:2018-11-15 06:06:46

标签: ruby-on-rails-4

我在rails 4中遇到线程问题。 我想在不使用ActiveRecord的新线程中打开方法。 方法只是睡眠了几分钟,然后才触发发布请求。

一切正常,但有时其中一些较新的线程在睡眠后触发。大多数情况下,例如20个垃圾邮件,有时甚至很少。在第三种情况下,即使我将其作为垃圾邮件,一切都还可以。我尝试了在睡眠之前和之后以及在睡眠消息始终输出之前在Threads中进行输出,这意味着线程在那儿,但是在睡眠之后,在某些情况下它无法发送POST请求就不会出现。

sth = "array of ActiveRecord objects, like 10 max"

sth.each do |s|
  ..some active record create new object..unrelated..

  Thread.new { custom_method(s.id) }
end

def custom_method(id)
 Rails.logger.info "BEFORE SLEEP FOR #{id}"
 sleep(180)     
 Thread.current.run # this is something that I just tried if it will help

 ..call post method to send "id" to some route, nothing special...

 Thread.current.exit # also added to see if it will help
end

因此,基本上,每次看到睡眠日志消息之前,但每次都不会看到,并且在这种情况下不会触发POST请求。任何想法为什么会如此随机发生?

谢谢

0 个答案:

没有答案