Sidekiq not finding records for Rails Active Job

2019-08-20 06:41发布

问题:

Jobs are queued on after a user is created like so in the model

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

However, it often throws the following error inside my sidekiq console

ActiveRecord::RecordNotFound: Couldn't find User with 'id'=7749 Processor: User-MacBook-Pro.local:*****

This error happens in production.

回答1:

In 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