Only retry fetching app store data once every 5 minutes in case it fails

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2020-10-14 16:58:52 +02:00
parent b516a58acf
commit 5aef556016
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
2 changed files with 52 additions and 76 deletions

View File

@ -42,6 +42,7 @@ use OCP\ILogger;
abstract class Fetcher { abstract class Fetcher {
public const INVALIDATE_AFTER_SECONDS = 3600; public const INVALIDATE_AFTER_SECONDS = 3600;
public const RETRY_AFTER_FAILURE_SECONDS = 300;
/** @var IAppData */ /** @var IAppData */
protected $appData; protected $appData;
@ -91,6 +92,9 @@ abstract class Fetcher {
*/ */
protected function fetch($ETag, $content) { protected function fetch($ETag, $content) {
$appstoreenabled = $this->config->getSystemValue('appstoreenabled', true); $appstoreenabled = $this->config->getSystemValue('appstoreenabled', true);
if ((int)$this->config->getAppValue('settings', 'appstore-fetcher-lastFailure', '0') > time() - self::RETRY_AFTER_FAILURE_SECONDS) {
return [];
}
if (!$appstoreenabled) { if (!$appstoreenabled) {
return []; return [];
@ -106,7 +110,12 @@ abstract class Fetcher {
} }
$client = $this->clientService->newClient(); $client = $this->clientService->newClient();
$response = $client->get($this->getEndpoint(), $options); try {
$response = $client->get($this->getEndpoint(), $options);
} catch (ConnectException $e) {
$this->config->setAppValue('settings', 'appstore-fetcher-lastFailure', (string)time());
throw $e;
}
$responseJson = []; $responseJson = [];
if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
@ -115,6 +124,7 @@ abstract class Fetcher {
$responseJson['data'] = json_decode($response->getBody(), true); $responseJson['data'] = json_decode($response->getBody(), true);
$ETag = $response->getHeader('ETag'); $ETag = $response->getHeader('ETag');
} }
$this->config->deleteAppValue('settings', 'appstore-fetcher-lastFailure');
$responseJson['timestamp'] = $this->timeFactory->getTime(); $responseJson['timestamp'] = $this->timeFactory->getTime();
$responseJson['ncversion'] = $this->getVersion(); $responseJson['ncversion'] = $this->getVersion();
@ -171,6 +181,11 @@ abstract class Fetcher {
// Refresh the file content // Refresh the file content
try { try {
$responseJson = $this->fetch($ETag, $content); $responseJson = $this->fetch($ETag, $content);
if (empty($responseJson)) {
return [];
}
$file->putContent(json_encode($responseJson)); $file->putContent(json_encode($responseJson));
return json_decode($file->getContent(), true)['data']; return json_decode($file->getContent(), true)['data'];
} catch (ConnectException $e) { } catch (ConnectException $e) {

View File

@ -120,32 +120,19 @@ abstract class FetcherBase extends TestCase {
public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion() { public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion() {
$this->config $this->config
->expects($this->at(0))
->method('getSystemValue') ->method('getSystemValue')
->with('appstoreenabled', true) ->willReturnCallback(function ($var, $default) {
->willReturn(true); if ($var === 'appstoreenabled') {
$this->config return true;
->expects($this->at(1)) } elseif ($var === 'has_internet_connection') {
->method('getSystemValue') return true;
->with('has_internet_connection', true) } elseif ($var === 'appstoreurl') {
->willReturn(true); return 'https://apps.nextcloud.com/api/v1';
$this->config } elseif ($var === 'version') {
->expects($this->at(2)) return '11.0.0.2';
->method('getSystemValue') }
->with('appstoreenabled', true) return $default;
->willReturn(true); });
$this->config
->expects($this->at(3))
->method('getSystemValue')
->with('appstoreurl', 'https://apps.nextcloud.com/api/v1')
->willReturn('https://apps.nextcloud.com/api/v1');
$this->config
->expects($this->at(4))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class); $folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);
@ -294,32 +281,19 @@ abstract class FetcherBase extends TestCase {
public function testGetWithAlreadyExistingFileAndNoVersion() { public function testGetWithAlreadyExistingFileAndNoVersion() {
$this->config $this->config
->expects($this->at(0))
->method('getSystemValue') ->method('getSystemValue')
->with('appstoreenabled', true) ->willReturnCallback(function ($var, $default) {
->willReturn(true); if ($var === 'appstoreenabled') {
$this->config return true;
->expects($this->at(1)) } elseif ($var === 'has_internet_connection') {
->method('getSystemValue') return true;
->with('has_internet_connection', true) } elseif ($var === 'appstoreurl') {
->willReturn(true); return 'https://apps.nextcloud.com/api/v1';
$this->config } elseif ($var === 'version') {
->expects($this->at(2)) return '11.0.0.2';
->method('getSystemValue') }
->with('appstoreenabled', true) return $default;
->willReturn(true); });
$this->config
->expects($this->at(3))
->method('getSystemValue')
->with('appstoreurl', 'https://apps.nextcloud.com/api/v1')
->willReturn('https://apps.nextcloud.com/api/v1');
$this->config
->expects($this->at(4))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class); $folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);
@ -391,32 +365,19 @@ abstract class FetcherBase extends TestCase {
public function testGetWithAlreadyExistingFileAndOutdatedVersion() { public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
$this->config $this->config
->expects($this->at(0))
->method('getSystemValue') ->method('getSystemValue')
->with('appstoreenabled', true) ->willReturnCallback(function ($var, $default) {
->willReturn(true); if ($var === 'appstoreenabled') {
$this->config return true;
->expects($this->at(1)) } elseif ($var === 'has_internet_connection') {
->method('getSystemValue') return true;
->with('has_internet_connection', true) } elseif ($var === 'appstoreurl') {
->willReturn(true); return 'https://apps.nextcloud.com/api/v1';
$this->config } elseif ($var === 'version') {
->expects($this->at(2)) return '11.0.0.2';
->method('getSystemValue') }
->with('appstoreenabled', true) return $default;
->willReturn(true); });
$this->config
->expects($this->at(3))
->method('getSystemValue')
->with('appstoreurl', 'https://apps.nextcloud.com/api/v1')
->willReturn('https://apps.nextcloud.com/api/v1');
$this->config
->expects($this->at(4))
->method('getSystemValue')
->with(
$this->equalTo('version'),
$this->anything()
)->willReturn('11.0.0.2');
$folder = $this->createMock(ISimpleFolder::class); $folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);