From ef9d0f4dafa1df2fb3dc0e79499f01d2d36f0dd9 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 16 Dec 2021 09:11:57 -0100 Subject: moving few method from cronService->pointService Signed-off-by: Maxence Lange --- lib/Command/PointPurge.php | 16 ++-- lib/Cron/Manage.php | 6 +- lib/Service/CronService.php | 161 ---------------------------------------- lib/Service/PointService.php | 173 ++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 174 insertions(+), 182 deletions(-) diff --git a/lib/Command/PointPurge.php b/lib/Command/PointPurge.php index f70636d..28ae98c 100755 --- a/lib/Command/PointPurge.php +++ b/lib/Command/PointPurge.php @@ -32,7 +32,7 @@ declare(strict_types=1); namespace OCA\Backup\Command; use OC\Core\Command\Base; -use OCA\Backup\Service\CronService; +use OCA\Backup\Service\PointService; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -44,19 +44,19 @@ use Symfony\Component\Console\Output\OutputInterface; class PointPurge extends Base { - /** @var CronService */ - private $cronService; + /** @var PointService */ + private $pointService; /** * PointPurge constructor. * - * @param CronService $cronService + * @param PointService $pointService */ - public function __construct(CronService $cronService) { + public function __construct(PointService $pointService) { parent::__construct(); - $this->cronService = $cronService; + $this->pointService = $pointService; } @@ -78,8 +78,8 @@ class PointPurge extends Base { * @return int */ protected function execute(InputInterface $input, OutputInterface $output): int { - $this->cronService->purgeRestoringPoints(); - $this->cronService->purgeRemoteRestoringPoints(); + $this->pointService->purgeRestoringPoints(); + $this->pointService->purgeRemoteRestoringPoints(); return 0; } diff --git a/lib/Cron/Manage.php b/lib/Cron/Manage.php index d8027ef..f21eb59 100644 --- a/lib/Cron/Manage.php +++ b/lib/Cron/Manage.php @@ -33,8 +33,8 @@ namespace OCA\Backup\Cron; use OC\BackgroundJob\TimedJob; use OCA\Backup\Exceptions\ExternalFolderNotFoundException; -use OCA\Backup\Model\RestoringPoint; use OCA\Backup\Exceptions\JobsTimeSlotException; +use OCA\Backup\Model\RestoringPoint; use OCA\Backup\Service\ConfigService; use OCA\Backup\Service\CronService; use OCA\Backup\Service\ExternalFolderService; @@ -130,8 +130,8 @@ class Manage extends TimedJob { $generateLogs = $this->configService->getAppValueBool(ConfigService::GENERATE_LOGS); // TODO: purge old restoring points. - $this->cronService->purgeRestoringPoints(); - $this->cronService->purgeRemoteRestoringPoints(); + $this->pointService->purgeRestoringPoints(); + $this->pointService->purgeRemoteRestoringPoints(); // next steps are only available during night shift $this->cronService->lockCron(); diff --git a/lib/Service/CronService.php b/lib/Service/CronService.php index fe3b1a7..db940ab 100644 --- a/lib/Service/CronService.php +++ b/lib/Service/CronService.php @@ -31,18 +31,9 @@ declare(strict_types=1); namespace OCA\Backup\Service; -use ArtificialOwl\MySmallPhpTools\Exceptions\SignatoryException; -use ArtificialOwl\MySmallPhpTools\Exceptions\SignatureException; use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools; -use OCA\Backup\Exceptions\ExternalFolderNotFoundException; use OCA\Backup\Exceptions\JobsTimeSlotException; -use OCA\Backup\Exceptions\RemoteInstanceException; -use OCA\Backup\Exceptions\RemoteInstanceNotFoundException; -use OCA\Backup\Exceptions\RemoteResourceNotFoundException; use OCA\Backup\Exceptions\SettingsException; -use OCA\Backup\Model\ExternalFolder; -use OCA\Backup\Model\RemoteInstance; -use Throwable; /** * Class CronService @@ -58,12 +49,6 @@ class CronService { public const LOCK_TIMEOUT = 3600; - /** @var PointService */ - private $pointService; - - /** @var RemoteService */ - private $remoteService; - /** @var RemoteStreamService */ private $remoteStreamService; @@ -84,23 +69,17 @@ class CronService { /** * CronService constructor. * - * @param PointService $pointService - * @param RemoteService $remoteService * @param RemoteStreamService $remoteStreamService * @param ExternalFolderService $externalFolderService * @param OutputService $outputService * @param ConfigService $configService */ public function __construct( - PointService $pointService, - RemoteService $remoteService, RemoteStreamService $remoteStreamService, ExternalFolderService $externalFolderService, OutputService $outputService, ConfigService $configService ) { - $this->pointService = $pointService; - $this->remoteService = $remoteService; $this->remoteStreamService = $remoteStreamService; $this->externalFolderService = $externalFolderService; $this->outputService = $outputService; @@ -258,146 +237,6 @@ class CronService { } - /** - * @param bool $local - * @param string $remote - * @param string $external - * - * @return array - */ - public function getRPFromInstances( - bool $local = false, - string $remote = '', - string $external = '' - ): array { - if ($local) { - $instances = [RemoteInstance::LOCAL]; - } elseif ($remote !== '') { - $instances = ['remote:' . $remote]; - } elseif ($external !== '') { - $instances = ['external:' . $external]; - } else { - $instances = array_merge( - [RemoteInstance::LOCAL], - array_map( - function (RemoteInstance $remoteInstance): string { - return 'remote:' . $remoteInstance->getInstance(); - }, $this->remoteService->getOutgoing() - ), - array_map( - function (ExternalFolder $externalFolder): string { - return 'external:' . $externalFolder->getStorageId(); - }, $this->externalFolderService->getAll() - ) - ); - } - - $points = $dates = []; - foreach ($instances as $instance) { - $this->o('- retreiving data from ' . $instance . ''); - - $list = []; - try { - if ($instance === RemoteInstance::LOCAL) { - $list = $this->pointService->getLocalRestoringPoints(); - } else { - [$source, $id] = explode(':', $instance, 2); - if ($source === 'remote') { - $list = $this->remoteService->getRestoringPoints($id); - } elseif ($source === 'external') { - try { - $external = $this->externalFolderService->getByStorageId((int)$id); - $list = $this->externalFolderService->getRestoringPoints($external); - } catch (ExternalFolderNotFoundException $e) { - } - } - } - } catch (RemoteInstanceException - | RemoteInstanceNotFoundException - | RemoteResourceNotFoundException $e) { - continue; - } - - foreach ($list as $item) { - $this->o(' > found RestoringPoint ' . $item->getId() . ''); - if (!array_key_exists($item->getId(), $points)) { - $points[$item->getId()] = []; - } - - $issue = ''; - if ($instance !== RemoteInstance::LOCAL) { - $storedDate = $this->getInt($item->getId(), $dates); - if ($storedDate > 0 && $storedDate !== $item->getDate()) { - $this->o(' ! different date'); - $issue = 'different date'; - } - - try { - $this->remoteStreamService->verifyPoint($item); - } catch (SignatoryException | SignatureException $e) { - $this->o(' ! cannot confirm integrity'); - $issue = 'cannot confirm integrity'; - } - } - - $points[$item->getId()][$instance] = [ - 'point' => $item, - 'issue' => $issue - ]; - - $dates[$item->getId()] = $item->getDate(); - } - } - - return $this->orderByDate($points, $dates); - } - - - /** - * - */ - public function purgeRestoringPoints(): void { - $c = $this->configService->getAppValue(ConfigService::STORE_ITEMS); - $i = 0; - foreach ($this->pointService->getLocalRestoringPoints(0, 0, false) as $point) { - if ($point->isArchive()) { - continue; - } - $i++; - if ($i > $c) { - try { - $this->pointService->delete($point); - } catch (Throwable $e) { - } - } - } - } - - /** - * - */ - public function purgeRemoteRestoringPoints(): void { - } - - - /** - * @param array $points - * @param array $dates - * - * @return array - */ - private function orderByDate(array $points, array $dates): array { - asort($dates); - - $result = []; - foreach ($dates as $pointId => $date) { - $result[$pointId] = $points[$pointId]; - } - - return $result; - } - - /** * @param string $line * @param bool $ln diff --git a/lib/Service/PointService.php b/lib/Service/PointService.php index 1fef028..5acf216 100644 --- a/lib/Service/PointService.php +++ b/lib/Service/PointService.php @@ -33,6 +33,7 @@ namespace OCA\Backup\Service; use ArtificialOwl\MySmallPhpTools\Exceptions\InvalidItemException; use ArtificialOwl\MySmallPhpTools\Exceptions\SignatoryException; +use ArtificialOwl\MySmallPhpTools\Exceptions\SignatureException; use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Deserialize; use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Logger; use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc23\TNC23Signatory; @@ -50,12 +51,16 @@ use OCA\Backup\Exceptions\BackupScriptNotFoundException; use OCA\Backup\Exceptions\ExternalAppdataException; use OCA\Backup\Exceptions\ExternalFolderNotFoundException; use OCA\Backup\Exceptions\ParentRestoringPointNotFoundException; +use OCA\Backup\Exceptions\RemoteInstanceException; +use OCA\Backup\Exceptions\RemoteInstanceNotFoundException; +use OCA\Backup\Exceptions\RemoteResourceNotFoundException; use OCA\Backup\Exceptions\RestoringChunkNotFoundException; use OCA\Backup\Exceptions\RestoringPointNotFoundException; use OCA\Backup\Exceptions\SqlDumpException; use OCA\Backup\ISqlDump; use OCA\Backup\Model\ChunkPartHealth; use OCA\Backup\Model\ExternalFolder; +use OCA\Backup\Model\RemoteInstance; use OCA\Backup\Model\RestoringChunk; use OCA\Backup\Model\RestoringChunkPart; use OCA\Backup\Model\RestoringData; @@ -94,6 +99,9 @@ class PointService { /** @var ChangesRequest */ private $changesRequest; + /** @var RemoteService */ + private $remoteService; + /** @var RemoteStreamService */ private $remoteStreamService; @@ -133,6 +141,7 @@ class PointService { * * @param PointRequest $pointRequest * @param ChangesRequest $changesRequest + * @param RemoteService $remoteService * @param RemoteStreamService $remoteStreamService * @param ExternalFolderService $externalFolderService * @param ChunkService $chunkService @@ -146,6 +155,7 @@ class PointService { public function __construct( PointRequest $pointRequest, ChangesRequest $changesRequest, + RemoteService $remoteService, RemoteStreamService $remoteStreamService, ExternalFolderService $externalFolderService, ChunkService $chunkService, @@ -158,6 +168,7 @@ class PointService { ) { $this->pointRequest = $pointRequest; $this->changesRequest = $changesRequest; + $this->remoteService = $remoteService; $this->remoteStreamService = $remoteStreamService; $this->externalFolderService = $externalFolderService; $this->chunkService = $chunkService; @@ -210,16 +221,6 @@ class PointService { } - /** - * @param string $instance - * - * @return RestoringPoint[] - */ - public function getRPByInstance(string $instance): array { - return $this->pointRequest->getByInstance($instance); - } - - /** * @param bool $complete * @param string $comment @@ -817,6 +818,158 @@ class PointService { } + /** + * @param string $instance + * + * @return RestoringPoint[] + */ + public function getRPByInstance(string $instance): array { + return $this->pointRequest->getByInstance($instance); + } + + + /** + * TODO: explode the method as some part of the process is external folder related... + * + * @param bool $local + * @param string $remote + * @param string $external + * + * @return array + * @throws SignatureException + */ + public function getRPFromInstances( + bool $local = false, + string $remote = '', + string $external = '' + ): array { + if ($local) { + $instances = [RemoteInstance::LOCAL]; + } elseif ($remote !== '') { + $instances = ['remote:' . $remote]; + } elseif ($external !== '') { + $instances = ['external:' . $external]; + } else { + $instances = array_merge( + [RemoteInstance::LOCAL], + array_map( + function (RemoteInstance $remoteInstance): string { + return 'remote:' . $remoteInstance->getInstance(); + }, $this->remoteService->getOutgoing() + ), + array_map( + function (ExternalFolder $externalFolder): string { + return 'external:' . $externalFolder->getStorageId(); + }, $this->externalFolderService->getAll() + ) + ); + } + + $points = $dates = []; + foreach ($instances as $instance) { + $this->o('- retreiving data from ' . $instance . ''); + + $list = []; + try { + if ($instance === RemoteInstance::LOCAL) { + $list = $this->getLocalRestoringPoints(); + } else { + [$source, $id] = explode(':', $instance, 2); + if ($source === 'remote') { + $list = $this->remoteService->getRestoringPoints($id); + } elseif ($source === 'external') { + try { + $external = $this->externalFolderService->getByStorageId((int)$id); + $list = $this->externalFolderService->getRestoringPoints($external); + } catch (ExternalFolderNotFoundException $e) { + } + } + } + } catch (RemoteInstanceException + | RemoteInstanceNotFoundException + | RemoteResourceNotFoundException $e) { + continue; + } + + foreach ($list as $item) { + $this->o(' > found RestoringPoint ' . $item->getId() . ''); + if (!array_key_exists($item->getId(), $points)) { + $points[$item->getId()] = []; + } + + $issue = ''; + if ($instance !== RemoteInstance::LOCAL) { + $storedDate = $this->getInt($item->getId(), $dates); + if ($storedDate > 0 && $storedDate !== $item->getDate()) { + $this->o(' ! different date'); + $issue = 'different date'; + } + + try { + $this->remoteStreamService->verifyPoint($item); + } catch (SignatoryException | SignatureException $e) { + $this->o(' ! cannot confirm integrity'); + $issue = 'cannot confirm integrity'; + } + } + + $points[$item->getId()][$instance] = [ + 'point' => $item, + 'issue' => $issue + ]; + + $dates[$item->getId()] = $item->getDate(); + } + } + + return $this->orderByDate($points, $dates); + } + + + /** + * @param array $points + * @param array $dates + * + * @return array + */ + private function orderByDate(array $points, array $dates): array { + asort($dates); + + $result = []; + foreach ($dates as $pointId => $date) { + $result[$pointId] = $points[$pointId]; + } + + return $result; + } + + /** + * + */ + public function purgeRestoringPoints(): void { + $c = $this->configService->getAppValue(ConfigService::STORE_ITEMS); + $i = 0; + foreach ($this->getLocalRestoringPoints(0, 0, false) as $point) { + if ($point->isArchive()) { + continue; + } + $i++; + if ($i > $c) { + try { + $this->delete($point); + } catch (Throwable $e) { + } + } + } + } + + /** + * + */ + public function purgeRemoteRestoringPoints(): void { + } + + /** * @param RestoringPoint $point * -- cgit v1.2.3