function CronExec($oP, $aProcesses, $bVerbose) { $iStarted = time(); $iMaxDuration = MetaModel::GetConfig()->Get('cron_max_execution_time'); $iTimeLimit = $iStarted + $iMaxDuration; if ($bVerbose) { $oP->p("Planned duration = {$iMaxDuration} seconds"); } // Reset the next planned execution to take into account new settings $oSearch = new DBObjectSearch('BackgroundTask'); $oTasks = new DBObjectSet($oSearch); while ($oTask = $oTasks->Fetch()) { $sTaskClass = $oTask->Get('class_name'); $oRefClass = new ReflectionClass($sTaskClass); $oNow = new DateTime(); if ($oRefClass->implementsInterface('iScheduledProcess') && ($oTask->Get('status') != 'active' || $oTask->Get('next_run_date') > $oNow->format('Y-m-d H:i:s'))) { if ($bVerbose) { $oP->p("Resetting the next run date for {$sTaskClass}"); } $oProcess = $aProcesses[$sTaskClass]; $oNextOcc = $oProcess->GetNextOccurrence(); $oTask->Set('next_run_date', $oNextOcc->format('Y-m-d H:i:s')); $oTask->DBUpdate(); } } $iCronSleep = MetaModel::GetConfig()->Get('cron_sleep'); $oSearch = new DBObjectSearch('BackgroundTask'); while (time() < $iTimeLimit) { $oTasks = new DBObjectSet($oSearch); $aTasks = array(); while ($oTask = $oTasks->Fetch()) { $aTasks[$oTask->Get('class_name')] = $oTask; } foreach ($aProcesses as $oProcess) { $sTaskClass = get_class($oProcess); $oNow = new DateTime(); if (!array_key_exists($sTaskClass, $aTasks)) { // New entry, let's create a new BackgroundTask record, and plan the first execution $oTask = new BackgroundTask(); $oTask->Set('class_name', get_class($oProcess)); $oTask->Set('total_exec_count', 0); $oTask->Set('min_run_duration', 99999.999); $oTask->Set('max_run_duration', 0); $oTask->Set('average_run_duration', 0); $oRefClass = new ReflectionClass($sTaskClass); if ($oRefClass->implementsInterface('iScheduledProcess')) { $oNextOcc = $oProcess->GetNextOccurrence(); $oTask->Set('next_run_date', $oNextOcc->format('Y-m-d H:i:s')); } else { // Background processes do start asap, i.e. "now" $oTask->Set('next_run_date', $oNow->format('Y-m-d H:i:s')); } if ($bVerbose) { $oP->p('Creating record for: ' . $sTaskClass); $oP->p('First execution planned at: ' . $oTask->Get('next_run_date')); } $oTask->DBInsert(); $aTasks[$oTask->Get('class_name')] = $oTask; } if ($aTasks[$sTaskClass]->Get('status') == 'active' && $aTasks[$sTaskClass]->Get('next_run_date') <= $oNow->format('Y-m-d H:i:s')) { // Run the task and record its next run time if ($bVerbose) { $oP->p(">> === " . $oNow->format('Y-m-d H:i:s') . sprintf(" Starting:%-'=40s", ' ' . $sTaskClass . ' ')); } $sMessage = RunTask($oProcess, $aTasks[$sTaskClass], $oNow, $iTimeLimit); if ($bVerbose) { if (!empty($sMessage)) { $oP->p("{$sTaskClass}: {$sMessage}"); } $oEnd = new DateTime(); $oP->p("<< === " . $oEnd->format('Y-m-d H:i:s') . sprintf(" End of: %-'=40s", ' ' . $sTaskClass . ' ')); } } else { // will run later if ($aTasks[$sTaskClass]->Get('status') == 'active' && $bVerbose) { $oP->p("Skipping asynchronous task: {$sTaskClass} until " . $aTasks[$sTaskClass]->Get('next_run_date')); } } } if ($bVerbose) { $oP->p("Sleeping"); } sleep($iCronSleep); } if ($bVerbose) { $oP->p("Reached normal execution time limit (exceeded by " . (time() - $iTimeLimit) . "s)"); } }
break; //用户详细 //用户详细 case 'saveuser': if (Gants(0)) { SaveUser(); } else { echo "error-gants0"; exit(0); } break; //保存用户信息 //保存用户信息 case 'runtask': if (Gants(0) || Gants(5)) { RunTask(); } else { echo "error-gants0"; exit(0); } break; //推送任务 //推送任务 case 'chtask': if (Gants(0) || Gants(5)) { CheckTaskState(); } else { echo "error-gants0"; exit(0); } break;