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
{
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());

View File

@ -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);
}
}
}

View File

@ -46,6 +46,30 @@
})
}}
</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">
{{
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[];