Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve downloader retry #558

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This recursive call doesn't seem right. So I updated.

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);
}
}