From e9af95d4650dde45f05b5568c389255e0b80ec71 Mon Sep 17 00:00:00 2001 From: Maurane GLAUDE Date: Tue, 8 Oct 2024 09:48:47 +0200 Subject: [PATCH] refactor(storage): refactor `_try_archive_studies` and fix minor issues --- antarest/core/config.py | 2 +- antarest/study/storage/auto_archive_service.py | 13 +++++++------ .../storage/business/test_autoarchive_service.py | 15 +++++---------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/antarest/core/config.py b/antarest/core/config.py index 56022904c8..b88e4f44bb 100644 --- a/antarest/core/config.py +++ b/antarest/core/config.py @@ -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, ), ) diff --git a/antarest/study/storage/auto_archive_service.py b/antarest/study/storage/auto_archive_service.py index a3a1525d69..b293fc1207 100644 --- a/antarest/study/storage/auto_archive_service.py +++ b/antarest/study/storage/auto_archive_service.py @@ -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 = [ @@ -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: @@ -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) ) @@ -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", diff --git a/tests/storage/business/test_autoarchive_service.py b/tests/storage/business/test_autoarchive_service.py index 7d1b0c94a7..eb04adf2b2 100644 --- a/tests/storage/business/test_autoarchive_service.py +++ b/tests/storage/business/test_autoarchive_service.py @@ -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() @@ -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), )