From 3c4d47d0729ed767f1be9c647859cb42313926b6 Mon Sep 17 00:00:00 2001 From: yinheli Date: Sat, 9 Nov 2024 10:50:08 +0800 Subject: [PATCH] feat: improve downloader retry (#558) - Refactored to remove duplicate retry expressions by utilizing the getRetryTime() method. - Fixed a typo in the log message. --- src/SPC/store/Downloader.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index f01519bf..16edeeae 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -28,7 +28,7 @@ public static function getLatestBitbucketTag(string $name, array $source): array 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 @@ public static function getLatestBitbucketTag(string $name, array $source): array $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=("?)(?.+\.tar\.gz)\1/im', $headers, $matches); if ($matches) { @@ -66,7 +66,7 @@ public static function getLatestGithubTarball(string $name, array $source, strin $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 @@ public static function getLatestGithubTarball(string $name, array $source, strin 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=("?)(?.+\.tar\.gz)\1/im', $headers, $matches); if ($matches) { @@ -108,11 +108,11 @@ public static function getLatestGithubTarball(string $name, array $source, strin */ 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 @@ public static function getLatestGithubRelease(string $name, array $source, bool 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 @@ public static function downloadFile(string $name, string $url, string $filename, } }; 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 @@ public static function downloadPackage(string $name, ?array $pkg = null, bool $f $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 @@ public static function downloadSource(string $name, ?array $source = null, bool $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 @@ public static function curlDown(string $url, string $path, string $method = 'GET } 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 @@ private static function unregisterCancelEvent(): void pcntl_signal(2, SIG_IGN); } } + + private static function getRetryTime(): int + { + return intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0); + } }