Skip to content

Commit 3c4d47d

Browse files
authored
feat: improve downloader retry (#558)
- Refactored to remove duplicate retry expressions by utilizing the getRetryTime() method. - Fixed a typo in the log message.
1 parent 0fb7784 commit 3c4d47d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/SPC/store/Downloader.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function getLatestBitbucketTag(string $name, array $source): array
2828
logger()->debug("finding {$name} source from bitbucket tag");
2929
$data = json_decode(self::curlExec(
3030
url: "https://api.bitbucket.org/2.0/repositories/{$source['repo']}/refs/tags",
31-
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
31+
retry: self::getRetryTime()
3232
), true);
3333
$ver = $data['values'][0]['name'];
3434
if (!$ver) {
@@ -38,7 +38,7 @@ public static function getLatestBitbucketTag(string $name, array $source): array
3838
$headers = self::curlExec(
3939
url: $url,
4040
method: 'HEAD',
41-
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
41+
retry: self::getRetryTime()
4242
);
4343
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
4444
if ($matches) {
@@ -66,7 +66,7 @@ public static function getLatestGithubTarball(string $name, array $source, strin
6666
$data = json_decode(self::curlExec(
6767
url: "https://api.github.com/repos/{$source['repo']}/{$type}",
6868
hooks: [[CurlHook::class, 'setupGithubToken']],
69-
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
69+
retry: self::getRetryTime()
7070
), true);
7171

7272
if (($source['prefer-stable'] ?? false) === false) {
@@ -85,7 +85,7 @@ public static function getLatestGithubTarball(string $name, array $source, strin
8585
url: $url,
8686
method: 'HEAD',
8787
hooks: [[CurlHook::class, 'setupGithubToken']],
88-
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
88+
retry: self::getRetryTime()
8989
);
9090
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
9191
if ($matches) {
@@ -108,11 +108,11 @@ public static function getLatestGithubTarball(string $name, array $source, strin
108108
*/
109109
public static function getLatestGithubRelease(string $name, array $source, bool $match_result = true): array
110110
{
111-
logger()->debug("finding {$name} from github releases assests");
111+
logger()->debug("finding {$name} from github releases assets");
112112
$data = json_decode(self::curlExec(
113113
url: "https://api.github.com/repos/{$source['repo']}/releases",
114114
hooks: [[CurlHook::class, 'setupGithubToken']],
115-
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
115+
retry: self::getRetryTime()
116116
), true);
117117
$url = null;
118118
foreach ($data as $release) {
@@ -149,7 +149,7 @@ public static function getLatestGithubRelease(string $name, array $source, bool
149149
public static function getFromFileList(string $name, array $source): array
150150
{
151151
logger()->debug("finding {$name} source from file list");
152-
$page = self::curlExec($source['url'], retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0));
152+
$page = self::curlExec($source['url'], retry: self::getRetryTime());
153153
preg_match_all($source['regex'], $page, $matches);
154154
if (!$matches) {
155155
throw new DownloaderException("Failed to get {$name} version");
@@ -194,7 +194,7 @@ public static function downloadFile(string $name, string $url, string $filename,
194194
}
195195
};
196196
self::registerCancelEvent($cancel_func);
197-
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0));
197+
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: self::getRetryTime());
198198
self::unregisterCancelEvent();
199199
logger()->debug("Locking {$filename}");
200200
self::lockSource($name, ['source_type' => 'archive', 'filename' => $filename, 'move_path' => $move_path, 'lock_as' => $lock_as]);
@@ -347,7 +347,7 @@ public static function downloadPackage(string $name, ?array $pkg = null, bool $f
347347
$pkg['url'],
348348
$pkg['rev'],
349349
$pkg['extract'] ?? null,
350-
intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0),
350+
self::getRetryTime(),
351351
SPC_LOCK_PRE_BUILT
352352
);
353353
break;
@@ -451,7 +451,7 @@ public static function downloadSource(string $name, ?array $source = null, bool
451451
$source['url'],
452452
$source['rev'],
453453
$source['path'] ?? null,
454-
intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0),
454+
self::getRetryTime(),
455455
$lock_as
456456
);
457457
break;
@@ -567,7 +567,7 @@ public static function curlDown(string $url, string $path, string $method = 'GET
567567
}
568568
if ($retry > 0) {
569569
logger()->notice('Retrying curl download ...');
570-
self::curlDown($url, $path, $method, $used_headers, retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0));
570+
self::curlDown($url, $path, $method, $used_headers, retry: $retry - 1);
571571
return;
572572
}
573573
throw $e;
@@ -601,4 +601,9 @@ private static function unregisterCancelEvent(): void
601601
pcntl_signal(2, SIG_IGN);
602602
}
603603
}
604+
605+
private static function getRetryTime(): int
606+
{
607+
return intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0);
608+
}
604609
}

0 commit comments

Comments
 (0)