vendredi 18 septembre 2015

undefined method messages for Twilio Rest client while running in delayed job

I am using twilio gem to send message. When I try to run function directly without any delay, message is getting sent to particular number but when i try to run with delayed job I am getting error as undefined method `messages' for #

I did require 'twilio-ruby' in my code but it does not show any effect.

If i try to manually invoke the job using Delayed::Job.where(:id => 21501).first.invoke_job it runs without any errors. Because of this I am more confused thinking where is the exact error.

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 file

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

calling delayed job

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

-- EDIT --

** This is the only error I can see in my delayed job trace **

[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>

Aucun commentaire:

Enregistrer un commentaire