/** * 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) { $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') . '.'); } }