Same name in other branches
- 5.0.x advagg.module \advagg_cron()
- 7.x-1.x advagg.module \advagg_cron()
- 7.x-2.x advagg.module \advagg_cron()
- 8.x-2.x advagg.module \advagg_cron()
- 8.x-3.x advagg.module \advagg_cron()
- 8.x-4.x advagg.module \advagg_cron()
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;
}