Skip to content

Commit

Permalink
Merge pull request #195 from man-group/fix-gridfs-deletion
Browse files Browse the repository at this point in the history
Fix GridFS item deletion
  • Loading branch information
ceallen authored Dec 13, 2024
2 parents 9b0d4d6 + 947e31e commit 6531886
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
11 changes: 5 additions & 6 deletions notebooker/serialization/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,14 @@ def n_all_results_for_report_name(self, report_name: str) -> int:
def delete_result(self, job_id: AnyStr) -> Dict[str, Any]:
result = self._get_raw_check_result(job_id)
status = JobStatus.from_string(result["status"])
gridfs_filenames = []
if status == JobStatus.DONE:
gridfs_filenames = load_files_from_gridfs(self.result_data_store, result, do_read=False)
elif status in (JobStatus.ERROR, JobStatus.TIMEOUT, JobStatus.CANCELLED):
gridfs_filenames = [_error_info_filename(job_id)]
gridfs_filenames = load_files_from_gridfs(self.result_data_store, result, do_read=False)
if status in (JobStatus.ERROR, JobStatus.TIMEOUT, JobStatus.CANCELLED):
gridfs_filenames.append(_error_info_filename(job_id))
self.update_check_status(job_id, JobStatus.DELETED)
for filename in gridfs_filenames:
logger.info(f"Deleting {filename}")
self.result_data_store.delete(filename)
for grid_out in self.result_data_store.find({"filename": filename}):
self.result_data_store.delete(grid_out._id)
return {"deleted_result_document": result, "gridfs_filenames": gridfs_filenames}


Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[tool.black]
line-length = 120
target-version = ['py36', 'py37', 'py38']
target-version = ['py36', 'py37', 'py38']

[tool.ruff]
line-length = 120
9 changes: 4 additions & 5 deletions tests/integration/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import uuid

import pytest
from gridfs.errors import NoFile

from notebooker.constants import JobStatus, NotebookResultComplete, NotebookResultError
from notebooker.serialization.serialization import initialize_serializer_from_config
Expand Down Expand Up @@ -83,9 +82,9 @@ def test_delete(bson_library, webapp_config):
assert result is not None
assert result.raw_html == raw_html
deleted_stuff = serializer.delete_result(job_id)
assert [r for r in serializer.result_data_store.list()] == []
for filename in deleted_stuff["gridfs_filenames"]:
with pytest.raises(NoFile):
serializer.result_data_store.get(filename)
assert serializer.result_data_store.exists(filename=filename) is False
assert serializer.get_check_result(job_id) is None


Expand All @@ -111,7 +110,7 @@ def test_delete_error(bson_library, webapp_config, job_status):
result = serializer.get_check_result(job_id)
assert result is not None
deleted_stuff = serializer.delete_result(job_id)
assert [r for r in serializer.result_data_store.list()] == []
for filename in deleted_stuff["gridfs_filenames"]:
with pytest.raises(NoFile):
serializer.result_data_store.get(filename)
assert serializer.result_data_store.exists(filename=filename) is False
assert serializer.get_check_result(job_id) is None

0 comments on commit 6531886

Please sign in to comment.