/** * Check if there are any upcoming schedules */ public static function check() { $numAdded = 0; foreach (self::getList() as $sheduleItem) { $storedLastRunTime = strtotime($sheduleItem->lastRun == '' ? $sheduleItem->start : $sheduleItem->lastRun); $previousCalculatedRunTime = CronParser::lastRun($sheduleItem->cron); // This looks at when the item had run. If the stored value is less than // the calculated value means that we have past a run period. So need to run if ($storedLastRunTime < $previousCalculatedRunTime) { // Update the run time to now $sheduleItem->lastRun = strftime('%Y-%m-%d %H:%M', $previousCalculatedRunTime); $sheduleItem->save(); // Enqueue a new item to run $job = new Cron(['ref' => $sheduleItem->id, 'cmd' => $sheduleItem->cmd]); $job->save(); $numAdded++; } } return $numAdded; }
function _runner() { _runnerLog('Running Cron'); $pageTimer = new \Jackbooted\Time\Stopwatch('Run time for ' . basename(__FILE__)); _runnerLog('Checking if scheduled jobs need to be added to CronQueue'); Scheduler::check(); $numberOfItemsProcessed = 0; while ($pageTimer->getTime() < 60) { $cronJobList = Cron::getList(1); if (count($cronJobList) <= 0) { break; } foreach ($cronJobList as $cronJob) { _runnerLog('Found Job: ' . $cronJob->id); flush(); _runnerLog('Changing the status to RUNNING for JobID: ' . $cronJob->id); $cronJob->status = CronDAO::STATUS_RUNNING; $cronJob->runTime = time(); $cronJob->save(); flush(); $cronJob->result = -1; $cronJob->message = ''; _runnerLog('Running command: ' . $cronJob->command . ' ID:' . $cronJob->id); unset($result); @eval('$result = ' . $cronJob->command); if (isset($result) && is_array($result)) { $cronJob->result = $result[0]; $cronJob->message = $result[1]; } _runnerLog('Finished Job ID: ' . $cronJob->id . ' Result: ' . $cronJob->result . ' Message: ' . $cronJob->message); $cronJob->status = CronDAO::STATUS_COMPLETE; $cronJob->save(); flush(); $numberOfItemsProcessed++; } } _runnerLog('Processed ' . $numberOfItemsProcessed . ' items.'); _runnerLog($pageTimer->logLoadTime()); }
public static function add($command, $id = 0, $priority = 0) { $cronJob = new Cron(['command' => $command, 'ref' => $id, 'status' => self::STATUS_NEW, 'priority' => $priority]); $cronJob->save(); return $cronJob; }