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

View File

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