2

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>
Ankita.P
  • 442
  • 1
  • 8
  • 17
  • ok i deleted my answer, can you post the whole trace of your error? – uday Sep 18 '15 at 15:55
  • I have edited my question with the delayed job trace – Ankita.P Sep 18 '15 at 16:02
  • There seems to be some bits of code missing here. What are the arguments you are passing to `SmsJob.create!`? Also, what version of the Twilio gem are you using? – philnash Sep 18 '15 at 16:22
  • Sorry for the late reply. I am using version (4.3.0) and parameters passed are self => object of twilio base class, q_sms => object of queued_sms object class(all smses are queued before sending), delivey_opts => other optional parameters required – Ankita.P Sep 19 '15 at 06:07
  • When I move below three line inside my call api function, sms is getting sent from delayed job.`account_sid = twilio_settings[:account_sid] auth_token = twilio_settings[:auth_token] @twilio_object = Twilio::REST::Client.new(account_sid, auth_token)` Can anyone point out to e what is the thing. I cannot find the exact reason why is it so – Ankita.P Sep 19 '15 at 10:32
  • Hi Ankita, Did you ever sort this out? – Megan Speir Aug 26 '16 at 23:24
  • Yes. Actually I forgot to restart my delayed_job because of which it was not working. Just restart your delayed_job script and it should work. – Ankita.P Sep 10 '16 at 16:32

0 Answers0