【问题标题】:Sidekiq not finding records for Rails Active JobSidekiq 没有找到 Rails Active Job 的记录
【发布时间】:2019-01-24 06:35:31
【问题描述】:

在模型中创建用户后,作业会排队

user.rb

after_create_commit :profile_photo_job

def profile_photo_job
    message = "Add a profile photo"
    ReminderJob.set(wait: 1800).perform_later(self.id.to_s, message)
end

reminder_job.rb

class ReminderJob < ApplicationJob
  queue_as :default

  def perform(user_id, message)
    if user_id
      user = User.find(user_id)
    end
    ##sending message notification here
  end

end

但是,它经常在我的 sidekiq 控制台中引发以下错误

ActiveRecord::RecordNotFound: 找不到 'id'=7749 的用户 处理器:User-MacBook-Pro.local:*****

这个错误发生在生产环境中。

【问题讨论】:

    标签: ruby-on-rails activerecord redis sidekiq rails-activejob


    【解决方案1】:

    在 User.rb 中

    after_commit :profile_photo_job, on: :create
    
    def profile_photo_job
        message = "Add a profile photo"
        ReminderJob.set(wait: 1800).perform_later(self.id.to_s, message)
    end
    

    【讨论】:

    • 不是 after_create_commit 和 after_commit, on: :create 同一个东西?
    • 错误原因,是sidekiq在作业完成前在数据库中查找记录。
    • 它们是一样的,但是我在使用after_create_commit 时遇到了问题。顺便说一句,您使用的是哪个版本的导轨?
    • 另外,看看this。有时即使在某些应用程序中,我在使用after_commit 后进行了工作,但我仍然面临Record not found 问题,并且在某些应用程序中已经解决。
    • 另外,你为什么要转换成字符串? self.id.to_s
    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 2015-07-07
    • 2018-08-29
    • 2016-09-30
    • 2014-12-08
    • 2011-07-16
    • 2011-07-16
    相关资源
    最近更新 更多