delayed_job_active_record不会执行延迟代码

时间:2013-12-22 07:51:14

标签: ruby-on-rails ruby delayed-job

我正在使用delayed_job_active_record gem。我想延迟对外部API的请求。即使我的作业已添加到数据库,rake jobs:work运行它并将其从数据库中删除,我的messages_controller.rb的实际延迟代码也永远不会被执行。

如果我从控制器方法中删除.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

*更新* 我尝试在没有工作人员运行的情况下启动我的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: myemail@aol.com\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,但错误仍然存​​在。

1 个答案:

答案 0 :(得分:1)

我想你的create_client方法正在实例化Api对象。很可能Api实例无效,因为延迟作业运行的过程是异步的。

那就是说,你应该在create_support_ticket中创建一个新的Zendesk Api实例。 如

def create_support_ticket(params={})
  @client = create_client
  unless params.blank? || @client.blank?
  ...
end