From 524ed07a6c3daf6c8e746a9b5e69f76742fe2301 Mon Sep 17 00:00:00 2001 From: mmonkey Date: Tue, 21 Sep 2021 23:39:00 -0500 Subject: [PATCH] Handle read only access to cache dir gracefully. minor cleanup --- cps.py | 3 +- cps/fs.py | 44 ++++++++++++++++++++++------ cps/schedule.py | 12 +++++--- cps/services/background_scheduler.py | 4 +-- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/cps.py b/cps.py index 82137bbc..fe006551 100755 --- a/cps.py +++ b/cps.py @@ -44,7 +44,7 @@ from cps.editbooks import editbook from cps.remotelogin import remotelogin from cps.search_metadata import meta from cps.error_handler import init_errorhandler -from cps.schedule import register_jobs +from cps.schedule import register_jobs, register_startup_jobs try: from cps.kobo import kobo, get_kobo_activated @@ -83,6 +83,7 @@ def main(): # Register scheduled jobs register_jobs() + # register_startup_jobs() success = web_server.start() sys.exit(0 if success else 1) diff --git a/cps/fs.py b/cps/fs.py index 8528e39c..4f835fa6 100644 --- a/cps/fs.py +++ b/cps/fs.py @@ -17,6 +17,7 @@ # along with this program. If not, see . from __future__ import division, print_function, unicode_literals +from . import logger from .constants import CACHE_DIR from os import listdir, makedirs, remove from os.path import isdir, isfile, join @@ -32,16 +33,26 @@ class FileSystem: def __new__(cls): if cls._instance is None: cls._instance = super(FileSystem, cls).__new__(cls) + cls.log = logger.create() return cls._instance def get_cache_dir(self, cache_type=None): if not isdir(self._cache_dir): - makedirs(self._cache_dir) + try: + makedirs(self._cache_dir) + except OSError: + self.log.info(f'Failed to create path {self._cache_dir} (Permission denied).') + return False - if cache_type and not isdir(join(self._cache_dir, cache_type)): - makedirs(join(self._cache_dir, cache_type)) + path = join(self._cache_dir, cache_type) + if cache_type and not isdir(path): + try: + makedirs(path) + except OSError: + self.log.info(f'Failed to create path {path} (Permission denied).') + return False - return join(self._cache_dir, cache_type) if cache_type else self._cache_dir + return path if cache_type else self._cache_dir def get_cache_file_path(self, filename, cache_type=None): return join(self.get_cache_dir(cache_type), filename) if filename else None @@ -60,10 +71,25 @@ class FileSystem: def delete_cache_dir(self, cache_type=None): if not cache_type and isdir(self._cache_dir): - rmtree(self._cache_dir) - if cache_type and isdir(join(self._cache_dir, cache_type)): - rmtree(join(self._cache_dir, cache_type)) + try: + rmtree(self._cache_dir) + except OSError: + self.log.info(f'Failed to delete path {self._cache_dir} (Permission denied).') + return False + + path = join(self._cache_dir, cache_type) + if cache_type and isdir(path): + try: + rmtree(path) + except OSError: + self.log.info(f'Failed to delete path {path} (Permission denied).') + return False def delete_cache_file(self, filename, cache_type=None): - if isfile(join(self.get_cache_dir(cache_type), filename)): - remove(join(self.get_cache_dir(cache_type), filename)) + path = join(self.get_cache_dir(cache_type), filename) + if isfile(path): + try: + remove(path) + except OSError: + self.log.info(f'Failed to delete path {path} (Permission denied).') + return False diff --git a/cps/schedule.py b/cps/schedule.py index 649b65a3..d427d908 100644 --- a/cps/schedule.py +++ b/cps/schedule.py @@ -18,8 +18,8 @@ from __future__ import division, print_function, unicode_literals -from datetime import datetime from .services.background_scheduler import BackgroundScheduler +from .services.worker import WorkerThread from .tasks.database import TaskReconnectDatabase from .tasks.thumbnail import TaskSyncCoverThumbnailCache, TaskGenerateCoverThumbnails @@ -29,10 +29,14 @@ def register_jobs(): if scheduler: # Reconnect metadata.db once every 12 hours - scheduler.add_task(user=None, task=lambda: TaskReconnectDatabase(), trigger='interval', hours=12) + scheduler.add_task(user=None, task=lambda: TaskReconnectDatabase(), trigger='cron', hour='4,16') # Cleanup book cover cache once every 24 hours - scheduler.add_task(user=None, task=lambda: TaskSyncCoverThumbnailCache(), trigger='interval', days=1) + scheduler.add_task(user=None, task=lambda: TaskSyncCoverThumbnailCache(), trigger='cron', hour=4) # Generate all missing book cover thumbnails once every 24 hours - scheduler.add_task(user=None, task=lambda: TaskGenerateCoverThumbnails(), trigger='interval', days=1) + scheduler.add_task(user=None, task=lambda: TaskGenerateCoverThumbnails(), trigger='cron', hour=4) + + +def register_startup_jobs(): + WorkerThread.add(None, TaskGenerateCoverThumbnails()) diff --git a/cps/services/background_scheduler.py b/cps/services/background_scheduler.py index f2688b7f..1b588ac2 100644 --- a/cps/services/background_scheduler.py +++ b/cps/services/background_scheduler.py @@ -28,7 +28,7 @@ try: except (ImportError, RuntimeError) as e: use_APScheduler = False log = logger.create() - log.info(f'APScheduler not found. Unable to schedule tasks.') + log.info('APScheduler not found. Unable to schedule tasks.') class BackgroundScheduler: @@ -58,7 +58,7 @@ class BackgroundScheduler: if use_APScheduler: def scheduled_task(): worker_task = task() - self.log.info('Running scheduled task in background: ' + worker_task.name + ': ' + worker_task.message) + self.log.info(f'Running scheduled task in background: {worker_task.name} - {worker_task.message}') WorkerThread.add(user, worker_task) return self.add(func=scheduled_task, trigger=trigger, **trigger_args)