Same name and namespace in other branches
  1. 6.0.x advagg.module \advagg_cron() 1 comment
  2. 7.x-1.x advagg.module \advagg_cron() 1 comment
  3. 7.x-2.x advagg.module \advagg_cron() 1 comment
  4. 8.x-2.x advagg.module \advagg_cron() 1 comment
  5. 8.x-3.x advagg.module \advagg_cron() 1 comment
  6. 8.x-4.x advagg.module \advagg_cron() 1 comment

Implements hook_cron().

Deletes outdated file/aggregate data.

3 calls to advagg_cron()
AdvaggCommands::cron in src/Commands/AdvaggCommands.php
Run the advagg cron hook.
drush_advagg_cron in ./advagg.drush.inc
Callback function for drush advagg-cron.
OperationsForm::clearStaleAggregates in src/Form/OperationsForm.php
Clear out all stale advagg aggregated files.

File

./advagg.module, line 46

Code

function advagg_cron($bypass = FALSE) {
    $time = \Drupal::time()->getRequestTime();
    $state = \Drupal::state();
    $file_system = \Drupal::service('file_system');
    // Ensure the htaccess files exist.
    AssetOptimizer::generateHtaccess('css');
    AssetOptimizer::generateHtaccess('js');
    // Execute only if been longer than advagg cron setting since last run.
    if (!$bypass && $state->get('advagg.cron_timestamp', $time) > $time - \Drupal::config('advagg.settings')->get('cron_frequency')) {
        return [];
    }
    $state->set('advagg.cron_timestamp', $time);
    $outdated = [
        'css' => [],
        'js' => [],
    ];
    $file_system = \Drupal::service('file_system');
    $cache = \Drupal::cache('advagg');
    $stale_file_threshold = \Drupal::config('system.performance')->get('stale_file_threshold');
    foreach ([
        'css',
        'js',
    ] as $type) {
        $path = $file_system->realpath("public://{$type}/optimized");
        if ($files = glob("{$path}/*.{$type}")) {
            foreach ($files as $file) {
                // Only delete files older than stale file threshold defined under Cron Options.
                if (filemtime($file) > $time - $stale_file_threshold) {
                    continue;
                }
                $filename = str_replace([
                    'css_',
                    'js_',
                ], '', pathinfo($file, PATHINFO_FILENAME));
                $cid = substr($filename, 0, strpos($filename, '.'));
                if (!$cache->get($cid)) {
                    @$file_system->delete($file);
                    @$file_system->delete("{$file}.gz");
                    $outdated[$type][] = "{$filename}.{$type}";
                }
            }
        }
    }
    return $outdated;
}