Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Only retry fetching app store data once every 5 minutes in case it fails
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr authored and backportbot[bot] committed Oct 22, 2020
commit 367a7bd87e6ca053cc55cdcc040eff82a53f2ea1
17 changes: 16 additions & 1 deletion lib/private/App/AppStore/Fetcher/Fetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

abstract class Fetcher {
public const INVALIDATE_AFTER_SECONDS = 3600;
public const RETRY_AFTER_FAILURE_SECONDS = 300;

/** @var IAppData */
protected $appData;
Expand Down Expand Up @@ -91,6 +92,9 @@ public function __construct(Factory $appDataFactory,
*/
protected function fetch($ETag, $content) {
$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) {
return [];
Expand All @@ -107,7 +111,12 @@ protected function fetch($ETag, $content) {
}

$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 = [];
if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
Expand All @@ -116,6 +125,7 @@ protected function fetch($ETag, $content) {
$responseJson['data'] = json_decode($response->getBody(), true);
$ETag = $response->getHeader('ETag');
}
$this->config->deleteAppValue('settings', 'appstore-fetcher-lastFailure');

$responseJson['timestamp'] = $this->timeFactory->getTime();
$responseJson['ncversion'] = $this->getVersion();
Expand Down Expand Up @@ -175,6 +185,11 @@ public function get($allowUnstable = false) {
// Refresh the file content
try {
$responseJson = $this->fetch($ETag, $content, $allowUnstable);

if (empty($responseJson)) {
return [];
}

// Don't store the apps request file
if ($allowUnstable) {
return $responseJson['data'];
Expand Down
111 changes: 36 additions & 75 deletions tests/lib/App/AppStore/Fetcher/FetcherBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,19 @@ public function testGetWithAlreadyExistingFileAndUpToDateTimestampAndVersion() {

public function testGetWithNotExistingFileAndUpToDateTimestampAndVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('appstoreenabled', true)
->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');
->willReturnCallback(function ($var, $default) {
if ($var === 'appstoreenabled') {
return true;
} elseif ($var === 'has_internet_connection') {
return true;
} elseif ($var === 'appstoreurl') {
return 'https://apps.nextcloud.com/api/v1';
} elseif ($var === 'version') {
return '11.0.0.2';
}
return $default;
});

$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
Expand Down Expand Up @@ -286,32 +273,19 @@ public function testGetWithAlreadyExistingFileAndOutdatedTimestamp() {

public function testGetWithAlreadyExistingFileAndNoVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('appstoreenabled', true)
->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');
->willReturnCallback(function ($var, $default) {
if ($var === 'appstoreenabled') {
return true;
} elseif ($var === 'has_internet_connection') {
return true;
} elseif ($var === 'appstoreurl') {
return 'https://apps.nextcloud.com/api/v1';
} elseif ($var === 'version') {
return '11.0.0.2';
}
return $default;
});

$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
Expand Down Expand Up @@ -375,32 +349,19 @@ public function testGetWithAlreadyExistingFileAndNoVersion() {

public function testGetWithAlreadyExistingFileAndOutdatedVersion() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('appstoreenabled', true)
->willReturn(true);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->at(2))
->method('getSystemValue')
->with('appstoreenabled', true)
->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');
->willReturnCallback(function ($var, $default) {
if ($var === 'appstoreenabled') {
return true;
} elseif ($var === 'has_internet_connection') {
return true;
} elseif ($var === 'appstoreurl') {
return 'https://apps.nextcloud.com/api/v1';
} elseif ($var === 'version') {
return '11.0.0.2';
}
return $default;
});

$folder = $this->createMock(ISimpleFolder::class);
$file = $this->createMock(ISimpleFile::class);
Expand Down