feat: improve downloader retry (#558)

- Refactored to remove duplicate retry expressions by utilizing the getRetryTime() method.
- Fixed a typo in the log message.
This commit is contained in:
yinheli 2024-11-09 10:50:08 +08:00 committed by GitHub
parent 0fb7784a20
commit 3c4d47d072
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,7 +28,7 @@ class Downloader
logger()->debug("finding {$name} source from bitbucket tag");
$data = json_decode(self::curlExec(
url: "https://api.bitbucket.org/2.0/repositories/{$source['repo']}/refs/tags",
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
retry: self::getRetryTime()
), true);
$ver = $data['values'][0]['name'];
if (!$ver) {
@ -38,7 +38,7 @@ class Downloader
$headers = self::curlExec(
url: $url,
method: 'HEAD',
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
retry: self::getRetryTime()
);
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
if ($matches) {
@ -66,7 +66,7 @@ class Downloader
$data = json_decode(self::curlExec(
url: "https://api.github.com/repos/{$source['repo']}/{$type}",
hooks: [[CurlHook::class, 'setupGithubToken']],
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
retry: self::getRetryTime()
), true);
if (($source['prefer-stable'] ?? false) === false) {
@ -85,7 +85,7 @@ class Downloader
url: $url,
method: 'HEAD',
hooks: [[CurlHook::class, 'setupGithubToken']],
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
retry: self::getRetryTime()
);
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
if ($matches) {
@ -108,11 +108,11 @@ class Downloader
*/
public static function getLatestGithubRelease(string $name, array $source, bool $match_result = true): array
{
logger()->debug("finding {$name} from github releases assests");
logger()->debug("finding {$name} from github releases assets");
$data = json_decode(self::curlExec(
url: "https://api.github.com/repos/{$source['repo']}/releases",
hooks: [[CurlHook::class, 'setupGithubToken']],
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
retry: self::getRetryTime()
), true);
$url = null;
foreach ($data as $release) {
@ -149,7 +149,7 @@ class Downloader
public static function getFromFileList(string $name, array $source): array
{
logger()->debug("finding {$name} source from file list");
$page = self::curlExec($source['url'], retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0));
$page = self::curlExec($source['url'], retry: self::getRetryTime());
preg_match_all($source['regex'], $page, $matches);
if (!$matches) {
throw new DownloaderException("Failed to get {$name} version");
@ -194,7 +194,7 @@ class Downloader
}
};
self::registerCancelEvent($cancel_func);
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0));
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: self::getRetryTime());
self::unregisterCancelEvent();
logger()->debug("Locking {$filename}");
self::lockSource($name, ['source_type' => 'archive', 'filename' => $filename, 'move_path' => $move_path, 'lock_as' => $lock_as]);
@ -347,7 +347,7 @@ class Downloader
$pkg['url'],
$pkg['rev'],
$pkg['extract'] ?? null,
intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0),
self::getRetryTime(),
SPC_LOCK_PRE_BUILT
);
break;
@ -451,7 +451,7 @@ class Downloader
$source['url'],
$source['rev'],
$source['path'] ?? null,
intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0),
self::getRetryTime(),
$lock_as
);
break;
@ -567,7 +567,7 @@ class Downloader
}
if ($retry > 0) {
logger()->notice('Retrying curl download ...');
self::curlDown($url, $path, $method, $used_headers, retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0));
self::curlDown($url, $path, $method, $used_headers, retry: $retry - 1);
return;
}
throw $e;
@ -601,4 +601,9 @@ class Downloader
pcntl_signal(2, SIG_IGN);
}
}
private static function getRetryTime(): int
{
return intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0);
}
}