-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
|
||
// Copyright 2023 Catchpoint Systems Inc. | ||
// Use of this source code is governed by the Polyform Shield 1.0.0 license that can be | ||
// found in the LICENSE.md file. | ||
chdir('..'); | ||
$MIN_DAYS = 2; | ||
|
||
require_once('common.inc'); | ||
require_once('clearCache.inc'); | ||
ignore_user_abort(true); | ||
set_time_limit(3300); // only allow it to run for 55 minutes | ||
error_reporting(E_ALL); | ||
$is_cli = php_sapi_name() == "cli"; | ||
if ($is_cli && function_exists('proc_nice')) { | ||
proc_nice(19); | ||
} | ||
|
||
// bail if we are already running | ||
$lock = Lock("ClearLocalCache", false, 3600); | ||
if (!isset($lock)) { | ||
if ($is_cli) { | ||
echo "Clear local cache process is already running\n"; | ||
} | ||
exit(0); | ||
} | ||
|
||
$days_results_cached = null; | ||
if (GetSetting('days_results_cached')) { | ||
$days_results_cached = GetSetting('days_results_cached'); | ||
} | ||
|
||
$kept = 0; | ||
$deleted = 0; | ||
$log = fopen('./cli/clearedCache.log', 'w'); | ||
|
||
$MIN_DAYS = max($days_results_cached, 0.1); | ||
$UTC = new DateTimeZone('UTC'); | ||
$now = time(); | ||
|
||
// Delete the cached results | ||
if (isset($days_results_cached)) { | ||
$years = scandir('./results'); | ||
foreach ($years as $year) { | ||
$yearDir = "./results/$year"; | ||
if (is_numeric($year) && is_dir($yearDir) && $year != '.' && $year != '..') { | ||
$months = scandir($yearDir); | ||
foreach ($months as $month) { | ||
$monthDir = "$yearDir/$month"; | ||
if (is_dir($monthDir) && $month != '.' && $month != '..') { | ||
$days = scandir($monthDir); | ||
foreach ($days as $day) { | ||
$dayDir = "$monthDir/$day"; | ||
if (is_dir($dayDir) && $day != '.' && $day != '..') { | ||
$elapsedDays = ElapsedDays($year, $month, $day); | ||
CheckDay($dayDir, "$year$month$day", $elapsedDays); | ||
} | ||
} | ||
@rmdir($monthDir); | ||
} | ||
} | ||
@rmdir($yearDir); | ||
} | ||
} | ||
} | ||
|
||
if ($is_cli) { | ||
echo "\nDone\n\n"; | ||
} | ||
|
||
if ($log) { | ||
fwrite($log, "Deleted: $deleted\nKept: $kept\n" . gmdate('r') . "\n"); | ||
fclose($log); | ||
} | ||
|
||
Unlock($lock); | ||
|
||
/** | ||
* Recursively check within a given day | ||
* | ||
* @param mixed $dir | ||
* @param mixed $baseID | ||
*/ | ||
function CheckDay($dir, $baseID, $elapsedDays) | ||
{ | ||
if (is_dir($dir)) { | ||
$tests = scandir($dir); | ||
if (isset($tests) && is_array($tests) && count($tests)) { | ||
foreach ($tests as $test) { | ||
if ($test != '.' && $test != '..') { | ||
// see if it is a test or a higher-level directory | ||
if ( | ||
is_file("$dir/$test/testinfo.ini") || | ||
is_file("$dir/$test/testinfo.json.gz") || | ||
is_file("$dir/$test/testinfo.json") || | ||
is_dir("$dir/$test/video_1") | ||
) { | ||
CheckTest("$dir/$test", "{$baseID}_$test", $elapsedDays); | ||
} else { | ||
// We're likely looking at a shared directory, loop through the actual tests | ||
CheckDay("$dir/$test", "{$baseID}_$test", $elapsedDays); | ||
} | ||
} | ||
} | ||
} | ||
@rmdir($dir); | ||
} | ||
} | ||
|
||
/** | ||
* Check the given logfile for all matching tests | ||
* | ||
* @param mixed $logFile | ||
* @param mixed $match | ||
*/ | ||
function CheckTest($testPath, $id, $elapsedDays) | ||
{ | ||
global $deleted; | ||
global $kept; | ||
global $log; | ||
global $MIN_DAYS; | ||
global $is_cli; | ||
$logLine = "$id ($elapsedDays): "; | ||
|
||
if ($is_cli) { | ||
echo "\rDeleted:$deleted, Kept:$kept, Checking:" . str_pad($id, 45); | ||
} | ||
|
||
$delete = false; | ||
if (is_file("$testPath/test.waiting")) { | ||
// Skip tests that are still queued | ||
} elseif (is_file("$testPath/test.running")) { | ||
// Skip tests that are still running | ||
} else { | ||
$elapsed = TestLastAccessed($id); | ||
if (isset($elapsed)) { | ||
$logLine .= "Last Accessed $elapsed days"; | ||
if ($elapsed >= $MIN_DAYS) { | ||
$delete = true; | ||
$logLine .= " Deleting"; | ||
} | ||
} | ||
} | ||
|
||
if ($delete) { | ||
delTree("$testPath/"); | ||
$deleted++; | ||
$logLine .= " Deleted"; | ||
} else { | ||
$kept++; | ||
} | ||
|
||
if ($log) { | ||
$logLine .= "\n"; | ||
fwrite($log, $logLine); | ||
} | ||
} | ||
|
||
/** | ||
* Calculate how many days have passed since the given day | ||
*/ | ||
function ElapsedDays($year, $month, $day) | ||
{ | ||
global $now; | ||
global $UTC; | ||
$date = DateTime::createFromFormat('ymd', "$year$month$day", $UTC); | ||
$daytime = $date->getTimestamp(); | ||
$elapsed = max($now - $daytime, 0) / 86400; | ||
return $elapsed; | ||
} |