Skip to content

Commit

Permalink
refactor(storage): refactor _try_archive_studies and fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maugde committed Oct 8, 2024
1 parent 277216e commit e9af95d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion antarest/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def from_dict(cls, data: JSON) -> "StorageConfig":
auto_archive_sleeping_time=data.get("auto_archive_sleeping_time", defaults.auto_archive_sleeping_time),
auto_archive_max_parallel=data.get("auto_archive_max_parallel", defaults.auto_archive_max_parallel),
snapshot_retention_days=data.get(
"snapshot_variant_lifespan_days",
"snapshot_retention_days",
defaults.snapshot_retention_days,
),
)
Expand Down
13 changes: 7 additions & 6 deletions antarest/study/storage/auto_archive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ def __init__(self, study_service: StudyService, config: Config):
self.max_parallel = self.config.storage.auto_archive_max_parallel

def _try_archive_studies(self) -> None:
"""
Archive old studies
Clear old variant snapshots
"""
old_date = datetime.datetime.utcnow() - datetime.timedelta(days=self.config.storage.auto_archive_threshold_days)
with db():
# in this part full `Read` rights over studies are granted to this function
studies: t.Sequence[Study] = self.study_service.repository.get_all(
study_filter=StudyFilter(managed=True, access_permissions=AccessPermissions(is_admin=True))
study_filter=StudyFilter(
managed=True,
access_permissions=AccessPermissions(is_admin=True))
)
# list of study IDs and boolean indicating if it's a raw study (True) or a variant (False)
study_ids_to_archive = [
Expand Down Expand Up @@ -73,9 +79,6 @@ def _try_archive_studies(self) -> None:
study_id,
params=RequestParameters(DEFAULT_ADMIN_USER),
)
self.study_service.storage_service.variant_study_service.clear_snapshot(
self.study_service.get_study(study_id)
)
except TaskAlreadyRunning:
pass
except Exception as e:
Expand All @@ -84,7 +87,6 @@ def _try_archive_studies(self) -> None:
exc_info=e,
)

def _try_clear_snapshot(self) -> None:
self.study_service.storage_service.variant_study_service.clear_all_snapshots(
datetime.timedelta(days=self.config.storage.snapshot_retention_days)
)
Expand All @@ -93,7 +95,6 @@ def _loop(self) -> None:
while True:
try:
self._try_archive_studies()
self._try_clear_snapshot()
except Exception as e:
logger.error(
"Unexpected error happened when processing auto archive service loop",
Expand Down
15 changes: 5 additions & 10 deletions tests/storage/business/test_autoarchive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def test_auto_archival(tmp_path: Path):
id="e",
updated_at=now - datetime.timedelta(days=61),
),
VariantStudy(
id="f",
updated_at=now - datetime.timedelta(days=1),
),
]
)
db_session.commit()
Expand All @@ -86,19 +90,10 @@ def test_auto_archival(tmp_path: Path):
# Check that the raw study "d" was about to be archived but failed because the task was already running
study_service.archive.assert_called_once_with("d", params=RequestParameters(DEFAULT_ADMIN_USER))

# Check that the snapshot of the variant study "e" is cleared
study_service.storage_service.variant_study_service.clear_snapshot.assert_called_once()
calls = study_service.storage_service.variant_study_service.clear_snapshot.call_args_list
assert len(calls) == 1
clear_snapshot_call = calls[0]
actual_study = clear_snapshot_call[0][0]
assert actual_study.id == "e"

# Check that the variant outputs are deleted for the variant study "e"
study_service.archive_outputs.assert_called_once_with("e", params=RequestParameters(DEFAULT_ADMIN_USER))

# Check if the `clear_all_snapshots` method was called with default values
auto_archive_service._try_clear_snapshot()
study_service.storage_service.variant_study_service.clear_all_snapshots.assert_called_once_with(
datetime.timedelta(hours=24),
datetime.timedelta(days=1),
)

0 comments on commit e9af95d

Please sign in to comment.