Skip to content

Commit a29992b

Browse files
committed
rename retry to retries
1 parent 0b3421e commit a29992b

1 file changed

Lines changed: 36 additions & 49 deletions

File tree

src/SPC/store/Downloader.php

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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::getRetryAttempts());
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;
@@ -510,36 +510,23 @@ public static function curlExec(string $url, string $method = 'GET', array $head
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,15 +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 ($retries > 0) {
555-
logger()->notice('Retrying curl exec after 5 seconds...');
556-
sleep(5);
557-
return self::curlExec($url, $method, $headers, $hooks, $retries - 1);
558-
}
559-
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'];
543+
}
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');
560550
}
551+
return implode("\n", $output);
561552
}
562553

563554
/**
@@ -566,7 +557,7 @@ public static function curlExec(string $url, string $method = 'GET', array $head
566557
* @throws RuntimeException
567558
* @throws WrongUsageException
568559
*/
569-
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
570561
{
571562
$used_headers = $headers;
572563
foreach ($hooks as $hook) {
@@ -580,18 +571,14 @@ public static function curlDown(string $url, string $path, string $method = 'GET
580571
};
581572
$headerArg = implode(' ', array_map(fn ($v) => '"-H' . $v . '"', $used_headers));
582573
$check = !defined('DEBUG_MODE') ? 's' : '#';
583-
$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}\"";
584576
try {
585577
f_passthru($cmd);
586578
} catch (RuntimeException $e) {
587579
if ($e->getCode() === 2 || $e->getCode() === -1073741510) {
588580
throw new WrongUsageException('Keyboard interrupted, download failed !');
589581
}
590-
if ($retry > 0) {
591-
logger()->notice('Retrying curl download ...');
592-
self::curlDown($url, $path, $method, $used_headers, retry: $retry - 1);
593-
return;
594-
}
595582
throw $e;
596583
}
597584
}

0 commit comments

Comments
 (0)