/**
  * Tests CronTaskController::isTaskDue
  */
 public function testIsTaskDue()
 {
     $runner = CronTaskController::create();
     $task = new CronTaskTest_TestCron();
     $cron = Cron\CronExpression::factory($task->getSchedule());
     // Assuming first run, match the exact time (seconds are ignored)
     SS_Datetime::set_mock_now('2010-06-20 13:00:10');
     $this->assertTrue($runner->isTaskDue($task, $cron));
     // Assume first run, do not match time just before or just after schedule
     SS_Datetime::set_mock_now('2010-06-20 13:01:10');
     $this->assertFalse($runner->isTaskDue($task, $cron));
     SS_Datetime::set_mock_now('2010-06-20 12:59:50');
     $this->assertFalse($runner->isTaskDue($task, $cron));
     // Mock a run and test that subsequent runs are properly scheduled
     SS_Datetime::set_mock_now('2010-06-20 13:30:10');
     CronTaskStatus::update_status('CronTaskTest_TestCron', true);
     // Job prior to next hour mark should not run
     SS_Datetime::set_mock_now('2010-06-20 13:40:00');
     $this->assertFalse($runner->isTaskDue($task, $cron));
     // Jobs just after the next hour mark should run
     SS_Datetime::set_mock_now('2010-06-20 14:10:00');
     $this->assertTrue($runner->isTaskDue($task, $cron));
     // Jobs somehow delayed a whole day should be run
     SS_Datetime::set_mock_now('2010-06-21 13:40:00');
     $this->assertTrue($runner->isTaskDue($task, $cron));
 }
예제 #2
0
 /**
  * A helper to set the status on the CronTaskStatus object
  *
  * @param $status string
  * @param bool $wasRun Flag indicating that the task was run this request default: false
  * @return $this
  */
 public function setStatus($status, $wasRun = false)
 {
     CronTaskStatus::update_status(get_class($this), $wasRun, $status);
     return $this;
 }
 /**
  * Checks and runs a single CronTask
  *
  * @param CronTask $task
  */
 public function runTask(CronTask $task)
 {
     $class_name = get_class($task);
     $status = CronTaskStatus::get_status($class_name);
     $cron = CronExpression::factory($status->ScheduleString);
     $isDue = $this->isTaskDue($task, $cron);
     if ($isDue) {
         $this->output($class_name . ' will start now.');
         $task->doProcess();
         foreach ($task->getMessages() as $message) {
             $this->output($class_name . ': ' . $message);
         }
     } else {
         $this->output($class_name . ' will run at ' . $cron->getNextRunDate()->format('Y-m-d H:i:s') . '.');
     }
 }
 /**
  * Checks and runs a single CronTask
  *
  * @param CronTask $task
  * @param boolean $forceRun
  */
 protected function runTask(CronTask $task, $forceRun = false)
 {
     $cron = Cron\CronExpression::factory($task->getSchedule());
     $isDue = $this->isTaskDue($task, $cron);
     $willRun = $isDue || $forceRun;
     // Update status of this task prior to execution in case of interruption
     CronTaskStatus::update_status(get_class($task), $willRun);
     if ($isDue || $forceRun) {
         $msg = ' will start now';
         if (!$isDue && $forceRun) {
             $msg .= " (forced run)";
         }
         $this->output(get_class($task) . $msg);
         // Handle exceptions for tasks
         $error = null;
         try {
             $result = $task->process();
             $this->output(CronTaskResult::PrettifyResult($result));
         } catch (Exception $ex) {
             $result = false;
             $error = $ex->getMessage();
             $this->output(CronTaskResult::PrettifyResult($result));
         }
         // Store result if we return something
         if (self::config()->store_results && $result !== null) {
             $cronResult = new CronTaskResult();
             if ($result === false) {
                 $cronResult->Failed = true;
                 $cronResult->Result = $error;
             } else {
                 if (is_object($result)) {
                     $result = print_r($result, true);
                 } else {
                     if (is_array($result)) {
                         $result = json_encode($result);
                     }
                 }
                 $cronResult->Result = $result;
             }
             $cronResult->TaskClass = get_class($task);
             $cronResult->ForcedRun = $forceRun;
             $cronResult->write();
         }
     } else {
         $this->output(get_class($task) . ' will run at ' . $cron->getNextRunDate()->format('Y-m-d H:i:s') . '.');
     }
 }
 /**
  * Checks and runs a single CronTask
  *
  * @param CronTask $task
  */
 public function runTask(CronTask $task)
 {
     $canRunTask = true;
     $enforceSchedule = true;
     $isDue = true;
     if (method_exists($task, "canRunTask")) {
         $canRunTask = $task->canRunTask();
     }
     if (method_exists($task, "enforceSchedule")) {
         $enforceSchedule = $task->enforceSchedule();
     }
     if ($canRunTask) {
         if ($enforceSchedule) {
             $cron = Cron\CronExpression::factory($task->getSchedule());
             $isDue = $this->isTaskDue($task, $cron);
         }
         // Update status of this task prior to execution in case of interruption
         CronTaskStatus::update_status(get_class($task), $isDue);
         if ($isDue) {
             $this->output(get_class($task) . ' will start now.');
             $task->process();
         } else {
             $this->output(get_class($task) . ' will run at ' . $cron->getNextRunDate()->format('Y-m-d H:i:s') . '.');
         }
     } else {
         $this->output(get_class($task) . ' cannot run.');
     }
 }
 /**
  * Checks and runs a single CronTask
  *
  * @param CronTask $task
  */
 public function runTask(CronTask $task)
 {
     $cron = Cron\CronExpression::factory($task->getSchedule());
     $isDue = $this->isTaskDue($task, $cron);
     // Update status of this task prior to execution in case of interruption
     CronTaskStatus::update_status(get_class($task), $isDue);
     if ($isDue) {
         $this->output(get_class($task) . ' will start now.');
         $task->process();
     } else {
         $this->output(get_class($task) . ' will run at ' . $cron->getNextRunDate()->format('Y-m-d H:i:s') . '.');
     }
 }