2

我正在使用 twilio gem 发送消息。当我尝试直接运行函数而没有任何延迟时,消息被发送到特定号码,但是当我尝试使用延迟作业运行时,我收到错误,因为未定义的方法 `messages' for #

我的代码中确实需要“twilio-ruby”,但它没有显示任何效果。

如果我尝试使用Delayed::Job.where(:id => 21501).first.invoke_job它手动调用作业,它会运行而不会出现任何错误。因此,我更加困惑地思考确切的错误在哪里。

TwilioBase.rb

require 'twilio-ruby'
module Common
 module Sms
  class TwilioBase < Base   

   def initialize(phone, client:, associate_with: nil)
     account_sid = twilio_settings[:account_sid]
     auth_token = twilio_settings[:auth_token]

     @twilio_object = Twilio::REST::Client.new(account_sid, auth_token)
   end      

    def call_api(query_hash)
     res = @twilio_object.account.messages.create(query_hash['query_hash'])
    end
  end 
 end
end

SmsJob.rb 文件

class SmsJob
 def self.create!(sms_gateway_object, queued_sms, opts={},      delayed_job_opts={})
   q_sms = queued_sms
   sms_job = self.new(q_sms.id, sms_gateway_object, opts)
   job = Delayed::Job.enqueue(sms_job, delayed_job_opts)
   q_sms.update_attributes!({:delayed_job_id => job.id})
   return sms_job
 end


  def perform
    q = Common::QueuedSms.find(@queued_sms_id                    
    @sms_gateway_object.deliver_sms(@opts)
  end


 private
 def initialize(queued_sms_id, sms_gateway_object, opts)
   @queued_sms_id = queued_sms_id
   @opts = opts
   @sms_gateway_object = sms_gateway_object
 end
end

调用延迟的工作

SmsJob.create!(self, @q_sms, deliver_sms_opts)

- 编辑 -

** 这是我在延迟工作跟踪中看到的唯一错误**

[Worker(host:ankita-Inspiron-5520 pid:9080)] Job SmsJob (id=21501)   FAILED (4 prior attempts) with ValidationError: undefined method `messages' for #<Twilio::REST::Client:0x0000000000000>
4

0 回答 0