2021-03-20 03:32:50 -07:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2020 mmonkey
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
import datetime
|
2021-03-20 03:32:50 -07:00
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
from . import config, constants
|
2022-04-25 08:00:07 -07:00
|
|
|
from .services.background_scheduler import BackgroundScheduler, use_APScheduler
|
2021-03-20 03:32:50 -07:00
|
|
|
from .tasks.database import TaskReconnectDatabase
|
2022-04-24 02:14:39 -07:00
|
|
|
from .tasks.thumbnail import TaskGenerateCoverThumbnails, TaskGenerateSeriesThumbnails, TaskClearCoverThumbnailCache
|
2021-09-29 00:40:12 -07:00
|
|
|
from .services.worker import WorkerThread
|
2021-03-20 03:32:50 -07:00
|
|
|
|
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
def get_scheduled_tasks(reconnect=True):
|
|
|
|
tasks = list()
|
2022-04-25 08:00:07 -07:00
|
|
|
# config.schedule_reconnect or
|
2021-09-29 00:40:12 -07:00
|
|
|
# Reconnect Calibre database (metadata.db)
|
|
|
|
if reconnect:
|
2022-04-24 23:24:14 -07:00
|
|
|
tasks.append([lambda: TaskReconnectDatabase(), 'reconnect', False])
|
2021-09-29 00:40:12 -07:00
|
|
|
|
|
|
|
# Generate all missing book cover thumbnails
|
|
|
|
if config.schedule_generate_book_covers:
|
2022-04-24 23:24:14 -07:00
|
|
|
tasks.append([lambda: TaskClearCoverThumbnailCache(0), 'delete superfluous book covers', True])
|
2022-04-26 01:49:06 -07:00
|
|
|
tasks.append([lambda: TaskGenerateCoverThumbnails(), 'generate book covers', False])
|
2021-09-29 00:40:12 -07:00
|
|
|
|
|
|
|
# Generate all missing series thumbnails
|
|
|
|
if config.schedule_generate_series_covers:
|
2022-04-24 23:24:14 -07:00
|
|
|
tasks.append([lambda: TaskGenerateSeriesThumbnails(), 'generate book covers', False])
|
2021-09-29 00:40:12 -07:00
|
|
|
|
|
|
|
return tasks
|
|
|
|
|
|
|
|
|
|
|
|
def end_scheduled_tasks():
|
|
|
|
worker = WorkerThread.get_instance()
|
2022-04-23 10:56:02 -07:00
|
|
|
for __, __, __, task, __ in worker.tasks:
|
2021-09-29 00:40:12 -07:00
|
|
|
if task.scheduled and task.is_cancellable:
|
|
|
|
worker.end_task(task.id)
|
|
|
|
|
|
|
|
|
2022-04-24 23:28:29 -07:00
|
|
|
def register_scheduled_tasks(reconnect=True):
|
2021-03-20 03:32:50 -07:00
|
|
|
scheduler = BackgroundScheduler()
|
|
|
|
|
2021-09-19 20:45:19 -07:00
|
|
|
if scheduler:
|
2021-09-29 00:40:12 -07:00
|
|
|
# Remove all existing jobs
|
|
|
|
scheduler.remove_all_jobs()
|
2021-03-20 03:32:50 -07:00
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
start = config.schedule_start_time
|
2022-04-25 08:00:07 -07:00
|
|
|
duration = config.schedule_duration
|
2021-09-26 00:02:48 -07:00
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
# Register scheduled tasks
|
2022-04-25 08:00:07 -07:00
|
|
|
scheduler.schedule_tasks(tasks=get_scheduled_tasks(), trigger='cron', hour=start)
|
2022-04-26 01:55:11 -07:00
|
|
|
end_time = calclulate_end_time(start, duration)
|
|
|
|
scheduler.schedule(func=end_scheduled_tasks, trigger='cron', name="end scheduled task", hour=end_time.hour,
|
|
|
|
minute=end_time.minute)
|
2021-09-21 21:39:00 -07:00
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
# Kick-off tasks, if they should currently be running
|
2022-04-25 08:00:07 -07:00
|
|
|
if should_task_be_running(start, duration):
|
2022-04-24 23:28:29 -07:00
|
|
|
scheduler.schedule_tasks_immediately(tasks=get_scheduled_tasks(reconnect))
|
2021-09-21 21:39:00 -07:00
|
|
|
|
2021-09-29 00:40:12 -07:00
|
|
|
|
|
|
|
def register_startup_tasks():
|
2021-09-26 00:02:48 -07:00
|
|
|
scheduler = BackgroundScheduler()
|
|
|
|
|
|
|
|
if scheduler:
|
2021-09-29 00:40:12 -07:00
|
|
|
start = config.schedule_start_time
|
2022-04-25 08:00:07 -07:00
|
|
|
duration = config.schedule_duration
|
2021-09-29 00:40:12 -07:00
|
|
|
|
|
|
|
# Run scheduled tasks immediately for development and testing
|
|
|
|
# Ignore tasks that should currently be running, as these will be added when registering scheduled tasks
|
2022-04-25 08:00:07 -07:00
|
|
|
if constants.APP_MODE in ['development', 'test'] and not should_task_be_running(start, duration):
|
2021-09-29 00:40:12 -07:00
|
|
|
scheduler.schedule_tasks_immediately(tasks=get_scheduled_tasks(False))
|
2022-01-28 21:48:44 -08:00
|
|
|
|
|
|
|
|
2022-04-25 08:00:07 -07:00
|
|
|
def should_task_be_running(start, duration):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
start_time = datetime.datetime.now().replace(hour=start, minute=0, second=0, microsecond=0)
|
|
|
|
end_time = start_time + datetime.timedelta(hours=duration // 60, minutes=duration % 60)
|
|
|
|
return start_time < now < end_time
|
2022-04-26 01:55:11 -07:00
|
|
|
|
|
|
|
def calclulate_end_time(start, duration):
|
|
|
|
start_time = datetime.datetime.now().replace(hour=start, minute=0)
|
|
|
|
return start_time + datetime.timedelta(hours=duration // 60, minutes=duration % 60)
|
|
|
|
|