class Litejob::Server

def run!

def run!
  @scheduler.spawn do
    Litejob.logger.info("[litejob]:[RUN] id=#{@scheduler.context.object_id}")
    worker_sleep_index = 0
    while @running
      processed = 0
      @prioritized_queues.each do |priority, queues|
        queues.each do |queue, spawns|
          batched = 0
          while (batched < priority) && (payload = pop(queue))
            batched += 1
            processed += 1
            id, serialized_job = payload
            processor = Processor.new(queue, id, serialized_job)
            processor.process!
            # give other contexts a chance to run here
            @scheduler.switch
          end
        end
        if processed == 0
          sleep @sleep_intervals[worker_sleep_index]
          worker_sleep_index += 1 if worker_sleep_index < @sleep_intervals.length - 1
        else
          worker_sleep_index = 0 # reset the index
        end
      end
    end
  end
end