From 0b3421e3cdf3ff863b2bfcdaf154391f674594d6 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Tue, 22 Apr 2025 12:23:47 +0700 Subject: [PATCH 1/4] rename SPC_RETRY_TIME to SPC_DOWNLOAD_RETRIES to clear up what it's doing sleep 5 seconds between retries --- src/SPC/command/DownloadCommand.php | 2 +- src/SPC/command/SwitchPhpVersionCommand.php | 2 +- src/SPC/store/Downloader.php | 31 +++++++++++---------- src/SPC/store/source/PhpSource.php | 2 +- 4 files changed, 19 insertions(+), 18 deletions(-) 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."); From a29992b907b72028c20d59cc1c88c0ae634b4a32 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Tue, 22 Apr 2025 14:34:43 +0700 Subject: [PATCH 2/4] rename retry to retries --- src/SPC/store/Downloader.php | 83 +++++++++++++++--------------------- 1 file changed, 35 insertions(+), 48 deletions(-) diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index f7fbd43b..69c3d322 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -201,7 +201,7 @@ class Downloader } }; 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(); logger()->debug("Locking {$filename}"); if ($download_as === SPC_DOWNLOAD_PRE_BUILT) { @@ -241,7 +241,7 @@ class Downloader * @throws RuntimeException * @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}"); if (file_exists($download_path)) { @@ -267,8 +267,8 @@ class Downloader if ($e->getCode() === 2 || $e->getCode() === -1073741510) { throw new WrongUsageException('Keyboard interrupted, download failed !'); } - if ($retry > 0) { - self::downloadGit($name, $url, $branch, $move_path, $retry - 1); + if ($retries > 0) { + self::downloadGit($name, $url, $branch, $move_path, $retries - 1); return; } throw $e; @@ -510,36 +510,23 @@ class Downloader $hook($method, $url, $headers); } - try { - FileSystem::findCommandPath('curl'); + FileSystem::findCommandPath('curl'); - $methodArg = match ($method) { - 'GET' => '', - 'HEAD' => '-I', - default => "-X \"{$method}\"", - }; - $headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $headers)); - - $cmd = SPC_CURL_EXEC . " -sfSL {$methodArg} {$headerArg} \"{$url}\""; - if (getenv('CACHE_API_EXEC') === 'yes') { - if (!file_exists(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'))) { - $cache = []; - } else { - $cache = json_decode(file_get_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache')), true); - } - 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)); + $methodArg = match ($method) { + 'GET' => '', + 'HEAD' => '-I', + default => "-X \"{$method}\"", + }; + $headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $headers)); + $retry = $retries > 0 ? "--retry {$retries}" : ''; + $cmd = SPC_CURL_EXEC . " -sfSL {$retry} {$methodArg} {$headerArg} \"{$url}\""; + if (getenv('CACHE_API_EXEC') === 'yes') { + if (!file_exists(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'))) { + $cache = []; + } else { + $cache = json_decode(file_get_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache')), true); + } + if (isset($cache[$cmd]) && $cache[$cmd]['expire'] >= time()) { return $cache[$cmd]['cache']; } f_exec($cmd, $output, $ret); @@ -549,15 +536,19 @@ class Downloader if ($ret !== 0) { throw new DownloaderException('failed http fetch'); } - return implode("\n", $output); - } catch (DownloaderException $e) { - if ($retries > 0) { - logger()->notice('Retrying curl exec after 5 seconds...'); - sleep(5); - return self::curlExec($url, $method, $headers, $hooks, $retries - 1); - } - throw $e; + $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']; } + 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 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; foreach ($hooks as $hook) { @@ -580,18 +571,14 @@ class Downloader }; $headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $used_headers)); $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 { f_passthru($cmd); } catch (RuntimeException $e) { if ($e->getCode() === 2 || $e->getCode() === -1073741510) { 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; } } From 74d8377400630ad88a5736b8b595c6c98b456e8a Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Tue, 22 Apr 2025 14:36:58 +0700 Subject: [PATCH 3/4] unwrap cast to unify curlDown with curlExec --- src/SPC/store/Downloader.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index 69c3d322..085c4ed4 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -573,14 +573,7 @@ class Downloader $check = !defined('DEBUG_MODE') ? 's' : '#'; $retry = $retries > 0 ? "--retry {$retries}" : ''; $cmd = SPC_CURL_EXEC . " -{$check}fSL {$retry} -o \"{$path}\" {$methodArg} {$headerArg} \"{$url}\""; - try { - f_passthru($cmd); - } catch (RuntimeException $e) { - if ($e->getCode() === 2 || $e->getCode() === -1073741510) { - throw new WrongUsageException('Keyboard interrupted, download failed !'); - } - throw $e; - } + f_passthru($cmd); } public static function getPreBuiltLockName(string $source): string From 7fd858b47238f31f4db1a721e8f31aa898354bc8 Mon Sep 17 00:00:00 2001 From: DubbleClick Date: Tue, 22 Apr 2025 14:44:57 +0700 Subject: [PATCH 4/4] Revert "unwrap cast to unify curlDown with curlExec" This reverts commit 74d8377400630ad88a5736b8b595c6c98b456e8a. --- src/SPC/store/Downloader.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/SPC/store/Downloader.php b/src/SPC/store/Downloader.php index 085c4ed4..69c3d322 100644 --- a/src/SPC/store/Downloader.php +++ b/src/SPC/store/Downloader.php @@ -573,7 +573,14 @@ class Downloader $check = !defined('DEBUG_MODE') ? 's' : '#'; $retry = $retries > 0 ? "--retry {$retries}" : ''; $cmd = SPC_CURL_EXEC . " -{$check}fSL {$retry} -o \"{$path}\" {$methodArg} {$headerArg} \"{$url}\""; - f_passthru($cmd); + try { + f_passthru($cmd); + } catch (RuntimeException $e) { + if ($e->getCode() === 2 || $e->getCode() === -1073741510) { + throw new WrongUsageException('Keyboard interrupted, download failed !'); + } + throw $e; + } } public static function getPreBuiltLockName(string $source): string