Add manual Piwigo cache build controls

This commit is contained in:
Terranom674
2026-08-15 19:16:59 +02:00
parent 0f9a107bf9
commit ad983163a6

View File

@@ -108,7 +108,7 @@ function bratonien_tools_clear_image_cache()
return array( return array(
'message' => sprintf( 'message' => sprintf(
'Bildcache vollstaendig geleert: %d Dateien (%s) entfernt, davon %d Custom-Derivate. %d Restdateien wurden zusaetzlich ausserhalb von Piwigos Standard-Loeschmustern entfernt. Der Cache wird bei Bedarf automatisch neu aufgebaut.', 'Bildcache vollstaendig geleert: %d Dateien (%s) entfernt, davon %d Custom-Derivate. %d Restdateien wurden zusaetzlich ausserhalb von Piwigos Standard-Loeschmustern entfernt. Der Cache wird bei Bedarf automatisch neu aufgebaut oder kann manuell vorbereitet werden.',
$before['files'], $before['files'],
bratonien_tools_format_bytes($before['bytes']), bratonien_tools_format_bytes($before['bytes']),
$before['custom'], $before['custom'],
@@ -167,6 +167,124 @@ function bratonien_tools_scan_image_cache($cache_root)
return $result; return $result;
} }
function bratonien_tools_main_cache_status_file()
{
return PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'bratonien-tools-main-cache.status.json';
}
function bratonien_tools_write_main_cache_status(array $status)
{
$file = bratonien_tools_main_cache_status_file();
$directory = dirname($file);
if (!is_dir($directory) && !mkdir($directory, 0755, true) && !is_dir($directory))
{
throw new RuntimeException('Cache-Status konnte nicht vorbereitet werden.');
}
$payload = array_merge(array(
'state'=>'idle',
'message'=>'',
'total'=>0,
'completed'=>0,
'generated'=>0,
'cached'=>0,
'skipped'=>0,
'errors'=>0,
'current'=>'',
'updated_at'=>time(),
), $status);
$payload['updated_at'] = time();
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if ($json === false || @file_put_contents($file, $json."\n", LOCK_EX) === false)
{
throw new RuntimeException('Cache-Status konnte nicht gespeichert werden.');
}
@chmod($file, 0664);
}
function bratonien_tools_main_cache_is_running()
{
$file = bratonien_tools_main_cache_status_file();
if (!is_file($file) || !is_readable($file))
{
return false;
}
$raw = @file_get_contents($file);
$status = $raw !== false ? json_decode($raw, true) : null;
if (!is_array($status) || ($status['state'] ?? '') !== 'running')
{
return false;
}
$updated = (int)($status['updated_at'] ?? 0);
return $updated > 0 && (time() - $updated) < 30;
}
function bratonien_tools_find_php_cli()
{
foreach (array('/usr/bin/php', '/usr/bin/php8.4', '/usr/local/bin/php') as $candidate)
{
if (is_file($candidate) && is_executable($candidate))
{
return $candidate;
}
}
return null;
}
function bratonien_tools_start_main_cache_build()
{
if (bratonien_tools_main_cache_is_running())
{
return array('started'=>false, 'running'=>true, 'message'=>'Der Piwigo-Bildcache wird bereits aufgebaut.');
}
$worker = realpath(BRATONIEN_TOOLS_PATH.'main-cache-build.php');
$php = bratonien_tools_find_php_cli();
if (!$worker || !is_file($worker))
{
throw new RuntimeException('Cache-Worker wurde nicht gefunden.');
}
if (!$php)
{
throw new RuntimeException('PHP-CLI wurde nicht gefunden.');
}
if (!function_exists('exec'))
{
throw new RuntimeException('PHP exec() ist deaktiviert; Cache-Worker kann nicht gestartet werden.');
}
bratonien_tools_write_main_cache_status(array(
'state'=>'queued',
'message'=>'Piwigo-Bildcache wird vorbereitet.',
));
$log = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'bratonien-tools-main-cache.log';
$command = 'nohup '.escapeshellarg($php).' '.escapeshellarg($worker).' >> '.escapeshellarg($log).' 2>&1 < /dev/null & echo $!';
$output = array();
$exit = 1;
@exec($command, $output, $exit);
$pid = isset($output[0]) ? (int)$output[0] : 0;
if ($exit !== 0 || $pid <= 0)
{
bratonien_tools_write_main_cache_status(array(
'state'=>'error',
'message'=>'Piwigo-Bildcache konnte nicht gestartet werden.',
'errors'=>1,
));
throw new RuntimeException('Piwigo-Bildcache konnte nicht gestartet werden.');
}
return array(
'started'=>true,
'pid'=>$pid,
'message'=>'Piwigo-Bildcache wurde manuell gestartet.',
);
}
function bratonien_tools_format_bytes($bytes) function bratonien_tools_format_bytes($bytes)
{ {
$units = array('B', 'KB', 'MB', 'GB', 'TB'); $units = array('B', 'KB', 'MB', 'GB', 'TB');