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?