这是我的config / initializers / bunny.rb文件:
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
如您所见,我将SmsHandle声明为全局变量,以便在任何地方使用它。
工人代码:
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
但是,我这里的问题是我无法在任何将在delayed_job中调用的方法中使用它。
那么,如何启用delayed_job来使用此特定变量。
我应该在config / initializer / delayed_job.rb中声明这样的变量吗?即使这似乎也没有用。有人能指出这样做的正确方法吗?