index: improve job message

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/579/head
Varun Patil 2023-04-16 18:47:26 -07:00
parent c3067dab91
commit 7a733d3d11
3 changed files with 82 additions and 7 deletions

View File

@ -111,6 +111,10 @@ class OtherController extends GenericApiController
public function getSystemStatus(): Http\Response public function getSystemStatus(): Http\Response
{ {
return Util::guardEx(function () { return Util::guardEx(function () {
$config = \OC::$server->get(\OCP\IConfig::class);
$index = \OC::$server->get(\OCA\Memories\Service\Index::class);
// Build status array
$status = []; $status = [];
// Check exiftool version // Check exiftool version
@ -129,9 +133,15 @@ class OtherController extends GenericApiController
$status['perl'] = $this->getExecutableStatus(exec('which perl')); $status['perl'] = $this->getExecutableStatus(exec('which perl'));
// Check number of indexed files // Check number of indexed files
$index = \OC::$server->get(\OCA\Memories\Service\Index::class);
$status['indexed_count'] = $index->getIndexedCount(); $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 // Check supported preview mimes
$status['mimes'] = $index->getPreviewMimes($index->getAllMimes()); $status['mimes'] = $index->getPreviewMimes($index->getAllMimes());

View File

@ -2,32 +2,39 @@
namespace OCA\Memories\Cron; namespace OCA\Memories\Cron;
use OCA\Memories\AppInfo\Application;
use OCA\Memories\Service; use OCA\Memories\Service;
use OCA\Memories\Util; use OCA\Memories\Util;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob; use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
use OCP\IUserManager; use OCP\IUserManager;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
const MAX_RUN_TIME = 10; // seconds const MAX_RUN_TIME = 300; // seconds
const INTERVAL = 600; // seconds (don't set this too low) const INTERVAL = 900; // seconds (don't set this too low)
class IndexJob extends TimedJob class IndexJob extends TimedJob
{ {
private Service\Index $service; private Service\Index $service;
private IUserManager $userManager; private IUserManager $userManager;
private LoggerInterface $logger; private LoggerInterface $logger;
private IConfig $config;
private bool $_hasError = false;
public function __construct( public function __construct(
ITimeFactory $time, ITimeFactory $time,
Service\Index $service, Service\Index $service,
IUserManager $userManager, IUserManager $userManager,
LoggerInterface $logger LoggerInterface $logger,
IConfig $config
) { ) {
parent::__construct($time); parent::__construct($time);
$this->service = $service; $this->service = $service;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->logger = $logger; $this->logger = $logger;
$this->config = $config;
$this->setInterval(INTERVAL); $this->setInterval(INTERVAL);
} }
@ -39,6 +46,10 @@ class IndexJob extends TimedJob
return; 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 // Run for a maximum of 5 minutes
$startTime = microtime(true); $startTime = microtime(true);
$this->service->continueCheck = function () use ($startTime) { $this->service->continueCheck = function () use ($startTime) {
@ -50,11 +61,16 @@ class IndexJob extends TimedJob
try { try {
\OCA\Memories\Exif::ensureStaticExiftoolProc(); \OCA\Memories\Exif::ensureStaticExiftoolProc();
$this->indexAllUsers(); $this->indexAllUsers();
$this->log('Indexing completed successfully', 'success');
} catch (Service\ProcessClosedException $e) { } 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 { } finally {
\OCA\Memories\Exif::closeStaticExiftoolProc(); \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) { } catch (Service\ProcessClosedException $e) {
throw $e; throw $e;
} catch (\Exception $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) { } 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);
}
}
} }

View File

@ -46,6 +46,30 @@
}) })
}} }}
</NcNoteCard> </NcNoteCard>
<NcNoteCard :type="status.last_index_job_status_type">
{{
t("memories", "Automatic Indexing status: {status}", {
status: status.last_index_job_status,
})
}}
</NcNoteCard>
<NcNoteCard
v-if="status.last_index_job_start"
:type="status.last_index_job_duration ? 'success' : 'warning'"
>
{{
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.")
}}
</NcNoteCard>
<NcNoteCard type="error" v-if="status.bad_encryption"> <NcNoteCard type="error" v-if="status.bad_encryption">
{{ {{
t( t(
@ -544,6 +568,11 @@ const invertedBooleans = ["enableTranscoding"];
type BinaryStatus = "ok" | "not_found" | "not_executable" | "test_ok" | string; type BinaryStatus = "ok" | "not_found" | "not_executable" | "test_ok" | string;
type IStatus = { 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; bad_encryption: boolean;
indexed_count: number; indexed_count: number;
mimes: string[]; mimes: string[];