/**
  * Generate the list of next 10 in queue.
  * @param int limit of how many to return for next in queue
  * @param boolean minimal fields returned
  * @param processing, if true only return true set of what needs to be executed next NOW
  * @return array of tasks in order of execution next.
  */
 public function next($limit = 10, $minimal = true, $processing = true)
 {
     //If we don't have any queued in table just exit with empty set.
     if (!$this->hasAny(array("{$this->alias}.status" => 1))) {
         return array();
     }
     $cpu = QueueUtil::currentCpu();
     $now = $this->str2datetime();
     $fields = $minimal ? array("{$this->alias}.id") : array("{$this->alias}.*");
     //Set of conditions in order
     $conditions = array(array("{$this->alias}.is_restricted" => true, "{$this->alias}.status" => 1, "{$this->alias}.scheduled <=" => $now, 'OR' => array(array("{$this->alias}.scheduled_end >=" => $now), array("{$this->alias}.scheduled_end" => null)), "{$this->alias}.cpu_limit >=" => $cpu), array("{$this->alias}.is_restricted" => true, "{$this->alias}.status" => 1, "{$this->alias}.scheduled <=" => $now, 'OR' => array(array("{$this->alias}.scheduled_end >=" => $now), array("{$this->alias}.scheduled_end" => null))), array("{$this->alias}.is_restricted" => true, "{$this->alias}.status" => 1, "{$this->alias}.cpu_limit >=" => $cpu), array("{$this->alias}.is_restricted" => false, "{$this->alias}.status" => 1));
     if (!$processing) {
         $conditions[] = array("{$this->alias}.is_restricted" => true, "{$this->alias}.status" => 1, "{$this->alias}.scheduled >=" => $now);
     }
     $retval = array();
     foreach ($conditions as $condition) {
         $current_count = count($retval);
         if ($current_count >= $limit) {
             break;
         }
         $new_limit = $limit - $current_count;
         $result = $this->find('all', array('limit' => $new_limit, 'order' => array("{$this->alias}.scheduled ASC, {$this->alias}.priority ASC"), 'fields' => $fields, 'conditions' => $condition));
         if (!empty($result)) {
             $retval = array_merge($retval, $result);
         }
     }
     return $retval;
 }