Added CACHE_DIR env variable, graceful handling when APScheduler is not installed
This commit is contained in:
parent
d648785471
commit
8bee2b9552
|
@ -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")
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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():
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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,9 +51,11 @@ class BackgroundScheduler:
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
def add(self, func, trigger, **trigger_args):
|
def add(self, func, trigger, **trigger_args):
|
||||||
|
if use_APScheduler:
|
||||||
return self.scheduler.add_job(func=func, trigger=trigger, **trigger_args)
|
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):
|
||||||
|
if use_APScheduler:
|
||||||
def scheduled_task():
|
def scheduled_task():
|
||||||
worker_task = task()
|
worker_task = task()
|
||||||
self.log.info('Running scheduled task in background: ' + worker_task.name + ': ' + worker_task.message)
|
self.log.info('Running scheduled task in background: ' + worker_task.name + ': ' + worker_task.message)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user