Skip to content

Commit

Permalink
Merge pull request #793 from nextcloud/backport/785/785-stable26
Browse files Browse the repository at this point in the history
[stable26] PlatformTemporaryException
  • Loading branch information
ArtificialOwl authored Sep 26, 2023
2 parents f2b222e + f7e7827 commit 131d06c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 45 deletions.
20 changes: 4 additions & 16 deletions lib/Command/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Exception;
use OC\Core\Command\InterruptedException;
use OCA\FullTextSearch\ACommandBase;
use OCA\FullTextSearch\Exceptions\PlatformTemporaryException;
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
use OCA\FullTextSearch\Model\Index as ModelIndex;
use OCA\FullTextSearch\Model\IndexOptions;
Expand Down Expand Up @@ -287,19 +288,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$this->runner->setInfo('documentCurrent', 'all');
$this->runner->stop();

// while (true) {
// $this->runner->updateAction('_indexOver', true);
// $pressed = strtolower($this->updateAction(''));
// if ($pressed === $this->keys['nextStep']) {
// $this->pauseRunning(false);
// break;
// }
// usleep(300000);
// }


// $output->writeLn('');

return 0;
}

Expand Down Expand Up @@ -412,9 +400,9 @@ private function indexProvider(IFullTextSearchProvider $provider, IndexOptions $
}

try {
$this->indexService->indexProviderContentFromUser(
$platform, $provider, $user->getUID(), $options
);
$this->indexService->indexProviderContentFromUser($platform, $provider, $user->getUID(), $options);
} catch (PlatformTemporaryException $e) {
throw $e;
} catch (Exception $e) {
continue;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/Command/Live.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
namespace OCA\FullTextSearch\Command;


use OCA\FullTextSearch\Exceptions\PlatformTemporaryException;
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
use Exception;
use OC\Core\Command\InterruptedException;
Expand Down Expand Up @@ -288,13 +289,17 @@ protected function execute(InputInterface $input, OutputInterface $output) {
* @throws TickDoesNotExistException
*/
private function liveCycle() {

$wrapper = $this->platformService->getPlatform();
$platform = $wrapper->getPlatform();
$platform->setRunner($this->runner);
$connected = true;

while (true) {

if (!$connected) {
$platform->loadPlatform();
}

$indexes = $this->indexService->getQueuedIndexes();

foreach ($indexes as $index) {
Expand All @@ -306,6 +311,9 @@ private function liveCycle() {

$provider->setRunner($this->runner);
$this->indexService->updateDocument($platform, $provider, $index);
} catch (PlatformTemporaryException $e) {
$connected = false; // assuming we're not connected anymore
break; // we'll connect and try again next tick
} catch (Exception $e) {
$this->runner->exception($e->getMessage(), false);
// TODO - upgrade error number - after too many errors, delete index
Expand Down
48 changes: 22 additions & 26 deletions lib/Cron/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,30 @@
use Exception;
use OC\BackgroundJob\TimedJob;
use OCA\FullTextSearch\AppInfo\Application;
use OCA\FullTextSearch\Exceptions\PlatformTemporaryException;
use OCA\FullTextSearch\Exceptions\RunnerAlreadyUpException;
use OCA\FullTextSearch\Model\Runner;
use OCA\FullTextSearch\Service\ConfigService;
use OCA\FullTextSearch\Service\IndexService;
use OCA\FullTextSearch\Service\MiscService;
use OCA\FullTextSearch\Service\PlatformService;
use OCA\FullTextSearch\Service\ProviderService;
use OCA\FullTextSearch\Service\RunningService;
use OCP\AppFramework\QueryException;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
use Throwable;


class Index extends TimedJob {

const HOUR_ERR_RESET = 240;


/** @var IUserManager */
private $userManager;

/** @var ConfigService */
private $configService;

/** @var IndexService */
private $indexService;

/** @var PlatformService */
private $platformService;

/** @var ProviderService */
private $providerService;

/** @var MiscService */
private $miscService;

/** @var Runner */
private $runner;
private IUserManager $userManager;
private ConfigService $configService;
private IndexService $indexService;
private PlatformService $platformService;
private ProviderService $providerService;
private Runner $runner;
private LoggerInterface $logger;

public function __construct() {
$this->setInterval(12 * 60); // 12 minutes
Expand All @@ -95,15 +81,18 @@ protected function run($argument) {
$this->indexService = $c->query(IndexService::class);
$this->platformService = $c->query(PlatformService::class);
$this->providerService = $c->query(ProviderService::class);
$this->miscService = $c->query(MiscService::class);
$this->logger = $c->query(LoggerInterface::class);

try {
$this->runner->start();
$this->liveCycle();
$this->runner->stop();
} catch (RunnerAlreadyUpException $e) {
} catch (Exception $e) {
$this->miscService->log('Exception while cronIndex: ' . get_class($e) . ' - ' . $e->getMessage());
$this->logger->notice(
'exception encountered while running fulltextsearch/lib/Cron/Index.php',
['exception' => $e]
);
$this->runner->exception($e->getMessage(), true);
}

Expand All @@ -129,8 +118,15 @@ private function liveCycle() {
$provider = $providerWrapper->getProvider();

$this->indexService->updateDocument($platform, $provider, $index);
} catch (Throwable | Exception $e) {
} catch (PlatformTemporaryException $e) {
$this->logger->warning('platform seems down. we will update index next cron tick');
return;
} catch (Throwable|Exception $e) {
$this->runner->exception(get_class($e) . ' - ' . $e->getMessage(), false);
$this->logger->notice(
'exception encountered while running fulltextsearch/lib/Cron/Index.php',
['exception' => $e]
);
// TODO - upgrade error number - after too many errors, delete index
// TODO - do not count error if elasticsearch is down.
}
Expand Down
38 changes: 38 additions & 0 deletions lib/Exceptions/PlatformTemporaryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* FullTextSearch - Full text search framework for Nextcloud
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <[email protected]>
* @copyright 2023
* @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\FullTextSearch\Exceptions;

use Exception;

/**
* @deprecated - nc28 use \OCP\FullTextSearch\Exceptions\PlatformTemporaryException
*/
class PlatformTemporaryException extends Exception {
}
8 changes: 6 additions & 2 deletions lib/Service/IndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCA\FullTextSearch\Db\IndexesRequest;
use OCA\FullTextSearch\Exceptions\IndexDoesNotExistException;
use OCA\FullTextSearch\Exceptions\NotIndexableDocumentException;
use OCA\FullTextSearch\Exceptions\PlatformTemporaryException;
use OCA\FullTextSearch\Exceptions\ProviderDoesNotExistException;
use OCA\FullTextSearch\Model\Index;
use OCA\FullTextSearch\Model\IndexOptions;
Expand Down Expand Up @@ -323,7 +324,8 @@ private function indexDocuments(

$index = $this->indexDocument($platform, $document);
$this->updateIndex($index);

} catch (PlatformTemporaryException $e) {
throw $e;
} catch (Exception $e) {
}

Expand Down Expand Up @@ -371,10 +373,11 @@ public function indexDocument(IFullTextSearchPlatform $platform, IIndexDocument

try {
return $platform->indexDocument($document);
} catch (PlatformTemporaryException $e) {
throw $e;
} catch (Exception $e) {
throw new IndexDoesNotExistException();
}

}


Expand All @@ -383,6 +386,7 @@ public function indexDocument(IFullTextSearchPlatform $platform, IIndexDocument
* @param IFullTextSearchProvider $provider
* @param Index $index
*
* @throws PlatformTemporaryException
* @throws Exception
*/
public function updateDocument(
Expand Down

0 comments on commit 131d06c

Please sign in to comment.