我正在使用SQS作为我的ActiveJobs的后端。Rails ActiveJob SQS和记录在执行作业时仍然不存在
我有被发送到队列中after_create
回调我的模型的一个非常简单的工作:
after_create :update_stuff_after_create
这初始化一个ActiveJob:
UpdateStuffAfterCreateJob.perform_later(self.id)
将作业发送到SQS正确排队。问题是,其他实例谁是负责从队列中读取时试图执行的工作,我看到一个ActiveRecord::RecordNotFound
:
class UpdateStuffAfterCreateJob < ActiveJob::Base
def perform(id)
publisher_offer_click = MyModel.find(id) # ActiveRecord::RecordNotFound
my_model.update_stuff
end
end
貌似在发作业,但该实例存在记录记录在使用作业的实例中不存在。
我一直在调试更深,我创建这个工作:
class TestingEntityExistsJob < ActiveJob::Base
def perform(id, exists_outside, time_outside_string)
Rails.logger.info "[TestingEntityExistsJob] #{id}, #{MyModel.last.id}, #{Time.now.strftime("%H:%M:%S.%L")}, #{time_outside_string}, exists: #{MyModel.where(:id => publisher_offer_click_id).exists?}, exists_outside: #{exists_outside}"
end
end
而且我在我的模型的after_create
这样称呼它:
TestingEntityExistsJob.perform_later(self.id, MyModel.where(:id => self.id).exists?, Time.now.strftime('%H:%M:%S.%L'))
我有这样奇怪的结果:
[2017-05-05 16:13:12.773] [TestingEntityExistsJob] 30645284, 30645284, 16:13:12.781, 16:13:12.720, exists: true, exists_outside: true
[2017-05-05 16:13:12.773] [TestingEntityExistsJob] 30645281, 30645284, 16:13:12.781, 16:13:12.659, exists: true, exists_outside: true
[2017-05-05 16:13:12.913] [TestingEntityExistsJob] 30645283, 30645284, 16:13:12.929, 16:13:12.838, exists: true, exists_outside: true
[2017-05-05 16:13:12.964] [TestingEntityExistsJob] 30645285, 30645285, 16:13:12.970, 16:13:12.927, exists: true, exists_outside: true
[2017-05-05 16:13:13.368] [TestingEntityExistsJob] 30645286, 30645285, 16:13:13.391, 16:13:13.309, exists: false, exists_outside: true
[2017-05-05 16:13:13.374] [TestingEntityExistsJob] 30645287, 30645285, 16:13:13.391, 16:13:13.339, exists: false, exists_outside: true
[2017-05-05 16:13:13.447] [TestingEntityExistsJob] 30645288, 30645288, 16:13:13.453, 16:13:13.411, exists: true, exists_outside: true
[2017-05-05 16:13:13.718] [TestingEntityExistsJob] 30645289, 30645289, 16:13:13.723, 16:13:13.658, exists: true, exists_outside: true
[2017-05-05 16:13:14.702] [TestingEntityExistsJob] 30645290, 30645290, 16:13:14.722, 16:13:14.642, exists: true, exists_outside: true
[2017-05-05 16:13:14.842] [TestingEntityExistsJob] 30645291, 30645291, 16:13:14.850, 16:13:14.789, exists: true, exists_outside: true
[2017-05-05 16:13:17.658] [TestingEntityExistsJob] 30645292, 30645292, 16:13:17.663, 16:13:17.599, exists: true, exists_outside: true
[2017-05-05 16:13:18.558] [TestingEntityExistsJob] 30645294, 30645294, 16:13:18.565, 16:13:18.513, exists: true, exists_outside: true
[2017-05-05 16:13:18.648] [TestingEntityExistsJob] 30645293, 30645294, 16:13:18.652, 16:13:18.592, exists: false, exists_outside: true
[2017-05-05 16:13:19.565] [TestingEntityExistsJob] 30645295, 30645295, 16:13:19.570, 16:13:19.514, exists: true, exists_outside: true
[2017-05-05 16:13:21.899] [TestingEntityExistsJob] 30645296, 30645295, 16:13:21.918, 16:13:21.771, exists: false, exists_outside: true
[2017-05-05 16:13:21.916] [TestingEntityExistsJob] 30645297, 30645295, 16:13:21.923, 16:13:21.832, exists: false, exists_outside: true
[2017-05-05 16:13:22.541] [TestingEntityExistsJob] 30645298, 30645298, 16:13:22.547, 16:13:22.484, exists: true, exists_outside: true
[2017-05-05 16:13:23.223] [TestingEntityExistsJob] 30645299, 30645299, 16:13:23.228, 16:13:23.171, exists: true, exists_outside: true
您可以从外部看到记录始终为exists
,但并不总是在作业执行内部。有时候MyModel.last.id
比我尝试加载的记录的id
小。
任何暗示为什么会发生这种情况的建议?我究竟做错了什么?
我实际的解决办法是'UpdateStuffAfterCreateJob.set(:等待=> 2.seconds).perform_later( self.id)'我认为丑陋,它不应该是必要的:/ – fguillen