Skip to content

Commit

Permalink
Try to solve issues with open db connections
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMStraub committed May 4, 2024
1 parent 1a5bea9 commit c6dbfd4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gramps_webapi/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
#

# make sure to match this version with the one in apispec.yaml
__version__ = "2.3.0"
__version__ = "2.3.1"
118 changes: 74 additions & 44 deletions gramps_webapi/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,16 @@ def import_file(
db_handle = get_db_outside_request(
tree=tree, view_private=True, readonly=True, user_id=user_id
)
run_import(
db_handle=db_handle,
file_name=file_name,
extension=extension.lower(),
delete=delete,
task=self,
)
try:
run_import(
db_handle=db_handle,
file_name=file_name,
extension=extension.lower(),
delete=delete,
task=self,
)
finally:
db_handle.close()
update_usage_people(tree=tree, user_id=user_id)
_search_reindex_incremental(
tree=tree,
Expand All @@ -204,8 +207,14 @@ def export_db(
db_handle = get_db_outside_request(
tree=tree, view_private=view_private, readonly=True, user_id=user_id
)
prepared_options = prepare_options(db_handle, options)
file_name, file_type = run_export(db_handle, extension, prepared_options, task=self)
try:
prepared_options = prepare_options(db_handle, options)
file_name, file_type = run_export(
db_handle, extension, prepared_options, task=self
)
finally:
db_handle.close()

extension = file_type.lstrip(".")
return {
"file_name": file_name,
Expand All @@ -227,12 +236,16 @@ def generate_report(
db_handle = get_db_outside_request(
tree=tree, view_private=view_private, readonly=True, user_id=user_id
)
file_name, file_type = run_report(
db_handle=db_handle,
report_id=report_id,
report_options=options,
language=locale,
)
try:
file_name, file_type = run_report(
db_handle=db_handle,
report_id=report_id,
report_options=options,
language=locale,
)
finally:
db_handle.close()

return {
"file_name": file_name,
"file_type": file_type,
Expand All @@ -248,17 +261,21 @@ def export_media(
db_handle = get_db_outside_request(
tree=tree, view_private=view_private, readonly=True, user_id=user_id
)
media_handler = get_media_handler(db_handle, tree=tree)
export_path = current_app.config["EXPORT_DIR"]
os.makedirs(export_path, exist_ok=True)
file_name = f"{uuid.uuid4()}.zip"
zip_filename = os.path.join(export_path, file_name)
media_handler.create_file_archive(
db_handle=db_handle,
zip_filename=zip_filename,
include_private=view_private,
progress_cb=progress_callback_count(self),
)
try:
media_handler = get_media_handler(db_handle, tree=tree)
export_path = current_app.config["EXPORT_DIR"]
os.makedirs(export_path, exist_ok=True)
file_name = f"{uuid.uuid4()}.zip"
zip_filename = os.path.join(export_path, file_name)
media_handler.create_file_archive(
db_handle=db_handle,
zip_filename=zip_filename,
include_private=view_private,
progress_cb=progress_callback_count(self),
)
finally:
db_handle.close()

file_size = os.path.getsize(zip_filename)
return {
"file_name": file_name,
Expand All @@ -275,14 +292,17 @@ def import_media_archive(
db_handle = get_db_outside_request(
tree=tree, view_private=True, readonly=True, user_id=user_id
)
importer = MediaImporter(
tree=tree,
user_id=user_id,
db_handle=db_handle,
file_name=file_name,
delete=delete,
)
result = importer(progress_cb=progress_callback_count(self))
try:
importer = MediaImporter(
tree=tree,
user_id=user_id,
db_handle=db_handle,
file_name=file_name,
delete=delete,
)
result = importer(progress_cb=progress_callback_count(self))
finally:
db_handle.close()
return result


Expand All @@ -299,10 +319,13 @@ def media_ocr(
db_handle = get_db_outside_request(
tree=tree, view_private=view_private, readonly=True, user_id=user_id
)
handler = get_media_handler(db_handle, tree).get_file_handler(
handle, db_handle=db_handle
)
return handler.get_ocr(lang=lang, output_format=output_format)
try:
handler = get_media_handler(db_handle, tree).get_file_handler(
handle, db_handle=db_handle
)
return handler.get_ocr(lang=lang, output_format=output_format)
finally:
db_handle.close()


@shared_task(bind=True)
Expand All @@ -311,7 +334,10 @@ def check_repair_database(self, tree: str, user_id: str):
db_handle = get_db_outside_request(
tree=tree, view_private=True, readonly=False, user_id=user_id
)
return check_database(db_handle, progress_cb=progress_callback_count(self))
try:
return check_database(db_handle, progress_cb=progress_callback_count(self))
finally:
db_handle.close()


@shared_task(bind=True)
Expand All @@ -328,9 +354,13 @@ def delete_objects(
db_handle = get_db_outside_request(
tree=tree, view_private=True, readonly=False, user_id=user_id
)
delete_all_objects(
db_handle=db_handle,
namespaces=namespaces,
progress_cb=progress_callback_count(self),
)
try:
delete_all_objects(
db_handle=db_handle,
namespaces=namespaces,
progress_cb=progress_callback_count(self),
)
finally:
db_handle.close()

update_usage_people(tree=tree, user_id=user_id)
5 changes: 4 additions & 1 deletion gramps_webapi/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,10 @@ def update_usage_people(
readonly=True,
user_id=user_id,
)
usage_people = db_handle.get_number_of_people()
try:
usage_people = db_handle.get_number_of_people()
finally:
db_handle.close()
set_tree_usage(tree, usage_people=usage_people)
return usage_people

Expand Down
2 changes: 1 addition & 1 deletion gramps_webapi/data/apispec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ info:
* More about Gramps and the numerous features it provides for genealogists can be found at https://gramps-project.org
version: "2.3.0" # make sure to match this version with the one in _version.py
version: "2.3.1" # make sure to match this version with the one in _version.py
license:
name: "GNU Affero General Public License v3.0"
url: "http://www.gnu.org/licenses/agpl-3.0.html"
Expand Down

0 comments on commit c6dbfd4

Please sign in to comment.