module Resque::DynamicQueues

def queues_with_dynamic


list for a key with Resque.set_dynamic_queue(key, ["q1", "q2"]
and negations can be used inside this dynamic queue list. Set the queue
If no key is supplied, it defaults to the worker's hostname, and wildcards
An @key can be used to dynamically look up the queue list for key from redis.

e.g. "*high*", and negation can be indicated with a prefix of "!"
The splat can also be used as a wildcard within a queue name,

can be useful for dynamically adding new queues.
A splat ("*") means you want every queue (in alpha order) - this

Returns a list of queues to use when searching for a job.
def queues_with_dynamic
  queue_names = @queues.dup
  
  return queues_without_dynamic if queue_names.grep(/(^!)|(^@)|(\*)/).size == 0
  real_queues = Resque.queues
  matched_queues = []
  #Remove Queues under Api Limits
  Redis.current.zremrangebyscore("APILimits", "0", "(#{Time.now.to_i}")
  api_limit_instances = Redis.current.zrange("APILimits", 0, -1).map {|key| key.to_i if key.match(/^\d*$/)}.compact
  real_queues = real_queues.select {|key| key if !api_limit_instances.include?((key.match(/^(\d*)_.*/) || [])[1].to_i)} ## 2
    
  #Queue Pausing 
  Resque.redis.zremrangebyscore("PauseQueue", "0", "(#{Time.now.to_i}")
  paused_instances = Resque.redis.zrange("PauseQueue", 0, -1).map {|key| key.split("__")[0].to_i if key.match(/^\d*__.*/)}.compact
  real_queues = real_queues.select {|key| key if !paused_instances.include?((key.match(/^(\d*)_.*/) || [])[1].to_i)}
  while q = queue_names.shift
    q = q.to_s
    if q =~ /^(!)?@(.*)/
      key = $2.strip
      key = hostname if key.size == 0
      add_queues = Resque.get_dynamic_queue(key)
      add_queues.map! { |q| q.gsub!(/^!/, '') || q.gsub!(/^/, '!') } if $1
      queue_names.concat(add_queues)
      next
    end
    if q =~ /^!/
      negated = true
      q = q[1..-1]
    end
    patstr = q.gsub(/\*/, '.*')
    pattern = /^#{patstr}$/
    if negated
      matched_queues -= matched_queues.grep(pattern)
    else
      matches = real_queues.grep(/^#{pattern}$/)
      matches = [q] if matches.size == 0 && q == patstr
      matched_queues.concat(matches.sort)
    end
  end
 
  return matched_queues.uniq
end