Handle read only access to cache dir gracefully. minor cleanup

This commit is contained in:
mmonkey 2021-09-21 23:39:00 -05:00
parent 8bee2b9552
commit 524ed07a6c
4 changed files with 47 additions and 16 deletions

3
cps.py
View File

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

View File

@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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

View File

@ -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())

View File

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