Skip to content

Commit 637ae89

Browse files
authored
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
2 parents 57097d1 + 7fd858b commit 637ae89

File tree

4 files changed

+50
-62
lines changed

4 files changed

+50
-62
lines changed

src/SPC/command/DownloadCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function handle(): int
134134

135135
// retry
136136
$retry = intval($this->getOption('retry'));
137-
f_putenv('SPC_RETRY_TIME=' . $retry);
137+
f_putenv('SPC_DOWNLOAD_RETRIES=' . $retry);
138138

139139
// Use shallow-clone can reduce git resource download
140140
if ($this->getOption('shallow-clone')) {

src/SPC/command/SwitchPhpVersionCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function handle(): int
5858

5959
// retry
6060
$retry = intval($this->getOption('retry'));
61-
f_putenv('SPC_RETRY_TIME=' . $retry);
61+
f_putenv('SPC_DOWNLOAD_RETRIES=' . $retry);
6262

6363
Downloader::downloadSource('php-src', Config::getSource('php-src'));
6464

src/SPC/store/Downloader.php

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function getLatestBitbucketTag(string $name, array $source): array
2929
logger()->debug("finding {$name} source from bitbucket tag");
3030
$data = json_decode(self::curlExec(
3131
url: "https://api.bitbucket.org/2.0/repositories/{$source['repo']}/refs/tags",
32-
retry: self::getRetryTime()
32+
retries: self::getRetryAttempts()
3333
), true);
3434
$ver = $data['values'][0]['name'];
3535
if (!$ver) {
@@ -39,7 +39,7 @@ public static function getLatestBitbucketTag(string $name, array $source): array
3939
$headers = self::curlExec(
4040
url: $url,
4141
method: 'HEAD',
42-
retry: self::getRetryTime()
42+
retries: self::getRetryAttempts()
4343
);
4444
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
4545
if ($matches) {
@@ -67,7 +67,7 @@ public static function getLatestGithubTarball(string $name, array $source, strin
6767
$data = json_decode(self::curlExec(
6868
url: "https://api.github.com/repos/{$source['repo']}/{$type}",
6969
hooks: [[CurlHook::class, 'setupGithubToken']],
70-
retry: self::getRetryTime()
70+
retries: self::getRetryAttempts()
7171
), true);
7272

7373
$url = null;
@@ -91,7 +91,7 @@ public static function getLatestGithubTarball(string $name, array $source, strin
9191
url: $url,
9292
method: 'HEAD',
9393
hooks: [[CurlHook::class, 'setupGithubToken']],
94-
retry: self::getRetryTime()
94+
retries: self::getRetryAttempts()
9595
);
9696
preg_match('/^content-disposition:\s+attachment;\s*filename=("?)(?<filename>.+\.tar\.gz)\1/im', $headers, $matches);
9797
if ($matches) {
@@ -118,7 +118,7 @@ public static function getLatestGithubRelease(string $name, array $source, bool
118118
$data = json_decode(self::curlExec(
119119
url: "https://api.github.com/repos/{$source['repo']}/releases",
120120
hooks: [[CurlHook::class, 'setupGithubToken']],
121-
retry: self::getRetryTime()
121+
retries: self::getRetryAttempts()
122122
), true);
123123
$url = null;
124124
foreach ($data as $release) {
@@ -156,7 +156,7 @@ public static function getLatestGithubRelease(string $name, array $source, bool
156156
public static function getFromFileList(string $name, array $source): array
157157
{
158158
logger()->debug("finding {$name} source from file list");
159-
$page = self::curlExec($source['url'], retry: self::getRetryTime());
159+
$page = self::curlExec($source['url'], retries: self::getRetryAttempts());
160160
preg_match_all($source['regex'], $page, $matches);
161161
if (!$matches) {
162162
throw new DownloaderException("Failed to get {$name} version");
@@ -201,7 +201,7 @@ public static function downloadFile(string $name, string $url, string $filename,
201201
}
202202
};
203203
self::registerCancelEvent($cancel_func);
204-
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retry: self::getRetryTime());
204+
self::curlDown(url: $url, path: FileSystem::convertPath(DOWNLOAD_PATH . "/{$filename}"), retries: self::getRetryAttempts());
205205
self::unregisterCancelEvent();
206206
logger()->debug("Locking {$filename}");
207207
if ($download_as === SPC_DOWNLOAD_PRE_BUILT) {
@@ -241,7 +241,7 @@ public static function lockSource(string $name, array $data): void
241241
* @throws RuntimeException
242242
* @throws WrongUsageException
243243
*/
244-
public static function downloadGit(string $name, string $url, string $branch, ?string $move_path = null, int $retry = 0, int $lock_as = SPC_DOWNLOAD_SOURCE): void
244+
public static function downloadGit(string $name, string $url, string $branch, ?string $move_path = null, int $retries = 0, int $lock_as = SPC_DOWNLOAD_SOURCE): void
245245
{
246246
$download_path = FileSystem::convertPath(DOWNLOAD_PATH . "/{$name}");
247247
if (file_exists($download_path)) {
@@ -267,8 +267,8 @@ public static function downloadGit(string $name, string $url, string $branch, ?s
267267
if ($e->getCode() === 2 || $e->getCode() === -1073741510) {
268268
throw new WrongUsageException('Keyboard interrupted, download failed !');
269269
}
270-
if ($retry > 0) {
271-
self::downloadGit($name, $url, $branch, $move_path, $retry - 1);
270+
if ($retries > 0) {
271+
self::downloadGit($name, $url, $branch, $move_path, $retries - 1);
272272
return;
273273
}
274274
throw $e;
@@ -366,7 +366,7 @@ public static function downloadPackage(string $name, ?array $pkg = null, bool $f
366366
$pkg['url'],
367367
$pkg['rev'],
368368
$pkg['extract'] ?? null,
369-
self::getRetryTime(),
369+
self::getRetryAttempts(),
370370
SPC_DOWNLOAD_PRE_BUILT
371371
);
372372
break;
@@ -472,7 +472,7 @@ public static function downloadSource(string $name, ?array $source = null, bool
472472
$source['url'],
473473
$source['rev'],
474474
$source['path'] ?? null,
475-
self::getRetryTime(),
475+
self::getRetryAttempts(),
476476
$download_as
477477
);
478478
break;
@@ -504,42 +504,29 @@ public static function downloadSource(string $name, ?array $source = null, bool
504504
*
505505
* @throws DownloaderException
506506
*/
507-
public static function curlExec(string $url, string $method = 'GET', array $headers = [], array $hooks = [], int $retry = 0): string
507+
public static function curlExec(string $url, string $method = 'GET', array $headers = [], array $hooks = [], int $retries = 0): string
508508
{
509509
foreach ($hooks as $hook) {
510510
$hook($method, $url, $headers);
511511
}
512512

513-
try {
514-
FileSystem::findCommandPath('curl');
515-
516-
$methodArg = match ($method) {
517-
'GET' => '',
518-
'HEAD' => '-I',
519-
default => "-X \"{$method}\"",
520-
};
521-
$headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $headers));
522-
523-
$cmd = SPC_CURL_EXEC . " -sfSL {$methodArg} {$headerArg} \"{$url}\"";
524-
if (getenv('CACHE_API_EXEC') === 'yes') {
525-
if (!file_exists(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'))) {
526-
$cache = [];
527-
} else {
528-
$cache = json_decode(file_get_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache')), true);
529-
}
530-
if (isset($cache[$cmd]) && $cache[$cmd]['expire'] >= time()) {
531-
return $cache[$cmd]['cache'];
532-
}
533-
f_exec($cmd, $output, $ret);
534-
if ($ret === 2 || $ret === -1073741510) {
535-
throw new RuntimeException('failed http fetch');
536-
}
537-
if ($ret !== 0) {
538-
throw new DownloaderException('failed http fetch');
539-
}
540-
$cache[$cmd]['cache'] = implode("\n", $output);
541-
$cache[$cmd]['expire'] = time() + 3600;
542-
file_put_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'), json_encode($cache));
513+
FileSystem::findCommandPath('curl');
514+
515+
$methodArg = match ($method) {
516+
'GET' => '',
517+
'HEAD' => '-I',
518+
default => "-X \"{$method}\"",
519+
};
520+
$headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $headers));
521+
$retry = $retries > 0 ? "--retry {$retries}" : '';
522+
$cmd = SPC_CURL_EXEC . " -sfSL {$retry} {$methodArg} {$headerArg} \"{$url}\"";
523+
if (getenv('CACHE_API_EXEC') === 'yes') {
524+
if (!file_exists(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'))) {
525+
$cache = [];
526+
} else {
527+
$cache = json_decode(file_get_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache')), true);
528+
}
529+
if (isset($cache[$cmd]) && $cache[$cmd]['expire'] >= time()) {
543530
return $cache[$cmd]['cache'];
544531
}
545532
f_exec($cmd, $output, $ret);
@@ -549,14 +536,19 @@ public static function curlExec(string $url, string $method = 'GET', array $head
549536
if ($ret !== 0) {
550537
throw new DownloaderException('failed http fetch');
551538
}
552-
return implode("\n", $output);
553-
} catch (DownloaderException $e) {
554-
if ($retry > 0) {
555-
logger()->notice('Retrying curl exec ...');
556-
return self::curlExec($url, $method, $headers, $hooks, $retry - 1);
557-
}
558-
throw $e;
539+
$cache[$cmd]['cache'] = implode("\n", $output);
540+
$cache[$cmd]['expire'] = time() + 3600;
541+
file_put_contents(FileSystem::convertPath(DOWNLOAD_PATH . '/.curl_exec_cache'), json_encode($cache));
542+
return $cache[$cmd]['cache'];
559543
}
544+
f_exec($cmd, $output, $ret);
545+
if ($ret === 2 || $ret === -1073741510) {
546+
throw new RuntimeException('failed http fetch');
547+
}
548+
if ($ret !== 0) {
549+
throw new DownloaderException('failed http fetch');
550+
}
551+
return implode("\n", $output);
560552
}
561553

562554
/**
@@ -565,7 +557,7 @@ public static function curlExec(string $url, string $method = 'GET', array $head
565557
* @throws RuntimeException
566558
* @throws WrongUsageException
567559
*/
568-
public static function curlDown(string $url, string $path, string $method = 'GET', array $headers = [], array $hooks = [], int $retry = 0): void
560+
public static function curlDown(string $url, string $path, string $method = 'GET', array $headers = [], array $hooks = [], int $retries = 0): void
569561
{
570562
$used_headers = $headers;
571563
foreach ($hooks as $hook) {
@@ -579,18 +571,14 @@ public static function curlDown(string $url, string $path, string $method = 'GET
579571
};
580572
$headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $used_headers));
581573
$check = !defined('DEBUG_MODE') ? 's' : '#';
582-
$cmd = SPC_CURL_EXEC . " -{$check}fSL -o \"{$path}\" {$methodArg} {$headerArg} \"{$url}\"";
574+
$retry = $retries > 0 ? "--retry {$retries}" : '';
575+
$cmd = SPC_CURL_EXEC . " -{$check}fSL {$retry} -o \"{$path}\" {$methodArg} {$headerArg} \"{$url}\"";
583576
try {
584577
f_passthru($cmd);
585578
} catch (RuntimeException $e) {
586579
if ($e->getCode() === 2 || $e->getCode() === -1073741510) {
587580
throw new WrongUsageException('Keyboard interrupted, download failed !');
588581
}
589-
if ($retry > 0) {
590-
logger()->notice('Retrying curl download ...');
591-
self::curlDown($url, $path, $method, $used_headers, retry: $retry - 1);
592-
return;
593-
}
594582
throw $e;
595583
}
596584
}
@@ -628,9 +616,9 @@ private static function unregisterCancelEvent(): void
628616
}
629617
}
630618

631-
private static function getRetryTime(): int
619+
private static function getRetryAttempts(): int
632620
{
633-
return intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0);
621+
return intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0);
634622
}
635623

636624
/**

src/SPC/store/source/PhpSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getLatestPHPInfo(string $major_version): array
3434
// 查找最新的小版本号
3535
$info = json_decode(Downloader::curlExec(
3636
url: "https://www.php.net/releases/index.php?json&version={$major_version}",
37-
retry: intval(getenv('SPC_RETRY_TIME') ? getenv('SPC_RETRY_TIME') : 0)
37+
retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0)
3838
), true);
3939
if (!isset($info['version'])) {
4040
throw new DownloaderException("Version {$major_version} not found.");

0 commit comments

Comments
 (0)