1

这是我的 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 使用这个特定的变量。

我应该在 config/initializer/delayed_job.rb 中声明这样的变量吗?即使这似乎也不起作用。有人可以指出这样做的正确方法吗?

4

0 回答 0