执行 Job 时 Rails ActiveJob SQS 和记录仍然不存在

Posted

技术标签:

【中文标题】执行 Job 时 Rails ActiveJob SQS 和记录仍然不存在【英文标题】:Rails ActiveJob SQS and record still not exists when executing the Job 【发布时间】:2017-10-04 05:13:36 【问题描述】:

我使用 SQS 作为我的 ActiveJobs 的后端。

我有一个非常简单的工作,它在我的模型的 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,但并不总是在 Job 执行内部看到。有时MyModel.last.id 小于我尝试加载的记录的id

关于为什么会发生这种情况的任何建议?我做错了什么?

【问题讨论】:

我的实际解决方法是 UpdateStuffAfterCreateJob.set(:wait =&gt; 2.seconds).perform_later(self.id) 我认为丑陋的,它不应该是必要的:/ 【参考方案1】:

这种行为的原因是after_createsave 的事务被提交时触发。这会导致在短时间内作业的数据库连接无法访问记录,因为模型的数据库连接尚未提交。

你会想要使用

after_commit :some_method, on: [:create]

而不是

after_create :some_method

https://apidock.com/rails/ActiveRecord/Transactions/ClassMethods/after_commit

【讨论】:

以上是关于执行 Job 时 Rails ActiveJob SQS 和记录仍然不存在的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ActiveJob 回调方法中获取 Job 属性?

Sidekiq Rails 4.2 使用 Active Job 还是 Worker?有啥不同

使用 Delayed::Job 时使用 ActiveJob 设置优先级

SerializationError Rails ActiveJob 时间和日期

如何使用 ActiveJob 设置 Sidekiq 的重试次数?

在 Rails 中安排一个 ActiveJob