-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Avoid requesting remote endpoints during bootstrap
This will add some abstraction layer for cached request data as used by capabilities and discovery endpoints. By default it will always return the cached data which will never expire (backed by a file in app data in case the memory cache vanishes). Signed-off-by: Julius Härtl <jus@bitgrid.net>
- Loading branch information
1 parent
5285965
commit 18a300f
Showing
6 changed files
with
263 additions
and
188 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net> | ||
* | ||
* @author Julius Härtl <jus@bitgrid.net> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
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
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.