rename retry to retries

This commit is contained in:
DubbleClick
2025-04-22 14:34:43 +07:00
parent 0b3421e3cd
commit a29992b907

View File

@@ -201,7 +201,7 @@ class Downloader
} }
}; };
self::registerCancelEvent($cancel_func); self::registerCancelEvent($cancel_func);
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: self::getRetryAttempts()); self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retries: self::getRetryAttempts());
self::unregisterCancelEvent(); self::unregisterCancelEvent();
logger()->debug("Locking {$filename}"); logger()->debug("Locking {$filename}");
if ($download_as === SPC_DOWNLOAD_PRE_BUILT) { if ($download_as === SPC_DOWNLOAD_PRE_BUILT) {
@@ -241,7 +241,7 @@ class Downloader
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException * @throws WrongUsageException
*/ */
public static function downloadGit(string $name, string $url, string $branch, ?string $move_path = null, int $retry = 0, int $lock_as = SPC_DOWNLOAD_SOURCE): void public static function downloadGit(string $name, string $url, string $branch, ?string $move_path = null, int $retries = 0, int $lock_as = SPC_DOWNLOAD_SOURCE): void
{ {
$download_path = FileSystem::convertPath(DOWNLOAD_PATH . "/{$name}"); $download_path = FileSystem::convertPath(DOWNLOAD_PATH . "/{$name}");
if (file_exists($download_path)) { if (file_exists($download_path)) {
@@ -267,8 +267,8 @@ class Downloader
if ($e->getCode() === 2 || $e->getCode() === -1073741510) { if ($e->getCode() === 2 || $e->getCode() === -1073741510) {
throw new WrongUsageException('Keyboard interrupted, download failed !'); throw new WrongUsageException('Keyboard interrupted, download failed !');
} }
if ($retry > 0) { if ($retries > 0) {
self::downloadGit($name, $url, $branch, $move_path, $retry - 1); self::downloadGit($name, $url, $branch, $move_path, $retries - 1);
return; return;
} }
throw $e; throw $e;
@@ -510,36 +510,23 @@ class Downloader
$hook($method, $url, $headers); $hook($method, $url, $headers);
} }
try { FileSystem::findCommandPath('curl');
FileSystem::findCommandPath('curl');
$methodArg = match ($method) { $methodArg = match ($method) {
'GET' => '', 'GET' => '',
'HEAD' => '-I', 'HEAD' => '-I',
default => "-X \"{$method}\"", default => "-X \"{$method}\"",
}; };
$headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $headers)); $headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $headers));
$retry = $retries > 0 ? "--retry {$retries}" : '';
$cmd = SPC_CURL_EXEC . " -sfSL {$methodArg} {$headerArg} \"{$url}\""; $cmd = SPC_CURL_EXEC . " -sfSL {$retry} {$methodArg} {$headerArg} \"{$url}\"";
if (getenv('CACHE_API_EXEC') === 'yes') { if (getenv('CACHE_API_EXEC') === 'yes') {
if (!file_exists(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'))) { if (!file_exists(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'))) {
$cache = []; $cache = [];
} else { } else {
$cache = json_decode(file_get_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache')), true); $cache = json_decode(file_get_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache')), true);
} }
if (isset($cache[$cmd]) && $cache[$cmd]['expire'] >= time()) { if (isset($cache[$cmd]) && $cache[$cmd]['expire'] >= time()) {
return $cache[$cmd]['cache'];
}
f_exec($cmd, $output, $ret);
if ($ret === 2 || $ret === -1073741510) {
throw new RuntimeException('failed http fetch');
}
if ($ret !== 0) {
throw new DownloaderException('failed http fetch');
}
$cache[$cmd]['cache'] = implode("\n", $output);
$cache[$cmd]['expire'] = time() + 3600;
file_put_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'), json_encode($cache));
return $cache[$cmd]['cache']; return $cache[$cmd]['cache'];
} }
f_exec($cmd, $output, $ret); f_exec($cmd, $output, $ret);
@@ -549,15 +536,19 @@ class Downloader
if ($ret !== 0) { if ($ret !== 0) {
throw new DownloaderException('failed http fetch'); throw new DownloaderException('failed http fetch');
} }
return implode("\n", $output); $cache[$cmd]['cache'] = implode("\n", $output);
} catch (DownloaderException $e) { $cache[$cmd]['expire'] = time() + 3600;
if ($retries > 0) { file_put_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'), json_encode($cache));
logger()->notice('Retrying curl exec after 5 seconds...'); return $cache[$cmd]['cache'];
sleep(5);
return self::curlExec($url, $method, $headers, $hooks, $retries - 1);
}
throw $e;
} }
f_exec($cmd, $output, $ret);
if ($ret === 2 || $ret === -1073741510) {
throw new RuntimeException('failed http fetch');
}
if ($ret !== 0) {
throw new DownloaderException('failed http fetch');
}
return implode("\n", $output);
} }
/** /**
@@ -566,7 +557,7 @@ class Downloader
* @throws RuntimeException * @throws RuntimeException
* @throws WrongUsageException * @throws WrongUsageException
*/ */
public static function curlDown(string $url, string $path, string $method = 'GET', array $headers = [], array $hooks = [], int $retry = 0): void public static function curlDown(string $url, string $path, string $method = 'GET', array $headers = [], array $hooks = [], int $retries = 0): void
{ {
$used_headers = $headers; $used_headers = $headers;
foreach ($hooks as $hook) { foreach ($hooks as $hook) {
@@ -580,18 +571,14 @@ class Downloader
}; };
$headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $used_headers)); $headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $used_headers));
$check = !defined('DEBUG_MODE') ? 's' : '#'; $check = !defined('DEBUG_MODE') ? 's' : '#';
$cmd = SPC_CURL_EXEC . " -{$check}fSL -o \"{$path}\" {$methodArg} {$headerArg} \"{$url}\""; $retry = $retries > 0 ? "--retry {$retries}" : '';
$cmd = SPC_CURL_EXEC . " -{$check}fSL {$retry} -o \"{$path}\" {$methodArg} {$headerArg} \"{$url}\"";
try { try {
f_passthru($cmd); f_passthru($cmd);
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
if ($e->getCode() === 2 || $e->getCode() === -1073741510) { if ($e->getCode() === 2 || $e->getCode() === -1073741510) {
throw new WrongUsageException('Keyboard interrupted, download failed !'); throw new WrongUsageException('Keyboard interrupted, download failed !');
} }
if ($retry > 0) {
logger()->notice('Retrying curl download ...');
self::curlDown($url, $path, $method, $used_headers, retry: $retry - 1);
return;
}
throw $e; throw $e;
} }
} }