Skip to content

Commit

Permalink
Added automatic retry for empty app lists in GetAppList.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Apr 16, 2020
1 parent 38e30aa commit 987d7d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Resource/FetchAppListException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace ScriptFUSION\Porter\Provider\Steam\Resource;

/**
* The exception that is thrown when a permanent error occurs trying to fetch the app list.
*/
final class FetchAppListException extends \RuntimeException
{
// Intentionally empty.
}
23 changes: 19 additions & 4 deletions src/Resource/GetAppList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,25 @@ public function getProviderClassName(): string

public function fetch(ImportConnector $connector): \Iterator
{
return new \ArrayIterator(\json_decode(
(string)$connector->fetch(new HttpDataSource(self::getUrl())),
true
)['applist']['apps']);
$tries = -1;

retry:
if (++$tries === 10) {
throw new FetchAppListException("Could not fetch app list after $tries attempts.");
}

$json = \json_decode((string)$connector->fetch(new HttpDataSource(self::getUrl())), true);

if (isset($json['applist']['apps'])) {
$apps = $json['applist']['apps'];
}

// App list is empty about 20% of the time, seemingly more often on CI, so counting is important.
if ($json === null || !isset($apps) || !count($apps)) {
goto retry;
}

return new \ArrayIterator($apps);
}

public static function getUrl(): string
Expand Down

0 comments on commit 987d7d0

Please sign in to comment.