これは私の 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 でこのような変数を宣言する必要がありますか? これでもうまくいかないようです。誰かがこれを行う正しい方法を指摘できますか?