Esempio n. 1
0
 public function shouldTaskExecute(Engine_Db_Table_Row $task, $refresh = true, $isAutomatic = false)
 {
     // Refresh to check if run in separate thread
     if ($refresh) {
         $task->refresh();
     }
     // Don't run it if it's disabled
     if (!$task->enabled || $task->type == 'disabled') {
         return false;
     }
     // Don't start manual tasks in automatic mode when they're dormant
     if (in_array($task->type, array('manual', 'semi-automatic')) && $task->state == 'dormant') {
         return false;
     }
     // Don't start executing tasks again unless they have been executing for > 15 minutes
     // We assume that 15 min means they have died and failed to cancel executing status
     if ($task->executing || $task->state == 'active') {
         if (time() > $task->started_last + $this->getTimeout()) {
             // Update the task, assuming it has timed out
             $newState = $task->type == 'semi-automatic' ? 'sleeping' : 'dormant';
             $task->getTable()->update(array('state' => $newState, 'executing' => false, 'executing_id' => 0), array('task_id = ?' => $task->task_id, 'state = ?' => $task->state, 'executing = ?' => $task->executing, 'executing_id = ?' => $task->executing_id));
         }
         return false;
     }
     // Tasks is not ready to be executed again yet
     if ($task->timeout > 0) {
         if (time() < $task->started_last + $task->timeout) {
             return false;
         }
         if (time() < $task->completed_last + $task->timeout) {
             return false;
         }
     }
     // Task is dormant
     if ($task->state == 'dormant') {
         // Automatic task is ready
         if ($task->type == 'automatic') {
             $task->getTable()->update(array('state' => 'ready'), array('task_id = ?' => $task->task_id, 'state = ?' => $task->state));
         }
         return false;
     }
     // Sanity check
     if ($task->state != 'ready' && $task->state != 'sleeping') {
         $this->getLog()->log('Weird task state: ' . $task->state, Zend_Log::WARN);
         return false;
     }
     // Task is ready
     return true;
 }