namespace :command do
  desc "Subscriber for commands & upgrade"
  task subscribe: :environment do
    redis = Redis.connect(:host => APPLICATION[Rails.env]["redis"]["host"], :port => APPLICATION[Rails.env]["redis"]["port"])	
    redis.subscribe("command-upgrade") do |on|
      on.message do |channel, message|
        if($redis.llen("command") > 1)
        	g = $redis.llen "command"
        	(0..g).each do |x|
        		cmd_data = $redis.lpop('command')
        		Monitoring.monitoring_data_update(cmd_data) unless cmd_data.blank?
        	end
        else
        	cmd_data = $redis.rpop('command')
        	Monitoring.monitoring_data_update(cmd_data) unless cmd_data.blank?
        end
      end
    end
  end
end
