1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-01 07:36:20 +00:00
calibre-web/cps/tasks/clean.py

65 lines
2.3 KiB
Python
Raw Normal View History

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/>.
import datetime
2023-12-04 18:26:43 +00:00
from flask_babel import lazy_gettext as N_
from sqlalchemy.sql.expression import or_
2023-12-04 18:26:43 +00:00
from cps import logger, file_helper, ub
2023-12-04 18:26:43 +00:00
from cps.services.worker import CalibreTask
class TaskClean(CalibreTask):
2023-12-04 18:26:43 +00:00
def __init__(self, task_message=N_('Delete temp folder contents')):
super(TaskClean, self).__init__(task_message)
2023-12-04 18:26:43 +00:00
self.log = logger.create()
self.app_db_session = ub.get_new_session_instance()
2023-12-04 18:26:43 +00:00
def run(self, worker_thread):
# 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))
# 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()
self.app_db_session.remove()
2023-12-04 18:26:43 +00:00
@property
def name(self):
return "Clean up"
2023-12-04 18:26:43 +00:00
@property
def is_cancellable(self):
return False