-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Avoid requesting remote endpoints during bootstrap #3749
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Richdocuments\Service; | ||
|
||
use OCA\Richdocuments\AppInfo\Application; | ||
use OCP\Files\AppData\IAppDataFactory; | ||
use OCP\Files\NotFoundException; | ||
use OCP\Files\SimpleFS\ISimpleFolder; | ||
use OCP\Http\Client\IClient; | ||
use OCP\Http\Client\IClientService; | ||
use OCP\IAppConfig; | ||
use OCP\ICacheFactory; | ||
use Psr\Log\LoggerInterface; | ||
|
||
abstract class CachedRequestService { | ||
|
||
public function __construct( | ||
private IClientService $clientService, | ||
private ICacheFactory $cacheFactory, | ||
private IAppDataFactory $appDataFactory, | ||
private IAppConfig $appConfig, | ||
private LoggerInterface $logger, | ||
private string $cacheKey, | ||
) { | ||
} | ||
|
||
/** | ||
* Method to implement sending a request and returning the result as a string | ||
* @throw \Exception in case the request fails | ||
*/ | ||
abstract protected function sendRequest(IClient $client): string; | ||
|
||
public function get(): ?string { | ||
$cache = $this->cacheFactory->createDistributed('richdocuments'); | ||
|
||
if ($cached = $cache->get($this->cacheKey)) { | ||
return $cached; | ||
} | ||
|
||
$folder = $this->getAppDataFolder(); | ||
if ($folder->fileExists($this->cacheKey)) { | ||
$value = $folder->getFile($this->cacheKey)->getContent(); | ||
$cache->set($this->cacheKey, $value, 3600); | ||
return $value; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getLastUpdate(): ?int { | ||
$folder = $this->getAppDataFolder(); | ||
if (!$folder->fileExists($this->cacheKey)) { | ||
return null; | ||
} | ||
return $folder->getFile($this->cacheKey)->getMTime(); | ||
} | ||
|
||
/** | ||
* Cached value will be kept if the request fails | ||
* | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
final public function fetch(): string { | ||
$cache = $this->cacheFactory->createDistributed('richdocuments'); | ||
$client = $this->clientService->newClient(); | ||
|
||
$startTime = microtime(true); | ||
$response = $this->sendRequest($client); | ||
$duration = round(((microtime(true) - $startTime)), 3); | ||
$this->logger->info('Fetched remote endpoint from ' . $this->cacheKey . ' in ' . $duration . ' seconds'); | ||
|
||
$this->getAppDataFolder()->newFile($this->cacheKey, $response); | ||
$cache->set($this->cacheKey, $response); | ||
return $response; | ||
} | ||
|
||
public function resetCache(): void { | ||
$cache = $this->cacheFactory->createDistributed('richdocuments'); | ||
$cache->remove($this->cacheKey); | ||
$folder = $this->getAppDataFolder(); | ||
if ($folder->fileExists($this->cacheKey)) { | ||
$folder->getFile($this->cacheKey)->delete(); | ||
} | ||
} | ||
|
||
protected function getDefaultRequestOptions(): array { | ||
$options = [ | ||
'timeout' => 5, | ||
'nextcloud' => [ | ||
'allow_local_address' => true | ||
] | ||
]; | ||
|
||
if ($this->appConfig->getValueString('richdocuments', 'disable_certificate_verification') === 'yes') { | ||
$options['verify'] = false; | ||
} | ||
|
||
if ($this->isProxyStarting()) { | ||
$options['timeout'] = 180; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
private function getAppDataFolder(): ISimpleFolder { | ||
$appData = $this->appDataFactory->get(Application::APPNAME); | ||
try { | ||
$folder = $appData->getFolder('remoteData'); | ||
} catch (NotFoundException $e) { | ||
$folder = $appData->newFolder('remoteData'); | ||
} | ||
return $folder; | ||
} | ||
|
||
/** | ||
* @return boolean indicating if proxy.php is in initialize or false otherwise | ||
*/ | ||
private function isProxyStarting(): bool { | ||
$url = $this->appConfig->getValueString('richdocuments', 'wopi_url', ''); | ||
$usesProxy = false; | ||
$proxyPos = strrpos($url, 'proxy.php'); | ||
if ($proxyPos !== false) { | ||
$usesProxy = true; | ||
} | ||
|
||
if ($usesProxy === true) { | ||
$statusUrl = substr($url, 0, $proxyPos); | ||
$statusUrl = $statusUrl . 'proxy.php?status'; | ||
|
||
$client = $this->clientService->newClient(); | ||
$options = ['timeout' => 5, 'nextcloud' => ['allow_local_address' => true]]; | ||
|
||
if ($this->appConfig->getValueString('richdocuments', 'disable_certificate_verification') === 'yes') { | ||
$options['verify'] = false; | ||
} | ||
|
||
try { | ||
$response = $client->get($statusUrl, $options); | ||
|
||
if ($response->getStatusCode() === 200) { | ||
$body = json_decode($response->getBody(), true); | ||
|
||
if ($body['status'] === 'starting' | ||
|| $body['status'] === 'stopped' | ||
|| $body['status'] === 'restarting') { | ||
return true; | ||
} | ||
} | ||
} catch (\Exception $e) { | ||
// ignore | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,52 +8,42 @@ | |
|
||
use OCA\Richdocuments\AppInfo\Application; | ||
use OCP\App\IAppManager; | ||
use OCP\Files\AppData\IAppDataFactory; | ||
use OCP\Http\Client\IClient; | ||
use OCP\Http\Client\IClientService; | ||
use OCP\ICache; | ||
use OCP\IAppConfig; | ||
use OCP\ICacheFactory; | ||
use OCP\IConfig; | ||
use OCP\IL10N; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class CapabilitiesService { | ||
/** @var IConfig */ | ||
private $config; | ||
/** @var IClientService */ | ||
private $clientService; | ||
/** @var ICache */ | ||
private $cache; | ||
/** @var IAppManager */ | ||
private $appManager; | ||
/** @var IL10N */ | ||
private $l10n; | ||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
/** @var array */ | ||
private $capabilities; | ||
|
||
|
||
public function __construct(IConfig $config, IClientService $clientService, ICacheFactory $cacheFactory, IAppManager $appManager, IL10N $l10n, LoggerInterface $logger) { | ||
$this->config = $config; | ||
$this->clientService = $clientService; | ||
$this->cache = $cacheFactory->createDistributed('richdocuments'); | ||
$this->appManager = $appManager; | ||
$this->l10n = $l10n; | ||
$this->logger = $logger; | ||
class CapabilitiesService extends CachedRequestService { | ||
|
||
private ?array $capabilities = null; | ||
|
||
public function __construct( | ||
private IClientService $clientService, | ||
private ICacheFactory $cacheFactory, | ||
private IAppDataFactory $appDataFactory, | ||
private IAppConfig $appConfig, | ||
private LoggerInterface $logger, | ||
private IConfig $config, | ||
private IAppManager $appManager, | ||
private IL10N $l10n, | ||
) { | ||
parent::__construct( | ||
$this->clientService, | ||
$this->cacheFactory, | ||
$this->appDataFactory, | ||
$this->appConfig, | ||
$this->logger, | ||
'capabilities', | ||
); | ||
} | ||
|
||
public function getCapabilities() { | ||
if (!$this->capabilities) { | ||
$this->capabilities = $this->cache->get('capabilities'); | ||
} | ||
|
||
$isARM64 = php_uname('m') === 'aarch64'; | ||
$CODEAppID = $isARM64 ? 'richdocumentscode_arm64' : 'richdocumentscode'; | ||
$isCODEInstalled = $this->appManager->isEnabledForUser($CODEAppID); | ||
$isCODEEnabled = strpos($this->config->getAppValue('richdocuments', 'wopi_url'), 'proxy.php?req=') !== false; | ||
$shouldRecheckCODECapabilities = $isCODEInstalled && $isCODEEnabled && ($this->capabilities === null || count($this->capabilities) === 0); | ||
if ($this->capabilities === null || $shouldRecheckCODECapabilities) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what has actually caused the issue, as the cache was only there for an hour every request after it expired triggered a new request to the capabilities endpoint, which was unreachable |
||
$this->fetchFromRemote(); | ||
$this->capabilities = $this->getParsedCapabilities(); | ||
} | ||
|
||
if (!is_array($this->capabilities)) { | ||
|
@@ -119,10 +109,6 @@ public function hasOtherOOXMLApps(): bool { | |
return false; | ||
} | ||
|
||
public function resetCache(): void { | ||
$this->cache->remove('capabilities'); | ||
} | ||
|
||
public function getCapabilitiesEndpoint(): ?string { | ||
$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); | ||
if ($remoteHost === '') { | ||
|
@@ -131,43 +117,13 @@ public function getCapabilitiesEndpoint(): ?string { | |
return rtrim($remoteHost, '/') . '/hosting/capabilities'; | ||
} | ||
|
||
public function fetchFromRemote($throw = false): void { | ||
if (!$this->getCapabilitiesEndpoint()) { | ||
return; | ||
} | ||
|
||
$client = $this->clientService->newClient(); | ||
$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]]; | ||
|
||
if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') { | ||
$options['verify'] = false; | ||
} | ||
|
||
try { | ||
$startTime = microtime(true); | ||
$response = $client->get($this->getCapabilitiesEndpoint(), $options); | ||
$duration = round(((microtime(true) - $startTime)), 3); | ||
$this->logger->info('Fetched capabilities endpoint from ' . $this->getCapabilitiesEndpoint(). ' in ' . $duration . ' seconds'); | ||
$responseBody = $response->getBody(); | ||
$capabilities = \json_decode($responseBody, true); | ||
|
||
if (!is_array($capabilities)) { | ||
$capabilities = []; | ||
} | ||
} catch (\Exception $e) { | ||
$this->logger->error('Failed to fetch the Collabora capabilities endpoint: ' . $e->getMessage(), [ 'exception' => $e ]); | ||
if ($throw) { | ||
throw $e; | ||
} | ||
$capabilities = []; | ||
} | ||
|
||
$this->capabilities = $capabilities; | ||
$ttl = 3600; | ||
if (count($capabilities) === 0) { | ||
$ttl = 60; | ||
} | ||
protected function sendRequest(IClient $client): string { | ||
$response = $client->get($this->getCapabilitiesEndpoint(), $this->getDefaultRequestOptions()); | ||
return (string)$response->getBody(); | ||
} | ||
|
||
$this->cache->set('capabilities', $capabilities, $ttl); | ||
private function getParsedCapabilities() { | ||
$response = $this->get(); | ||
return json_decode($response, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to do anything here with the caught error? Is it OK to catch it and just throw it away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just ignored it as I fear spamming the log here for instances that use richdocumentscode, so I'd rather silence them. The cronjob would still trigger logging of potential errors then.
Will probably look into refactoring that part tomorrow to get rid of those calls.