diff --git a/src/SPC/command/DownloadCommand.php b/src/SPC/command/DownloadCommand.php index b31db2d5..c49c7808 100644 --- a/src/SPC/command/DownloadCommand.php +++ b/src/SPC/command/DownloadCommand.php @@ -134,7 +134,7 @@ class DownloadCommand extends BaseCommand // retry $retry = intval($this->getOption('retry')); - f_putenv('SPC_RETRY_TIME=' . $retry); + f_putenv('SPC_DOWNLOAD_RETRIES=' . $retry); // Use shallow-clone can reduce git resource download if ($this->getOption('shallow-clone')) { diff --git a/src/SPC/command/SwitchPhpVersionCommand.php b/src/SPC/command/SwitchPhpVersionCommand.php index ab91ed07..ef463ef4 100644 --- a/src/SPC/command/SwitchPhpVersionCommand.php +++ b/src/SPC/command/SwitchPhpVersionCommand.php @@ -58,7 +58,7 @@ class SwitchPhpVersionCommand extends BaseCommand // retry $retry = intval($this->getOption('retry')); - f_putenv('SPC_RETRY_TIME=' . $retry); + f_putenv('SPC_DOWNLOAD_RETRIES=' . $retry); Downloader::downloadSource('php-src', Config::getSource('php-src')); diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index 7a857c48..f7fbd43b 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -29,7 +29,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: self::getRetryTime() + retries: self::getRetryAttempts() ), true); $ver = $data['values'][0]['name']; if (!$ver) { @@ -39,7 +39,7 @@ class Downloader $headers = self::curlExec( url: $url, method: 'HEAD', - retry: self::getRetryTime() + retries: self::getRetryAttempts() ); preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?.+\.tar\.gz)\1/im', $headers, $matches); if ($matches) { @@ -67,7 +67,7 @@ class Downloader $data = json_decode(self::curlExec( url: "https://api.github.com/repos/{$source['repo']}/{$type}", hooks: [[CurlHook::class, 'setupGithubToken']], - retry: self::getRetryTime() + retries: self::getRetryAttempts() ), true); $url = null; @@ -91,7 +91,7 @@ class Downloader url: $url, method: 'HEAD', hooks: [[CurlHook::class, 'setupGithubToken']], - retry: self::getRetryTime() + retries: self::getRetryAttempts() ); preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?.+\.tar\.gz)\1/im', $headers, $matches); if ($matches) { @@ -118,7 +118,7 @@ class Downloader $data = json_decode(self::curlExec( url: "https://api.github.com/repos/{$source['repo']}/releases", hooks: [[CurlHook::class, 'setupGithubToken']], - retry: self::getRetryTime() + retries: self::getRetryAttempts() ), true); $url = null; foreach ($data as $release) { @@ -156,7 +156,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: self::getRetryTime()); + $page = self::curlExec($source['url'], retries: self::getRetryAttempts()); preg_match_all($source['regex'], $page, $matches); if (!$matches) { throw new DownloaderException("Failed to get {$name} version"); @@ -201,7 +201,7 @@ class Downloader } }; self::registerCancelEvent($cancel_func); - self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: self::getRetryTime()); + self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: self::getRetryAttempts()); self::unregisterCancelEvent(); logger()->debug("Locking {$filename}"); if ($download_as === SPC_DOWNLOAD_PRE_BUILT) { @@ -366,7 +366,7 @@ class Downloader $pkg['url'], $pkg['rev'], $pkg['extract'] ?? null, - self::getRetryTime(), + self::getRetryAttempts(), SPC_DOWNLOAD_PRE_BUILT ); break; @@ -472,7 +472,7 @@ class Downloader $source['url'], $source['rev'], $source['path'] ?? null, - self::getRetryTime(), + self::getRetryAttempts(), $download_as ); break; @@ -504,7 +504,7 @@ class Downloader * * @throws DownloaderException */ - public static function curlExec(string $url, string $method = 'GET', array $headers = [], array $hooks = [], int $retry = 0): string + public static function curlExec(string $url, string $method = 'GET', array $headers = [], array $hooks = [], int $retries = 0): string { foreach ($hooks as $hook) { $hook($method, $url, $headers); @@ -551,9 +551,10 @@ class Downloader } return implode("\n", $output); } catch (DownloaderException $e) { - if ($retry > 0) { - logger()->notice('Retrying curl exec ...'); - return self::curlExec($url, $method, $headers, $hooks, $retry - 1); + if ($retries > 0) { + logger()->notice('Retrying curl exec after 5 seconds...'); + sleep(5); + return self::curlExec($url, $method, $headers, $hooks, $retries - 1); } throw $e; } @@ -628,9 +629,9 @@ class Downloader } } - private static function getRetryTime(): int + private static function getRetryAttempts(): int { - return intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0); + return intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0); } /** diff --git a/src/SPC/store/source/PhpSource.php b/src/SPC/store/source/PhpSource.php index d2617e3b..5b4fd097 100644 --- a/src/SPC/store/source/PhpSource.php +++ b/src/SPC/store/source/PhpSource.php @@ -34,7 +34,7 @@ class PhpSource extends CustomSourceBase // 查找最新的小版本号 $info = json_decode(Downloader::curlExec( url: "https://www.php.net/releases/index.php?json&version={$major_version}", - retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0) + retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0) ), true); if (!isset($info['version'])) { throw new DownloaderException("Version {$major_version} not found.");