Session no longer expires on commit

This commit is contained in:
Ozzieisaacs 2020-12-08 08:01:42 +01:00
parent 5e3618716d
commit d15d252af7
2 changed files with 18 additions and 12 deletions

View File

@ -426,18 +426,19 @@ class CalibreDB():
# instances alive once they reach the end of their respective scopes
instances = WeakSet()
def __init__(self):
def __init__(self, expire_on_commit=False):
""" Initialize a new CalibreDB session
"""
self.session = None
if self._init:
self.initSession()
self.initSession(expire_on_commit)
self.instances.add(self)
def initSession(self):
def initSession(self, expire_on_commit):
self.session = self.session_factory()
self.session.expire_on_commit = expire_on_commit
self.update_title_sort(self.config)
@classmethod

View File

@ -9,7 +9,7 @@ from shutil import copyfile
from sqlalchemy.exc import SQLAlchemyError
from cps.services.worker import CalibreTask
from cps import calibre_db, db
from cps import db
from cps import logger, config
from cps.subproc_wrapper import process_open
from flask_babel import gettext as _
@ -33,8 +33,10 @@ class TaskConvert(CalibreTask):
def run(self, worker_thread):
self.worker_thread = worker_thread
if config.config_use_google_drive:
cur_book = calibre_db.get_book(self.bookid)
data = calibre_db.get_book_format(self.bookid, self.settings['old_book_format'])
worker_db = db.CalibreDB()
cur_book = worker_db.get_book(self.bookid)
data = worker_db.get_book_format(self.bookid, self.settings['old_book_format'])
worker_db.session.close()
df = gdriveutils.getFileFromEbooksFolder(cur_book.path,
data.name + "." + self.settings['old_book_format'].lower())
if df:
@ -71,7 +73,7 @@ class TaskConvert(CalibreTask):
def _convert_ebook_format(self):
error_message = None
local_session = db.CalibreDB().session
local_db = db.CalibreDB()
file_path = self.file_path
book_id = self.bookid
format_old_ext = u'.' + self.settings['old_book_format'].lower()
@ -82,10 +84,11 @@ class TaskConvert(CalibreTask):
# this will allow send to kindle workflow to continue to work
if os.path.isfile(file_path + format_new_ext):
log.info("Book id %d already converted to %s", book_id, format_new_ext)
cur_book = calibre_db.get_book(book_id)
cur_book = local_db.session.get_book(book_id)
self.results['path'] = file_path
self.results['title'] = cur_book.title
self._handleSuccess()
local_db.session.close()
return os.path.basename(file_path + format_new_ext)
else:
log.info("Book id %d - target format of %s does not exist. Moving forward with convert.",
@ -105,18 +108,19 @@ class TaskConvert(CalibreTask):
check, error_message = self._convert_calibre(file_path, format_old_ext, format_new_ext)
if check == 0:
cur_book = calibre_db.get_book(book_id)
cur_book = local_db.get_book(book_id)
if os.path.isfile(file_path + format_new_ext):
# self.db_queue.join()
new_format = db.Data(name=cur_book.data[0].name,
book_format=self.settings['new_book_format'].upper(),
book=book_id, uncompressed_size=os.path.getsize(file_path + format_new_ext))
try:
local_session.merge(new_format)
local_session.commit()
local_db.session.merge(new_format)
local_db.session.commit()
except SQLAlchemyError as e:
local_session.rollback()
local_db.rollback()
log.error("Database error: %s", e)
local_db.session.close()
return
self.results['path'] = cur_book.path
self.results['title'] = cur_book.title
@ -125,6 +129,7 @@ class TaskConvert(CalibreTask):
return os.path.basename(file_path + format_new_ext)
else:
error_message = _('%(format)s format not found on disk', format=format_new_ext.upper())
local_db.session.close()
log.info("ebook converter failed with error while converting book")
if not error_message:
error_message = _('Ebook converter failed with unknown error')