Skip to content

Commit c4e21b7

Browse files
feat: Implement retries for downloading binaries in lower versions
1 parent 6cf8905 commit c4e21b7

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.3
1+
0.4.4

src/Utils/LibsChecker.php

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,64 @@ private static function install(string $libsDir): void
5151
default => 'tar.gz',
5252
};
5353

54-
$baseUrl = "https://github.com/CodeWithKyrian/transformers-php/releases/download/$version";
55-
$downloadFile = "transformersphp-$version-$os-$arch.$extension";
56-
$downloadUrl = "$baseUrl/$downloadFile";
57-
$downloadPath = tempnam(sys_get_temp_dir(), 'transformers-php').".$extension";
58-
59-
echo " - Downloading ".self::colorize("transformersphp-$version-$os-$arch")."\n";
60-
Downloader::download($downloadUrl, $downloadPath);
61-
echo " - Installing ".self::colorize($downloadFile)." : Extracting archive\n";
62-
63-
$archive = new \PharData($downloadPath);
64-
if ($extension != 'zip') {
65-
$archive = $archive->decompress();
66-
}
54+
$maxRetries = 10;
55+
$attempts = 0;
56+
57+
do {
58+
$baseUrl = "https://github.com/CodeWithKyrian/transformers-php/releases/download/$version";
59+
$downloadFile = "transformersphp-$version-$os-$arch.$extension";
60+
$downloadUrl = "$baseUrl/$downloadFile";
61+
$downloadPath = tempnam(sys_get_temp_dir(), 'transformers-php').".$extension";
62+
63+
echo " - Downloading ".self::colorize("transformersphp-$version-$os-$arch")."\n";
64+
65+
$downloadSuccess = false;
66+
67+
try {
68+
$downloadSuccess = Downloader::download($downloadUrl, $downloadPath);
69+
} catch (\Exception) {
70+
}
71+
72+
if ($downloadSuccess) {
73+
echo " - Installing ".self::colorize("transformersphp-$version-$os-$arch")." : Extracting archive\n";
74+
75+
$archive = new \PharData($downloadPath);
76+
if ($extension != 'zip') {
77+
$archive = $archive->decompress();
78+
}
6779

68-
$archive->extractTo($libsDir, overwrite: true);
80+
$archive->extractTo($libsDir, overwrite: true);
81+
@unlink($downloadPath);
6982

70-
@unlink($downloadPath);
83+
echo "TransformersPHP libraries installed\n";
84+
return;
85+
} else {
86+
echo " - Failed to download transformersphp-$version-$os-$arch, trying a lower version...\n";
87+
$version = self::getLowerVersion($version);
88+
}
89+
90+
$attempts++;
91+
} while ($version !== null && $attempts < $maxRetries);
92+
93+
throw new \Exception("Could not find the required binaries after $maxRetries attempts.");
94+
}
95+
96+
private static function getLowerVersion(string $version): ?string
97+
{
98+
$parts = explode('.', $version);
99+
100+
if (count($parts) === 3 && $parts[2] > 0) {
101+
$parts[2]--;
102+
} elseif (count($parts) === 3) {
103+
$parts[1]--;
104+
$parts[2] = 9; // Reset patch version
105+
} elseif (count($parts) === 2 && $parts[1] > 0) {
106+
$parts[1]--;
107+
} else {
108+
return null; // No lower version possible
109+
}
71110

72-
echo "TransformersPHP libraries installed\n";
111+
return implode('.', $parts);
73112
}
74113

75114
private static function colorize(string $text, string $color = 'green'): string

0 commit comments

Comments
 (0)