2023-12-04 18:26:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2023 OzzieIsaacs
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2024-07-17 17:59:15 +00:00
|
|
|
import datetime
|
2023-12-04 18:26:43 +00:00
|
|
|
|
|
|
|
from flask_babel import lazy_gettext as N_
|
2024-07-17 17:59:15 +00:00
|
|
|
from sqlalchemy.sql.expression import or_
|
2023-12-04 18:26:43 +00:00
|
|
|
|
2024-07-17 17:59:15 +00:00
|
|
|
from cps import logger, file_helper, ub
|
2023-12-04 18:26:43 +00:00
|
|
|
from cps.services.worker import CalibreTask
|
|
|
|
|
|
|
|
|
2024-07-17 17:59:15 +00:00
|
|
|
class TaskClean(CalibreTask):
|
2023-12-04 18:26:43 +00:00
|
|
|
def __init__(self, task_message=N_('Delete temp folder contents')):
|
2024-07-17 17:59:15 +00:00
|
|
|
super(TaskClean, self).__init__(task_message)
|
2023-12-04 18:26:43 +00:00
|
|
|
self.log = logger.create()
|
2024-07-17 17:59:15 +00:00
|
|
|
self.app_db_session = ub.get_new_session_instance()
|
2023-12-04 18:26:43 +00:00
|
|
|
|
|
|
|
def run(self, worker_thread):
|
2024-07-17 17:59:15 +00:00
|
|
|
# delete temp folder
|
2023-12-04 18:26:43 +00:00
|
|
|
try:
|
|
|
|
file_helper.del_temp_dir()
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
except (PermissionError, OSError) as e:
|
|
|
|
self.log.error("Error deleting temp folder: {}".format(e))
|
2024-07-17 17:59:15 +00:00
|
|
|
# delete expired session keys
|
|
|
|
self.log.debug("Deleted expired session_keys" )
|
|
|
|
expiry = int(datetime.datetime.now().timestamp())
|
|
|
|
try:
|
|
|
|
self.app_db_session.query(ub.User_Sessions).filter(or_(ub.User_Sessions.expiry < expiry,
|
|
|
|
ub.User_Sessions.expiry == None)).delete()
|
|
|
|
self.app_db_session.commit()
|
|
|
|
except Exception as ex:
|
|
|
|
self.log.debug('Error deleting expired session keys: ' + str(ex))
|
|
|
|
self._handleError('Error deleting expired session keys: ' + str(ex))
|
|
|
|
self.app_db_session.rollback()
|
|
|
|
return
|
|
|
|
|
2023-12-04 18:26:43 +00:00
|
|
|
self._handleSuccess()
|
2024-07-17 17:59:15 +00:00
|
|
|
self.app_db_session.remove()
|
2023-12-04 18:26:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2024-07-17 17:59:15 +00:00
|
|
|
return "Clean up"
|
2023-12-04 18:26:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_cancellable(self):
|
|
|
|
return False
|