Skip to content

Commit

Permalink
feat: improve downloader retry (#558)
Browse files Browse the repository at this point in the history
- Refactored to remove duplicate retry expressions by utilizing the getRetryTime() method.
- Fixed a typo in the log message.
  • Loading branch information
yinheli authored Nov 9, 2024
1 parent 0fb7784 commit 3c4d47d
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/SPC/store/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
if ($matches) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
if ($matches) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 3c4d47d

Please sign in to comment.