2021-10-04 18:26:46 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2020 pwr
|
|
|
|
#
|
|
|
|
# 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/>.
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
import threading
|
2020-08-24 05:00:23 +02:00
|
|
|
import abc
|
|
|
|
import uuid
|
2020-08-30 19:49:45 +02:00
|
|
|
import time
|
2020-08-24 03:21:55 +02:00
|
|
|
|
2020-08-22 22:31:00 +02:00
|
|
|
try:
|
|
|
|
import queue
|
|
|
|
except ImportError:
|
|
|
|
import Queue as queue
|
2020-08-30 13:43:08 +02:00
|
|
|
from datetime import datetime
|
2020-08-24 05:00:23 +02:00
|
|
|
from collections import namedtuple
|
2020-08-22 22:31:00 +02:00
|
|
|
|
2020-08-24 03:21:55 +02:00
|
|
|
from cps import logger
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
# task 'status' consts
|
|
|
|
STAT_WAITING = 0
|
|
|
|
STAT_FAIL = 1
|
|
|
|
STAT_STARTED = 2
|
|
|
|
STAT_FINISH_SUCCESS = 3
|
|
|
|
|
2020-08-24 05:00:23 +02:00
|
|
|
# Only retain this many tasks in dequeued list
|
2020-08-28 03:49:55 +02:00
|
|
|
TASK_CLEANUP_TRIGGER = 20
|
2020-08-24 05:00:23 +02:00
|
|
|
|
2020-08-28 03:44:28 +02:00
|
|
|
QueuedTask = namedtuple('QueuedTask', 'num, user, added, task')
|
2020-08-24 05:00:23 +02:00
|
|
|
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
def _get_main_thread():
|
|
|
|
for t in threading.enumerate():
|
|
|
|
if t.__class__.__name__ == '_MainThread':
|
|
|
|
return t
|
|
|
|
raise Exception("main thread not found?!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImprovedQueue(queue.Queue):
|
|
|
|
def to_list(self):
|
|
|
|
"""
|
|
|
|
Returns a copy of all items in the queue without removing them.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with self.mutex:
|
|
|
|
return list(self.queue)
|
|
|
|
|
2021-04-22 19:14:56 +02:00
|
|
|
# Class for all worker tasks in the background
|
2020-08-22 22:31:00 +02:00
|
|
|
class WorkerThread(threading.Thread):
|
2020-08-23 04:44:28 +02:00
|
|
|
_instance = None
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def getInstance(cls):
|
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = WorkerThread()
|
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
2020-08-24 05:00:23 +02:00
|
|
|
self.dequeued = list()
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
self.doLock = threading.Lock()
|
|
|
|
self.queue = ImprovedQueue()
|
2020-08-28 03:44:28 +02:00
|
|
|
self.num = 0
|
2020-08-22 22:31:00 +02:00
|
|
|
self.start()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add(cls, user, task):
|
|
|
|
ins = cls.getInstance()
|
2020-08-28 03:44:28 +02:00
|
|
|
ins.num += 1
|
2021-04-22 19:14:56 +02:00
|
|
|
log.debug("Add Task for user: {}: {}".format(user, task))
|
2020-08-24 05:00:23 +02:00
|
|
|
ins.queue.put(QueuedTask(
|
2020-08-28 03:44:28 +02:00
|
|
|
num=ins.num,
|
2020-08-24 05:00:23 +02:00
|
|
|
user=user,
|
|
|
|
added=datetime.now(),
|
|
|
|
task=task,
|
|
|
|
))
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tasks(self):
|
|
|
|
with self.doLock:
|
2020-08-28 03:44:28 +02:00
|
|
|
tasks = self.queue.to_list() + self.dequeued
|
|
|
|
return sorted(tasks, key=lambda x: x.num)
|
|
|
|
|
2020-08-29 18:24:12 +02:00
|
|
|
def cleanup_tasks(self):
|
|
|
|
with self.doLock:
|
|
|
|
dead = []
|
|
|
|
alive = []
|
|
|
|
for x in self.dequeued:
|
|
|
|
(dead if x.task.dead else alive).append(x)
|
|
|
|
|
|
|
|
# if the ones that we need to keep are within the trigger, do nothing else
|
|
|
|
delta = len(self.dequeued) - len(dead)
|
|
|
|
if delta > TASK_CLEANUP_TRIGGER:
|
|
|
|
ret = alive
|
|
|
|
else:
|
|
|
|
# otherwise, lop off the oldest dead tasks until we hit the target trigger
|
|
|
|
ret = sorted(dead, key=lambda x: x.task.end_time)[-TASK_CLEANUP_TRIGGER:] + alive
|
|
|
|
|
|
|
|
self.dequeued = sorted(ret, key=lambda x: x.num)
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
# Main thread loop starting the different tasks
|
|
|
|
def run(self):
|
|
|
|
main_thread = _get_main_thread()
|
|
|
|
while main_thread.is_alive():
|
2020-08-30 19:49:45 +02:00
|
|
|
try:
|
|
|
|
# this blocks until something is available. This can cause issues when the main thread dies - this
|
|
|
|
# thread will remain alive. We implement a timeout to unblock every second which allows us to check if
|
|
|
|
# the main thread is still alive.
|
|
|
|
# We don't use a daemon here because we don't want the tasks to just be abruptly halted, leading to
|
|
|
|
# possible file / database corruption
|
|
|
|
item = self.queue.get(timeout=1)
|
2020-12-08 13:34:15 +01:00
|
|
|
except queue.Empty:
|
2020-08-30 19:49:45 +02:00
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
|
2020-08-22 22:31:00 +02:00
|
|
|
with self.doLock:
|
2020-08-28 03:44:28 +02:00
|
|
|
# add to list so that in-progress tasks show up
|
2020-08-24 05:00:23 +02:00
|
|
|
self.dequeued.append(item)
|
|
|
|
|
2020-08-29 18:24:12 +02:00
|
|
|
# once we hit our trigger, start cleaning up dead tasks
|
|
|
|
if len(self.dequeued) > TASK_CLEANUP_TRIGGER:
|
|
|
|
self.cleanup_tasks()
|
|
|
|
|
2020-08-23 04:44:28 +02:00
|
|
|
# sometimes tasks (like Upload) don't actually have work to do and are created as already finished
|
2020-08-28 03:44:28 +02:00
|
|
|
if item.task.stat is STAT_WAITING:
|
2020-08-24 03:51:44 +02:00
|
|
|
# CalibreTask.start() should wrap all exceptions in it's own error handling
|
2020-08-28 03:44:28 +02:00
|
|
|
item.task.start(self)
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
self.queue.task_done()
|
|
|
|
|
2020-08-28 03:44:28 +02:00
|
|
|
|
2020-08-24 03:21:55 +02:00
|
|
|
class CalibreTask:
|
|
|
|
__metaclass__ = abc.ABCMeta
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
def __init__(self, message):
|
|
|
|
self._progress = 0
|
|
|
|
self.stat = STAT_WAITING
|
|
|
|
self.error = None
|
|
|
|
self.start_time = None
|
|
|
|
self.end_time = None
|
|
|
|
self.message = message
|
2020-08-24 05:00:23 +02:00
|
|
|
self.id = uuid.uuid4()
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def run(self, worker_thread):
|
|
|
|
"""Provides the caller some human-readable name for this class"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def name(self):
|
|
|
|
"""Provides the caller some human-readable name for this class"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def start(self, *args):
|
|
|
|
self.start_time = datetime.now()
|
2020-08-23 04:44:28 +02:00
|
|
|
self.stat = STAT_STARTED
|
2020-08-24 03:51:44 +02:00
|
|
|
|
|
|
|
# catch any unhandled exceptions in a task and automatically fail it
|
|
|
|
try:
|
|
|
|
self.run(*args)
|
2021-04-04 19:40:34 +02:00
|
|
|
except Exception as ex:
|
|
|
|
self._handleError(str(ex))
|
|
|
|
log.debug_or_exception(ex)
|
2020-08-24 03:51:44 +02:00
|
|
|
|
2020-08-22 22:31:00 +02:00
|
|
|
self.end_time = datetime.now()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def stat(self):
|
|
|
|
return self._stat
|
|
|
|
|
|
|
|
@stat.setter
|
|
|
|
def stat(self, x):
|
|
|
|
self._stat = x
|
|
|
|
|
|
|
|
@property
|
|
|
|
def progress(self):
|
|
|
|
return self._progress
|
|
|
|
|
|
|
|
@progress.setter
|
|
|
|
def progress(self, x):
|
2020-08-24 03:51:44 +02:00
|
|
|
if not 0 <= x <= 1:
|
|
|
|
raise ValueError("Task progress should within [0, 1] range")
|
2020-08-22 22:31:00 +02:00
|
|
|
self._progress = x
|
|
|
|
|
|
|
|
@property
|
|
|
|
def error(self):
|
|
|
|
return self._error
|
|
|
|
|
|
|
|
@error.setter
|
|
|
|
def error(self, x):
|
|
|
|
self._error = x
|
|
|
|
|
|
|
|
@property
|
|
|
|
def runtime(self):
|
|
|
|
return (self.end_time or datetime.now()) - self.start_time
|
|
|
|
|
2020-08-28 03:44:28 +02:00
|
|
|
@property
|
|
|
|
def dead(self):
|
|
|
|
"""Determines whether or not this task can be garbage collected
|
|
|
|
|
|
|
|
We have a separate dictating this because there may be certain tasks that want to override this
|
|
|
|
"""
|
|
|
|
# By default, we're good to clean a task if it's "Done"
|
|
|
|
return self.stat in (STAT_FINISH_SUCCESS, STAT_FAIL)
|
|
|
|
|
2021-07-05 18:55:54 +02:00
|
|
|
'''@progress.setter
|
|
|
|
def progress(self, x):
|
|
|
|
if x > 1:
|
|
|
|
x = 1
|
|
|
|
if x < 0:
|
|
|
|
x = 0
|
|
|
|
self._progress = x'''
|
2020-08-22 22:31:00 +02:00
|
|
|
|
|
|
|
def _handleError(self, error_message):
|
|
|
|
self.stat = STAT_FAIL
|
|
|
|
self.progress = 1
|
|
|
|
self.error = error_message
|
|
|
|
|
|
|
|
def _handleSuccess(self):
|
|
|
|
self.stat = STAT_FINISH_SUCCESS
|
|
|
|
self.progress = 1
|