| Server IP : 103.88.176.108 / Your IP : 216.73.216.211 Web Server : Apache/2.4.41 (Ubuntu) System : Linux webserver 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 User : www-data ( 33) PHP Version : 7.4.3-4ubuntu2.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/wp-content/plugins/wp-cloudflare-page-cache/src/Modules/ |
Upload File : |
<?php
namespace SPC\Modules;
use SPC\Constants;
use DateTime;
use DateTimeZone;
use SPC\Services\Metrics;
class Metrics_Cleanup implements Module_Interface {
const HOOK = 'spc/prune_metric_files';
/**
* Register the recurring task and callback.
*/
public function init(): void {
add_action( 'init', [ $this, 'schedule_cleanup' ] );
add_action( self::HOOK, [ $this, 'prune_files' ] );
}
/**
* Schedule the daily cleanup.
*
* @return void
*/
public function schedule_cleanup() {
if ( ! is_dir( SPC_METRICS_DIR ) ) {
return;
}
if ( as_next_scheduled_action( self::HOOK ) ) {
return;
}
as_schedule_recurring_action(
(int) gmdate( 'U', strtotime( 'tomorrow 00:07 UTC' ) ),
DAY_IN_SECONDS,
self::HOOK,
[],
Constants::ACTION_SCHEDULER_GROUP
);
}
/**
* Delete old bucket files based on their hourāstamp.
*
* @return void
*/
public static function prune_files(): void {
if ( ! is_dir( SPC_METRICS_DIR ) ) {
return;
}
$cfg = Metrics::get_config();
$window_hours = isset( $cfg['window'] ) ? max( 1, (int) $cfg['window'] ) : 24;
$cutoff_in_seconds = time() - $window_hours * HOUR_IN_SECONDS;
foreach ( glob( SPC_METRICS_DIR . '/*.txt' ) as $file ) {
$base = basename( $file );
// Extract 10-digit hour stamp: hit_2025061213.txt -> 2025061213
if ( ! preg_match( '/^(?:hit|miss)_(\d{10})\.txt$/', $base, $m ) ) {
continue;
}
$bucket_timestamp = DateTime::createFromFormat(
'YmdH',
$m[1],
new DateTimeZone( 'UTC' )
)->getTimestamp();
if ( $bucket_timestamp >= $cutoff_in_seconds ) {
continue;
}
@unlink( $file );
}
}
}