1

This is my config/initializers/bunny.rb file:

  if Setting.RABBITMQ_ENABLED[Rails.env]
     conn = Bunny.new Setting.MERCURY_URL[Rails.env]
     conn.start

     bunny_settings = Setting.BUNNY_SETTINGS

     ## Channel && Topic for sending SMS-es.
     sms_ch = conn.create_channel
     ::SmsHandle = sms_ch.topic(bunny_settings["sms_topic_name"], :durable => true, :auto_delete => true)

     ## Channel && Topic for sending seller requests
     seller_ch = conn.create_channel
     exchange = seller_ch.topic(bunny_settings["seller_topic_name"], :durable => true, :auto_delete => true)

     seller_ch.queue(bunny_settings["seller_queue_name"], :durable => true, :auto_delete => true, :arguments => {}, :exclusive => false).bind(exchange, :routing_key => bunny_settings["seller_routing_key"]).subscribe do |delivery_info, properties, payload|
     payload_json = JSON.parse payload
     BunnyConsumer.consume_seller_request(delivery_info, properties, payload_json)
   end
 end

As you can see, I declared SmsHandle as a global variable to use it anywhere.

Worker code:

  def send_sms(message_handle, caller_params)
    if Setting.RABBITMQ_ENABLED[Rails.env]
      pay_load = {:usertype => "custom", :triggers => [message_handle], :type => "notification", :caller_params => caller_params}.to_json
      SmsHandle.publish(pay_load, :routing_key => "route_key" + rand(0..9).to_s, :content_type => "application/json", :type => "transport")
      self.notification_events.create!(handle_name: message_handle, notification_type: SellerLead::NotificationType::SMS, payload: caller_params.to_json)
    end
  end

But, the problem for me here is that I unable to use it in any method that'll be called in a delayed_job.

So, how can I enable the delayed_job to use this particular variable.

Should I declare variable like this in the config/initializer/delayed_job.rb? Even this doesn't seem to working. Can someone point to the right way of doing this?

  • DelayedJob will get its own instance of this. Did you want to share the very same object across multiple processes? – Sergio Tulentsev Dec 16 '15 at 12:32
  • I don't need share the same object across multiple processes. But, it'll be great as it will help me avoid duplicate code. So, is there a way to share the very same object like you mentioned? Thanks. – Manoj Venkat Dec 16 '15 at 12:53
  • No, there's no way. :) Also I don't see how it would save you from duplicate code. You don't need to duplicate any code here. Just make sure that `Setting.RABBITMQ_ENABLED[Rails.env]` is truthy in DJ environment. – Sergio Tulentsev Dec 16 '15 at 12:55
  • So, if I declare all the variables in the bunny.rb in delayed_job.rb as well, right? – Manoj Venkat Dec 16 '15 at 12:59
  • No, bunny.rb initializer will run in DJ too. – Sergio Tulentsev Dec 16 '15 at 13:01
  • Can you attach the lines in your worker in which you call this global variable? – Oss Dec 16 '15 at 13:03
  • I attached my worker code as well. – Manoj Venkat Dec 16 '15 at 13:12
  • @SergioTulentsev If I am using rake jobs:work it's in the foreground, it's recognizing the SmsHandle global variable but if run in the background using "bin/delayed_job start", then it's unable to recognize this variable. – Manoj Venkat Dec 16 '15 at 13:16

0 Answers0