/** * get the status object for the task * * @return CronTaskStatus */ public function getStatusObject() { if (!$this->statusObj) { $this->setStatusObject(CronTaskStatus::get_status(get_class($this))); } return $this->statusObj; }
/** * Determine if a task should be run * * @param CronTask $task * @param \Cron\CronExpression $cron */ protected function isTaskDue(CronTask $task, \Cron\CronExpression $cron) { // Get last run status $status = CronTaskStatus::get_status(get_class($task)); // If the cron is due immediately, then run it $now = new DateTime(SS_Datetime::now()->getValue()); if ($cron->isDue($now)) { if (empty($status) || empty($status->LastRun)) { return true; } // In case this process is invoked twice in one minute, supress subsequent executions $lastRun = new DateTime($status->LastRun); return $lastRun->format('Y-m-d H:i') != $now->format('Y-m-d H:i'); } // If this is the first time this task is ever checked, no way to detect postponed execution if (empty($status) || empty($status->LastChecked)) { return false; } // Determine if we have passed the last expected run time $nextExpectedDate = $cron->getNextRunDate($status->LastChecked); return $nextExpectedDate <= $now; }
/** * 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') . '.'); } }