From 7a733d3d116239fc99ebc9a26ff0b3b4675a4f7c Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sun, 16 Apr 2023 18:47:26 -0700 Subject: [PATCH] index: improve job message Signed-off-by: Varun Patil --- lib/Controller/OtherController.php | 12 +++++++- lib/Cron/IndexJob.php | 48 ++++++++++++++++++++++++++---- src/Admin.vue | 29 ++++++++++++++++++ 3 files changed, 82 insertions(+), 7 deletions(-) diff --git a/lib/Controller/OtherController.php b/lib/Controller/OtherController.php index 896948b2..1038c9cc 100644 --- a/lib/Controller/OtherController.php +++ b/lib/Controller/OtherController.php @@ -111,6 +111,10 @@ class OtherController extends GenericApiController public function getSystemStatus(): Http\Response { return Util::guardEx(function () { + $config = \OC::$server->get(\OCP\IConfig::class); + $index = \OC::$server->get(\OCA\Memories\Service\Index::class); + + // Build status array $status = []; // Check exiftool version @@ -129,9 +133,15 @@ class OtherController extends GenericApiController $status['perl'] = $this->getExecutableStatus(exec('which perl')); // Check number of indexed files - $index = \OC::$server->get(\OCA\Memories\Service\Index::class); $status['indexed_count'] = $index->getIndexedCount(); + // Automatic indexing stats + $jobStart = $config->getAppValue(Application::APPNAME, 'last_index_job_start', 0); + $status['last_index_job_start'] = $jobStart ? time() - $jobStart : 0; // Seconds ago + $status['last_index_job_duration'] = $config->getAppValue(Application::APPNAME, 'last_index_job_duration', 0); + $status['last_index_job_status'] = $config->getAppValue(Application::APPNAME, 'last_index_job_status', 'Indexing has not been run yet'); + $status['last_index_job_status_type'] = $config->getAppValue(Application::APPNAME, 'last_index_job_status_type', 'warning'); + // Check supported preview mimes $status['mimes'] = $index->getPreviewMimes($index->getAllMimes()); diff --git a/lib/Cron/IndexJob.php b/lib/Cron/IndexJob.php index bf3a16b5..0fc391f4 100644 --- a/lib/Cron/IndexJob.php +++ b/lib/Cron/IndexJob.php @@ -2,32 +2,39 @@ namespace OCA\Memories\Cron; +use OCA\Memories\AppInfo\Application; use OCA\Memories\Service; use OCA\Memories\Util; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; +use OCP\IConfig; use OCP\IUserManager; use Psr\Log\LoggerInterface; -const MAX_RUN_TIME = 10; // seconds -const INTERVAL = 600; // seconds (don't set this too low) +const MAX_RUN_TIME = 300; // seconds +const INTERVAL = 900; // seconds (don't set this too low) class IndexJob extends TimedJob { private Service\Index $service; private IUserManager $userManager; private LoggerInterface $logger; + private IConfig $config; + + private bool $_hasError = false; public function __construct( ITimeFactory $time, Service\Index $service, IUserManager $userManager, - LoggerInterface $logger + LoggerInterface $logger, + IConfig $config ) { parent::__construct($time); $this->service = $service; $this->userManager = $userManager; $this->logger = $logger; + $this->config = $config; $this->setInterval(INTERVAL); } @@ -39,6 +46,10 @@ class IndexJob extends TimedJob return; } + // Store the last run time + $this->config->setAppValue(Application::APPNAME, 'last_index_job_start', time()); + $this->config->setAppValue(Application::APPNAME, 'last_index_job_duration', 0); + // Run for a maximum of 5 minutes $startTime = microtime(true); $this->service->continueCheck = function () use ($startTime) { @@ -50,11 +61,16 @@ class IndexJob extends TimedJob try { \OCA\Memories\Exif::ensureStaticExiftoolProc(); $this->indexAllUsers(); + $this->log('Indexing completed successfully', 'success'); } catch (Service\ProcessClosedException $e) { - $this->logger->warning('Memories: Indexing process closed before completion, will continue on next run.'); + $this->log('Indexing process stopped before completion. Will continue on next run', 'warning'); } finally { \OCA\Memories\Exif::closeStaticExiftoolProc(); } + + // Store the last run duration + $duration = round(microtime(true) - $startTime, 2); + $this->config->setAppValue(Application::APPNAME, 'last_index_job_duration', $duration); } /** @@ -70,10 +86,30 @@ class IndexJob extends TimedJob } catch (Service\ProcessClosedException $e) { throw $e; } catch (\Exception $e) { - $this->logger->error('Indexing failed for user '.$user->getUID().': '.$e->getMessage()); + $this->log('Indexing failed for user '.$user->getUID().': '.$e->getMessage()); } catch (\Throwable $e) { - $this->logger->error('[BUG] uncaught exception in memories: '.$e->getMessage()); + $this->log('[BUG] uncaught exception: '.$e->getMessage()); } }); } + + private function log(string $msg, string $type = 'error'): void + { + if ($this->_hasError && 'success' === $type) { + // Don't overwrite an error with a success + return; + } + + $this->config->setAppValue(Application::APPNAME, 'last_index_job_status', $msg); + $this->config->setAppValue(Application::APPNAME, 'last_index_job_status_type', $type); + + if ('success' === $type) { + // Nothing + } elseif ('warning' === $type) { + $this->logger->warning('Memories: '.$msg); + } elseif ('error' === $type) { + $this->_hasError = true; + $this->logger->error('Memories: '.$msg); + } + } } diff --git a/src/Admin.vue b/src/Admin.vue index 0653a260..b7ef517d 100644 --- a/src/Admin.vue +++ b/src/Admin.vue @@ -46,6 +46,30 @@ }) }} + + {{ + t("memories", "Automatic Indexing status: {status}", { + status: status.last_index_job_status, + }) + }} + + + {{ + t("memories", "Last index job was run {t} seconds ago.", { + t: status.last_index_job_start, + }) + }} + {{ + status.last_index_job_duration + ? t("memories", "It took {t} seconds to complete.", { + t: status.last_index_job_duration, + }) + : t("memories", "It is still running or was interrupted.") + }} + {{ t( @@ -544,6 +568,11 @@ const invertedBooleans = ["enableTranscoding"]; type BinaryStatus = "ok" | "not_found" | "not_executable" | "test_ok" | string; type IStatus = { + last_index_job_start: number; + last_index_job_duration: number; + last_index_job_status: string; + last_index_job_status_type: string; + bad_encryption: boolean; indexed_count: number; mimes: string[];