Ejemplo n.º 1
0
 public function runScheduledTasks()
 {
     ServiceState::UpdateLastIterationTime();
     if (!count($this->m_tasks)) {
         return;
     }
     $currentTime = time();
     $executionCount = 0;
     // Gather the last execution time for each task, and sort the task in a descending
     // order prioritizing the tasks which are longest overdue.
     foreach ($this->m_tasks as $index => $task) {
         $this->m_tasks[$index]['last_execution'] = ServiceState::GetLastExecutionTime($task);
     }
     usort($this->m_tasks, function ($left, $right) use($currentTime) {
         $leftOverdue = $currentTime - $left['last_execution'] - $left['interval'];
         $rightOverdue = $currentTime - $right['last_execution'] - $right['interval'];
         return $leftOverdue > $rightOverdue ? -1 : 1;
     });
     // Now execute the tasks which are overdue. We process a maximum of |MaximumTasksToExecute|
     // per iteration, running new tasks as long as |MaximumTimeToExecute| doesn't get hit.
     foreach ($this->m_tasks as $task) {
         if ($currentTime - $task['last_execution'] - $task['interval'] < 0) {
             return;
         }
         if ($executionCount++ > self::MaximumTasksToExecute || time() - $currentTime > self::MaximumTimeToExecute) {
             return;
         }
         $this->runTask($task);
     }
 }