3
\$\begingroup\$

We are using a message broker called rabbitmq. The code checks the status of number of messages in q queue and sent messages. This is achieved by a curl request to a given API.The script has a sleep of 5 seconds. I want to beutify and optimize this code for memory. I am getting offences too many lines. Since there is an infinite loop Want to make sure code does not leak memory.

 require 'net/http'
 require 'uri'
 require 'json'
 require 'slack-notifier'
 require 'sevak_publisher'

  CONFIG = YAML.load_file(File.join(__dir__, 'rabbitmq_config.yml'))
  def monitor_rabbitmq
      rabbitmqctl_url = CONFIG['rabbitmqctl']['url']
      rabbitmqctl_user = CONFIG['rabbitmqctl']['username']
      rabbitmqctl_password = CONFIG['rabbitmqctl']['password']
      uri = URI.parse("#{rabbitmqctl_url}/api/queues")
      request = Net::HTTP::Get.new(uri)
      request.basic_auth(rabbitmqctl_user, rabbitmqctl_password)
      req_options = { use_ssl: uri.scheme == 'https' }
      response = Net::HTTP.start(uri.hostname, uri.port, req_options)  do |http|
           http.request(request)
     end
    queue_details = JSON.parse(response.body)
    queue_details.each do |queue|
          output = { name: queue['name'],
           messages: {
             total: queue['messages'],
             ready: queue['messages_ready'],
             Unacknowlged: queue['messages_unacknowledged']
           },
           node: queue['node'],
           state: queue['state'],
           consumers: queue['consumers'] }
         puts output
         if output[:messages][:ready] > 1
              sent_alert_slack("RabbitMQ QUEUE High! \n #{output[:messages][:ready]} :\n #{output}")
              sent_sms(message)
         end
      end
   end
   def sent_alert_slack(message)
      notifier = Slack::Notifier.new CONFIG['slack_settings']    ['notification_api'],
                             channel: '#rabbitmq-monitoring',
                             username: 'notifier'
      notifier.ping message
    end

    def sent_sms(message)
        SevakPublisher.configure do |f|
          f.host = CONFIG['bunny']['host']
          f.port = CONFIG['bunny']['port']
          f.username = CONFIG['bunny']['username']
          f.password = CONFIG['bunny']['password']
          f.queues = ['sms_default']
        end
        publisher = SevakPublisher::Publisher.new
        CONFIG['sms_alert_receivers'].each do |msisdn|
           publisher.publish('sms_default', msisdn: msisdn, message:     message)
        end
    end
   loop do
      begin
      puts "\n", Time.now
      monitor_rabbitmq
   rescue => e
     puts "Error: #{e.message}"
     end
    sleep(300)
    end
\$\endgroup\$
  • \$\begingroup\$ "I am getting offences too many lines." From who? \$\endgroup\$ – Mast Feb 27 '17 at 19:42
  • \$\begingroup\$ Rubocop validation \$\endgroup\$ – tessie Feb 28 '17 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.