此問題僅在test
環境中存在。在development
環境中一切正常。ActiveJob在`test`環境中找不到記錄
最近在從Rails 4.2.7.1升級到Rails 5.0.0.1後,我遇到了一個奇怪的問題。在升級之前,一切正常。
在我的一個模型中,我使用ActiveJob來執行任務。
# webhook_invocation.rb
def schedule_invocation
WebhookRequestJob.perform_later(id)
end
def init
remember_webhook # No DB changes
init_errors_context # No DB changes
flow_step_invocation.implementation = self
flow_step_invocation.save!
return unless calculate_expressions # No DB changes
calculated! # An AASM event, with no callbacks
schedule_invocation
end
和WebhookRequestJob#perform
,我檢索使用的ID的對象供給
# webhook_request_job.rb
def perform(webhook_invocation_id)
invocation = WebhookInvocation.find_by(id: webhook_invocation_id)
invocation.run_request
end
的問題是,在#perform
,它不能找到記錄(invocation
變得nil
)。我甚至嘗試將p WebhookInvocation.all
作爲第一行,但它打印的只是一個空集合。另一方面,如果我在#schedule_invocation
方法中嘗試使用p WebhookInvocation.all
,則會正確打印出WebhookInvocation
的所有對象。
沒有異常被提出,也沒有警告線。
編輯1:
我甚至試圖直接傳遞的對象#perform_later
WebhookRequestJob.perform_later(self)
即,但是#perform
接收到的對象是nil
。
編輯2:
我注意到,有一些消息像Creating scope :fail. Overwriting existing method FlowStepInvocation.fail
,因使用AASM。我通過使用create_scopes: false
來消除它們。但是這仍然沒有解決問題。
也許升級更新了你的一些寶石。什麼是運行這些後臺作業 - sidekiq? resque?在更新之前和之後版本如何變化?另外如果它在幕後使用redis,你也應該看看。 –
@Joel_Blum感謝您的回覆。這段代碼運行在我的本地機器上('Rails.env.development?'是'true')。沒有sidekiq,resque等啓用。另外,在本地Redis上也沒有被使用。 – 31piy