1
我正在使用delayed_job_active_record
寶石。我想延遲對外部API的請求。即使我的作業被添加到數據庫中,並且rake jobs:work
將其運行並將其從數據庫中刪除,但從我的messages_controller.rb
實際延遲的代碼永遠不會執行。delayed_job_active_record不會執行延遲代碼
如果我如果從控制器方法中刪除.delay
,則代碼按預期執行,我的測試全部通過。
# messages_controller.rb
require 'zendesk'
class MessagesController < ApplicationController
layout "application"
# /suport/contact-us
def contact_us
zendesk = MyApp::Zendesk.new
zendesk.delay.create_support_ticket(params[:message])
# render page
respond_to do |format|
flash[:notice] = "Email sent successfully!" if @sent
format.html { render "pages/support/contact-us" }
end
end
end
# zendesk.rb
require 'zendesk_api'
module MyApp
class Zendesk
attr_accessor :client
def initialize(*args)
@client = create_client
end
# contact-us ticket methods
def create_support_ticket(params={})
unless params.blank? || @client.blank?
# get or create user_id for submitter
params[:requester_id] = check_user_exists(params)
begin
ticket = @client.tickets.create(
subject: "Support Ticket",
comment: { value: params[:message] },
submitter_id: params[:requester_id],
requester_id: params[:requester_id],
assignee_id: 201578811,
status: "new",
fields: [
{id: 20887016, value: "Support"},
{id: 20966436, value: "New"}])
return ticket
rescue => e
Airbrake.notify e
end
else
return false
end
end
end
end
* UPDATE * 我試圖啓動我的Rails服務器沒有工人運行,並通過Delayed::Job.last.invoke_job
手動調用作業時,我看到這個錯誤:
Delayed::DeserializationError: Job failed to load: undefined class/module MyApp::. Handler: "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/object:MyApp::Zendesk\n client: !ruby/object:ZendeskAPI::Client\n config: !ruby/object:ZendeskAPI::Configuration\n client_options: {}\n cache: !ruby/object:ZendeskAPI::LRUCache\n size: 1000\n store: {}\n lru: []\n url: https://REDACTED/api/v2\n username: REDACTED\n password: REDACTED retry: true\n logger: !ruby/object:Logger\n progname: \n level: 0\n default_formatter: !ruby/object:Logger::Formatter\n datetime_format: \n formatter: \n logdev: !ruby/object:Logger::LogDevice\n shift_size: \n shift_age: \n filename: \n dev: !ruby/object:IO {}\n mutex: !ruby/object:Logger::LogDevice::LogDeviceMutex\n mon_owner: \n mon_count: 0\n mon_mutex: !ruby/object:Mutex {}\n callbacks:\n - !ruby/object:Proc {}\n resource_cache: {}\nmethod_name: :create_support_ticket\nargs:\n- !ruby/hash:ActionController::Parameters\n name: ben\n email: [email protected]\n reason: General\n message: test\n"
from /vendor/bundler/gems/delayed_job-4.0.0/lib/delayed/backend/base.rb:97:in `rescue in payload_object'
我嘗試添加require 'zendesk'
到一個config/initializers/custom.rb
,但錯誤仍然存在。
我發佈了一個更新的問題,但我認爲你是正確的,這也會是一個問題。 – professormeowingtons
'模塊MyApp'駐留在單獨的.rb文件中嗎?如果沒有,你應該在/lib/my_app.rb中創建它,並且需要lib在application.rb – swapab
是的,'module MyApp'在'/ lib/my_app.rb'中,'lib'已經用'config自動加載.autoload_paths + =%W(#{config.root}/lib)'in'application.rb' – professormeowingtons