Ejemplo n.º 1
0
function cron_dispatcher()
{
    // runs due or overdue jobs
    // get last cron.php run time
    $cronLastRunTime = $GLOBALS['SETTINGS']['bgtasks_lastRun'];
    $thisCronRunTime = time();
    // call log function if cron jobs exit or die
    register_shutdown_function('cron_logErrorsOnDieOrExit');
    // run cron tasks
    $dispatchedTaskCounter = 0;
    foreach (getCronList() as $cron) {
        //print "DEBUG: Checking... " .$cron['function']. " => " .$cron['expression']. "\n";
        // get last job run time and oldest time to check
        $jobLastLogRecord = mysql_get('_cron_log', null, ' function = "' . mysql_escape($cron['function']) . '" ORDER BY num DESC');
        $jobLastRunTime = strtotime($jobLastLogRecord['createdDate']);
        $oldestTimeToCheck = max($cronLastRunTime, $jobLastRunTime);
        // get most recent valid run time (from now to the last time cron.php ran)
        $lastScheduleRunTime = cronExpression_getLastScheduledTime($cron['expression'], $oldestTimeToCheck, $cronExprParseErrors);
        $skipTask = false;
        if (!$lastScheduleRunTime && !$cronExprParseErrors) {
            $skipTask = true;
        }
        // skip if no scheduled runtime found since last cronrun (and no errors which might have caused that)
        if ($lastScheduleRunTime && $lastScheduleRunTime <= $cronLastRunTime) {
            $skipTask = true;
        }
        // skip if scheduled to run, but not quite yet (if scheduled time is blank then there was an error)
        if ($thisCronRunTime - 60 < $jobLastRunTime) {
            $skipTask = true;
        }
        // don't run jobs more than once a minute
        if ($skipTask) {
            if (!inCLI()) {
                print "Skipping {$cron['activity']}, function: {$cron['function']} (not scheduled to run again yet)\n";
            }
            continue;
        }
        // Add log entry for job
        $hasLock = mysql_get_lock($cron['function']);
        // get a lock for this specific function
        if ($cronExprParseErrors) {
            $summary = $cronExprParseErrors;
        } elseif (!$hasLock) {
            $summary = t('Aborting, task still running from last time.');
        } else {
            $summary = t('Running...');
        }
        $jobLogNum = mysql_insert('_cron_log', array('createdDate=' => 'NOW()', 'function' => $cron['function'], 'activity' => $cron['activity'], 'summary' => $summary, 'completed' => 0));
        // skip if errors parsing cronExpression or getting lock
        if ($cronExprParseErrors || !$hasLock) {
            continue;
        }
        // execute function
        $dispatchedTaskCounter++;
        if (!inCLI()) {
            print "Running {$cron['activity']}, function: {$cron['function']}\n";
        }
        ob_start();
        $startTime = microtime(true);
        $GLOBALS['CRON_JOB_START'] = $startTime;
        // store job num in a global so we can update it after die/exit with cron_logErrorsOnDieOrExit
        $GLOBALS['CRON_JOB_LOG_NUM'] = $jobLogNum;
        // store job num in a global so we can update it after die/exit with cron_logErrorsOnDieOrExit
        $summary = call_user_func($cron['function'], array('note' => 'this $info array is for future use'));
        $GLOBALS['CRON_JOB_LOG_NUM'] = '';
        $endTime = microtime(true);
        $output = ob_get_clean();
        // update job log entry
        mysql_update('_cron_log', $jobLogNum, null, array('completed' => 1, 'summary' => $summary, 'output' => $output, 'runtime' => sprintf("%0.2f", $endTime - $startTime)));
        mysql_release_lock($cron['function']);
    }
    // update lastrun time
    $GLOBALS['SETTINGS']['bgtasks_lastRun'] = time();
    saveSettings();
}
function addCronJob($functionName, $activityName, $cronExpression)
{
    if (!$functionName) {
        dieAsCaller(__FUNCTION__ . ": No functioname specified!");
    }
    if (!function_exists($functionName)) {
        dieAsCaller(__FUNCTION__ . ": Specified function '" . htmlencode($functionName) . "' doesn't exist!");
    }
    // add actions
    $cronList =& getCronList();
    if (array_key_exists($functionName, $cronList)) {
        dieAsCaller(__FUNCTION__ . ": Specified function '" . htmlencode($functionName) . "' already exists in cron list!");
    }
    $cronList[$functionName] = array('function' => $functionName, 'activity' => $activityName, 'expression' => $cronExpression);
}
Ejemplo n.º 3
0
et('Activity');
?>
</th>
                  <th><?php 
et('Last Run');
?>
</th>
                  <th><?php 
et('Frequency');
?>
 (<a href="http://en.wikipedia.org/wiki/Cron#CRON_expression" target="_blank">?</a>)</th>
                </tr>
              </thead>

              <?php 
$cronRecords = getCronList();
if ($cronRecords) {
    ?>

              <?php 
    foreach ($cronRecords as $record) {
        ?>
                <tr class="listRow <?php 
        echo @++$cronTaskCounter % 2 ? 'listRowOdd' : 'listRowEven';
        ?>
">
                  <td><?php 
        echo htmlencode($record['function']);
        ?>
</td>
                  <td><?php