Merge pull request #707 from crazywhalecc/retries

rename SPC_RETRY_TIME to SPC_DOWNLOAD_RETRIES to clear up what it's doing sleep 5 seconds between retries
This commit is contained in:
Marc 2025-04-22 15:45:02 +07:00 committed by GitHub
commit 637ae89583
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 61 deletions

View File

@ -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')) {

View File

@ -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'));

View File

@ -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=("?)(?<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=("?)(?<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}"), 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;
@ -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,42 +504,29 @@ 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);
}
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,14 +536,19 @@ class Downloader
if ($ret !== 0) {
throw new DownloaderException('failed http fetch');
}
return implode("\n", $output);
} catch (DownloaderException $e) {
if ($retry > 0) {
logger()->notice('Retrying curl exec ...');
return self::curlExec($url, $method, $headers, $hooks, $retry - 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);
}
/**
@ -565,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) {
@ -579,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;
}
}
@ -628,9 +616,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);
}
/**

View File

@ -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.");