module Resque::DynamicQueues

def get_queued_job(grouped_queues)

def get_queued_job(grouped_queues)
  if defined?(Resque::Plugins::ConcurrentRestriction)
    # Bounded retry
    1.upto(Resque::Plugins::ConcurrentRestriction.reserve_queued_job_attempts) do |i|
      resque_job = get_next_job(grouped_queues)
      # Short-curcuit if a job was not found
      return if resque_job.nil?
      # If there is a job on regular queues, then only run it if its not restricted
      job_class = resque_job.payload_class
      job_args = resque_job.args
      # Return to work on job if not a restricted job
      return resque_job unless job_class.is_a?(Resque::Plugins::ConcurrentRestriction)
      # Keep trying if job is restricted. If job is runnable, we keep the lock until
      # done_working
      return resque_job unless job_class.stash_if_restricted(resque_job)
    end
    
    # Safety net, here in case we hit the upper bound and there are still queued items
    return nil        
  else
    return get_next_job(grouped_queues)
  end
end