2016-11-18 35 views
4

我試圖在生產中發送郵件,但它拋出Activejob deserailization error.sidekiq在後臺運行。我已添加sidekiq寶石。我在comment_notification.rb中寫了一個用於發送電子郵件給用戶的方法。然後,在創建動作控制器我加入這個ActiveJob :: DeserializationError:嘗試反序列化參數時出錯

def create 
    CommentNotification.send_comment_mail(@current_user).deliver_later(wait: 1.minute) 
end 


def send_comment_email(current_user) 
    mail(to: current_user.email, 
    :subject => "commented on post", 
    :from => "<[email protected]>") 
end 

這是工作的罰款在本地服務器,但在生產中我得到這個錯誤

/home/apps/commentpost/shared/bundle/ruby/2.3.0/gems/sidekiq-4.2.3/lib/sidekiq/processor.rb:69:in `run' 
/home/apps/commentpost/shared/bundle/ruby/2.3.0/gems/sidekiq-4.2.3/lib/sidekiq/util.rb:17:in `watchdog' 
/home/apps/commentpost/shared/bundle/ruby/2.3.0/gems/sidekiq-4.2.3/lib/sidekiq/util.rb:25:in `block in safe_thread' 
2016-11-18T06:47:16.162Z 19093 TID-uw66g ActionMailer::DeliveryJob JID-e56b150964abf082e78089d9 INFO: start 
2016-11-18T06:47:16.167Z 19093 TID-uw66g ActionMailer::DeliveryJob JID-e56b150964abf082e78089d9 INFO: fail: 0.005 sec 
2016-11-18T06:47:16.167Z 19093 TID-uw66g WARN: {"context":"Job raised exception","job":{"class":"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper","wrapped":"ActionMailer::DeliveryJob","queue":"mailers","args":[{"job_class":"ActionMailer::DeliveryJob","job_id":"96e06bc6-1380-47b9-9393-9727868b3897","queue_name":"mailers","priority":null,"arguments":["CommentNotification","send_comment_email","deliver_later",{"_aj_globalid":"gid://commentpost/comment/40"},{"_aj_globalid":"gid://commentpost/User/20"}],"locale":"en"}],"retry":true,"jid":"e56b150964abf082e78089d9","created_at":1479450405.8364522,"enqueued_at":1479451636.1602836,"error_message":"Error while trying to deserialize arguments: Couldn't find Comment with 'id'=40","error_class":"ActiveJob::DeserializationError","failed_at":1479450405.8429642,"retry_count":6,"retried_at":1479451636.1668367},"jobstr":"{\"class\":\"ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper\",\"wrapped\":\"ActionMailer::DeliveryJob\",\"queue\":\"mailers\",\"args\":[{\"job_class\":\"ActionMailer::DeliveryJob\",\"job_id\":\"96e06bc6-1380-47b9-9393-9727868b3897\",\"queue_name\":\"mailers\",\"priority\":null,\"arguments\":[\"CommentNotification\",\"send_comment_email\",\"deliver_later\",{\"_aj_globalid\":\"gid://commentpost/comment/40\"},{\"_aj_globalid\":\"gid://commentpost/User/20\"}],\"locale\":\"en\"}],\"retry\":true,\"jid\":\"e56b150964abf082e78089d9\",\"created_at\":1479450405.8364522,\"enqueued_at\":1479451636.1602836,\"error_message\":\"Error while trying to deserialize arguments: Couldn't find Comment with 'id'=40\",\"error_class\":\"ActiveJob::DeserializationError\",\"failed_at\":1479450405.8429642,\"retry_count\":5,\"retried_at\":1479450981.998904}"} 
2016-11-18T06:47:16.167Z 19093 TID-uw66g WARN: ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find Comment with 'id'=40 
2016-11-18T06:47:16.167Z 19093 TID-uw66g WARN:/

任何人都可以請幫我對此疑問?爲此,我會很感激。

+0

現在,當您發佈的錯誤,閱讀它,尤其是這部分'錯誤嘗試反序列化參數:無法找到「身份證」 = 40'評論。它不是給你一個解決問題的提示嗎? –

+0

id在數據庫中有40個,註釋也在創建中。唯一的問題是郵件沒有被觸發。 – asha

回答

5

據我瞭解,控制器中的create方法也創建了評論併發送新創建的評論的電子郵件?

然後在這裏使用回調會更好。
我們在我們的項目中同樣的問題,我們通過這樣的事情解決它:

# in model 
after_commit :send_mail, on: :create 

private 

def send_mail 
    CommentNotification.send_comment_mail(campaign.user).deliver_later 
end 

然後你就可以確保記錄確實存在於數據庫中的郵件被交付之前。

這裏的問題是,你運行一個Comment#create並將郵件排入控制器。現在可能發生,Sidekiq在Rails提交新評論之前運行該任務。
然後你會得到這個錯誤。

問候,
溫泉

+0

我們可以把它寫在控制器中嗎? 例如:after_action:發送電子郵件,只:[:create] – asha

+1

您可以。但是你應該使用'CommentNotification.send_comment_mail(campaign.user).deliver_later(wait:5.minutes)'或類似的。對於after_commit和上面的方法都不起作用的 – Spa

+0

。仍然顯示相同的錯誤。 – asha

相關問題