Rails 4 Sidekiq根本无法找到ActiveRecord对象

时间:2017-02-24 05:41:28

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord sidekiq

我正在使用Rails 4.2.6和sidekiq 4.2.2。我总是得到一个错误

的错误
2017-02-23T07:33:23.455Z 31886 TID-ovz89uiyg WARN: {"class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"ActionMailer::DeliveryJob","queue":"mailers","args":[{"job_class":"ActionMailer::DeliveryJob","job_id":"d59ec97f-b858-4f47-aefd-c9150836d7d2","queue_name":"mailers","arguments":["UserMailer","send_password","deliver_now",{"_aj_globalid":"gid://my-app/User/85"},"http://0.0.0.0:3000/auth/password/edit?config=default&reset_password_token=zBdjahtAX7w3BC3Si8Fz&redirect_url="],"locale":"en"}],"retry":true,"jid":"f5dfa96df15443fc7ed2f3ce","created_at":1487832182.7783082,"enqueued_at":1487835165.842009,"error_message":"Error while trying to deserialize arguments: Couldn't find User with 'id'=85","error_class":"ActiveJob::DeserializationError","failed_at":1487832182.8214011,"retry_count":7,"retried_at":1487835203.4498932}

向用户发送重置密码电子邮件时出现上述错误。

我使用此代码在后台发送邮件

UserMailer.send_password(self.id, reset_password_link).deliver_later

我尝试了以下内容:

UserMailer.send_password(self.id, reset_password_link).deliver_later(wait: 1.minutes)

我也尝试过after_commit回调,因为这些文件说:

https://github.com/plataformatec/devise/issues/3550 https://github.com/mperham/sidekiq/issues/322

我正在传递id而不是对象本身。我仍然得到上述错误。我将sidekiq版本更新到4.2.9重新启动了sidekiq。

仍然是同样的错误。在sidekiq内无法找到ActiveRecord对象。我尝试使用byebug在sidekiq中找到非常旧的物体。但是sidekiq找不到ActiveRecord对象。

我正在尝试这一天,我创建了工作人员(比如说'SendResetPasswordWorker')而不是delay_later方法。仍然是同样的错误

{"class":"SendResetPasswordWorker","args":[90,"http://0.0.0.0:3000/auth/password/edit?config=default&reset_password_token=8xTR5enYdHDpuQg6cyPZ&redirect_url="],"retry":true,"queue":"default","jid":"7a8a831d837d3e4f3d75d5f7","created_at":1487842213.2409139,"enqueued_at":1487842213.240968,"error_message":"Couldn't find User with 'id'=90","error_class":"ActiveRecord::RecordNotFound","failed_at":1487842213.267404,"retry_count":0}2017-02-23T09:30:13.268Z 35119 TID-ow2w9e41c WARN: ActiveRecord::RecordNotFound: Couldn't find User with 'id'=90 2017-02-23T09:30:13.268Z 35119 TID-ow2w9e41c WARN: /Users/abhilash/.rbenv/versions/2.2.1/gemsets/gauge-slcsl/gems/activerecord-4.2.6/lib/active_record/core.rb:155:in `find' /Users/abhilash/my-app/components/auth/app/workers/

我还测试了另一个工作人员'UserAgentWorker',另一个方案但仍然是同样的错误。

有人对此错误有任何想法吗?如果是,请分享。

1 个答案:

答案 0 :(得分:0)

最后我找到了答案。我使用 Apartment Gem 进行多租户。在sidekiq内部,默认架构返回' public' 而不是 postgres 中的当前架构。因此,每次sidekiq运行后台作业时,我都必须切换回当前的租户模式。

当我们在后台运行代码时,是否真的需要切换回架构?也许我们可以在sidekiq中进行设置,每当它启动作业时,将模式切换到当前租户。我将检查这是否是最好的解决方案。谢谢。