1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-18 22:20:30 +00:00

Added CACHE_DIR env variable, graceful handling when APScheduler is not installed

This commit is contained in:
mmonkey 2021-09-19 22:45:19 -05:00
parent d648785471
commit 8bee2b9552
5 changed files with 40 additions and 16 deletions

View File

@ -40,7 +40,10 @@ else:
STATIC_DIR = os.path.join(BASE_DIR, 'cps', 'static') STATIC_DIR = os.path.join(BASE_DIR, 'cps', 'static')
TEMPLATES_DIR = os.path.join(BASE_DIR, 'cps', 'templates') TEMPLATES_DIR = os.path.join(BASE_DIR, 'cps', 'templates')
TRANSLATIONS_DIR = os.path.join(BASE_DIR, 'cps', 'translations') TRANSLATIONS_DIR = os.path.join(BASE_DIR, 'cps', 'translations')
CACHE_DIR = os.path.join(BASE_DIR, 'cps', 'cache')
# Cache dir - use CACHE_DIR environment variable, otherwise use the default directory: cps/cache
DEFAULT_CACHE_DIR = os.path.join(BASE_DIR, 'cps', 'cache')
CACHE_DIR = os.environ.get("CACHE_DIR", DEFAULT_CACHE_DIR)
if HOME_CONFIG: if HOME_CONFIG:
home_dir = os.path.join(os.path.expanduser("~"),".calibre-web") home_dir = os.path.join(os.path.expanduser("~"),".calibre-web")

View File

@ -50,6 +50,14 @@ class FileSystem:
path = self.get_cache_dir(cache_type) path = self.get_cache_dir(cache_type)
return [file for file in listdir(path) if isfile(join(path, file))] return [file for file in listdir(path) if isfile(join(path, file))]
def list_existing_cache_files(self, filenames, cache_type=None):
path = self.get_cache_dir(cache_type)
return [file for file in listdir(path) if isfile(join(path, file)) and file in filenames]
def list_missing_cache_files(self, filenames, cache_type=None):
path = self.get_cache_dir(cache_type)
return [file for file in listdir(path) if isfile(join(path, file)) and file not in filenames]
def delete_cache_dir(self, cache_type=None): def delete_cache_dir(self, cache_type=None):
if not cache_type and isdir(self._cache_dir): if not cache_type and isdir(self._cache_dir):
rmtree(self._cache_dir) rmtree(self._cache_dir)

View File

@ -405,6 +405,7 @@ def update_dir_structure_file(book_id, calibrepath, first_author, orignal_filepa
src=new_path, dest=new_name, error=str(ex)) src=new_path, dest=new_name, error=str(ex))
return False return False
def update_dir_structure_gdrive(book_id, first_author): def update_dir_structure_gdrive(book_id, first_author):
error = False error = False
book = calibre_db.get_book(book_id) book = calibre_db.get_book(book_id)
@ -505,6 +506,7 @@ def uniq(inpt):
output.append(x) output.append(x)
return output return output
def check_email(email): def check_email(email):
email = valid_email(email) email = valid_email(email)
if ub.session.query(ub.User).filter(func.lower(ub.User.email) == email.lower()).first(): if ub.session.query(ub.User).filter(func.lower(ub.User.email) == email.lower()).first():

View File

@ -27,13 +27,12 @@ from .tasks.thumbnail import TaskSyncCoverThumbnailCache, TaskGenerateCoverThumb
def register_jobs(): def register_jobs():
scheduler = BackgroundScheduler() scheduler = BackgroundScheduler()
# Generate up to 1000 book covers daily if scheduler:
generate_thumbnails_task = scheduler.add_task(user=None, task=lambda: TaskGenerateCoverThumbnails(limit=1000), # Reconnect metadata.db once every 12 hours
trigger='interval', days=1) scheduler.add_task(user=None, task=lambda: TaskReconnectDatabase(), trigger='interval', hours=12)
generate_thumbnails_task.modify(next_run_time=datetime.now())
# Cleanup book cover cache every 6 hours # Cleanup book cover cache once every 24 hours
scheduler.add_task(user=None, task=lambda: TaskSyncCoverThumbnailCache(), trigger='cron', minute='15', hour='*/6') scheduler.add_task(user=None, task=lambda: TaskSyncCoverThumbnailCache(), trigger='interval', days=1)
# Reconnect metadata.db every 4 hours # Generate all missing book cover thumbnails once every 24 hours
scheduler.add_task(user=None, task=lambda: TaskReconnectDatabase(), trigger='cron', minute='5', hour='*/4') scheduler.add_task(user=None, task=lambda: TaskGenerateCoverThumbnails(), trigger='interval', days=1)

View File

@ -21,13 +21,23 @@ import atexit
from .. import logger from .. import logger
from .worker import WorkerThread from .worker import WorkerThread
from apscheduler.schedulers.background import BackgroundScheduler as BScheduler
try:
from apscheduler.schedulers.background import BackgroundScheduler as BScheduler
use_APScheduler = True
except (ImportError, RuntimeError) as e:
use_APScheduler = False
log = logger.create()
log.info(f'APScheduler not found. Unable to schedule tasks.')
class BackgroundScheduler: class BackgroundScheduler:
_instance = None _instance = None
def __new__(cls): def __new__(cls):
if not use_APScheduler:
return False
if cls._instance is None: if cls._instance is None:
cls._instance = super(BackgroundScheduler, cls).__new__(cls) cls._instance = super(BackgroundScheduler, cls).__new__(cls)
@ -41,12 +51,14 @@ class BackgroundScheduler:
return cls._instance return cls._instance
def add(self, func, trigger, **trigger_args): def add(self, func, trigger, **trigger_args):
return self.scheduler.add_job(func=func, trigger=trigger, **trigger_args) if use_APScheduler:
return self.scheduler.add_job(func=func, trigger=trigger, **trigger_args)
def add_task(self, user, task, trigger, **trigger_args): def add_task(self, user, task, trigger, **trigger_args):
def scheduled_task(): if use_APScheduler:
worker_task = task() def scheduled_task():
self.log.info('Running scheduled task in background: ' + worker_task.name + ': ' + worker_task.message) worker_task = task()
WorkerThread.add(user, worker_task) self.log.info('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) return self.add(func=scheduled_task, trigger=trigger, **trigger_args)