You are here

function cdn_cron_run in CDN 5

Executes a CDN synchronization cron run when called

Graciously stolen from Drupal's includes/common.inc.

2 calls to cdn_cron_run()
cdn_cron in ./cdn.module
Implementation of hook_cron().
cdn_cron.php in ./cdn_cron.php

File

./cdn_cron.inc, line 14
Basic functions for CDN synchronization cron.

Code

function cdn_cron_run() {
  if (!ini_get('safe_mode')) {
    set_time_limit(240);
  }

  // Fetch the CDN synchronization cron semaphore
  $semaphore = variable_get('cdn_cron_semaphore', FALSE);
  _cdn_cron_delete_temporary_files();
  if ($semaphore) {
    if (time() - $semaphore > 3600) {

      // Either cron has been running for more than an hour or the semaphore
      // was not reset due to a database error.
      watchdog('cdn_cron', 'CDN synchronization cron has been running for more than an hour and is most likely stuck.', WATCHDOG_ERROR);

      // Release CDN synchronization cron semaphore
      variable_del('cdn_cron_semaphore');
    }
    else {

      // CDN synchronization cron is still running normally.
      watchdog('cdn_cron', 'Attempting to re-run CDN synchronization cron while it is already running.', WATCHDOG_WARNING);
    }
  }
  else {

    // Register shutdown callback
    register_shutdown_function('cdn_cron_cleanup');

    // Lock CDN synchronization cron semaphore
    variable_set('cdn_cron_semaphore', time());

    // Get the CDN integration configuration.
    $sync_method = variable_get('cdn_sync_method', 'ftp');
    $sync_filters = variable_get('cdn_sync_filters', array());
    $sync_method_settings = variable_get('cdn_sync_method_settings', FALSE);

    // Get the list of files, the unique settings for each file, and the
    // update settings for each file.
    list($files, $files_unique_settings, $files_update_setting) = _cdn_cron_get_files_to_sync($sync_filters);

    // Make all filenames unique.
    $files_unique = array_combine(array_keys($files), array_map('cdn_unique_filename', array_keys($files), $files_unique_settings));

    // Store the files that are being synced in a variable, these URLs will be
    // used while updating files.
    variable_set('cdn_cron_files_syncing', $files_unique);

    // Update files if necessary.
    $files_updated = array_combine(array_keys($files), _cdn_cron_update_files_wrapper(array_keys($files), $files_update_setting));

    // Perform the CDN synchronization using the configured method. Default to
    // the FTP  method.
    require_once drupal_get_path('module', 'cdn') . "/sync_plugins/{$sync_method}.inc";
    $sync_hook = $sync_method . '_cdn_cron_perform_sync';
    timer_start('cdn_sync');
    $stats = $sync_hook($files, $files_unique, $files_updated, _cdn_cron_init_stats(), $sync_method_settings);
    $timer = timer_stop('cdn_sync');
    _cdn_cron_delete_temporary_files();

    // Build message.
    $duration = round($timer['time'] / 1000, 1);
    extract($stats);
    $exists_kbytes = number_format($exists_bytes / 1024, 1);
    $uploaded_kbytes = number_format($uploaded_bytes / 1024, 1);
    $message = "<ul>";
    $message = "<li>Method: <em>{$sync_method}</em></li>";
    $message .= "<li>Duration: <em>{$duration}</em> seconds</li>";
    $message .= "<li>Deleted: <em>{$deletes}</em> files</li>";
    if ($exists_bytes) {
      $message .= "<li>No action: <em>{$exists}</em> files ({$exists_kbytes} KB)</li>";
    }
    else {
      $message .= "<li>No action: <em>{$exists}</em> files</li>";
    }
    if ($uploaded_bytes) {
      $message .= "<li>Uploaded: <em>{$uploads}</em> files ({$uploaded_kbytes} KB)</li>";
    }
    else {
      $message .= "<li>Uploaded: <em>{$uploads}</em> files</li>";
    }
    $message .= '</ul>';

    // Log to watchdog.
    watchdog('cdn_cron', "CDN synchronization cron run completed.<br />{$message}", WATCHDOG_NOTICE);
    if ($uploads_failed) {
      watchdog('cdn_cron', "{$uploads_failed} file uploads have failed.", WATCHDOG_ERROR);
    }

    // Record CDN synchronization cron time and statistics message for usage
    // in the status report at admin/logs/status.
    variable_set('cdn_cron_last', time());
    variable_set('cdn_cron_last_stats', $message);

    // Record which files have been synchronized. We have to know this to be
    // able to generate CDN URLs only for files that have been synchronized.
    variable_set('cdn_files_synced', $files_unique);

    // Delete the variable that contains an array of files that were being
    // synced, which /are/ synced by now.
    variable_del('cdn_cron_files_syncing');

    // Release CDN synchronization cron semaphore
    variable_del('cdn_cron_semaphore');

    // Return TRUE so other functions can check if it did run successfully
    return TRUE;
  }
}