Skip to content

Commit

Permalink
refactor(variant_study_service): re-use snapshot clearing task feature
Browse files Browse the repository at this point in the history
  • Loading branch information
maugde committed Oct 7, 2024
1 parent 289b9c5 commit 627c295
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
16 changes: 5 additions & 11 deletions antarest/study/storage/auto_archive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,12 @@ def _try_archive_studies(self) -> None:
)

def _try_clear_snapshot(self) -> None:
snapshot_old_time = datetime.datetime.utcnow() - datetime.timedelta(
days=self.config.storage.variant_snapshot_lifespan_days
)
# get variants
variants = self.study_service.repository.get_all(
study_filter=StudyFilter(variant=True, access_permissions=AccessPermissions(is_admin=True))
# convert days in hours
hours_of_lifespan = hours=self.config.storage.variant_snapshot_lifespan_days * 24

self.study_service.storage_service.variant_study_service.clear_all_snapshots(
hours_of_lifespan
)
# check their date
for variant in variants:
if (variant.last_access or variant.updated_at) < snapshot_old_time:
# clear ones that are too old
self.study_service.storage_service.variant_study_service.clear_snapshot(variant.id)

def _loop(self) -> None:
while True:
Expand Down
15 changes: 15 additions & 0 deletions tests/storage/business/test_autoarchive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ 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=0),
),
VariantStudy(
id="g",
updated_at=now - datetime.timedelta(days=1),
),
VariantStudy(
id="h",
last_access=now - datetime.timedelta(days=2)
)
]
)
db_session.commit()
Expand Down Expand Up @@ -97,4 +109,7 @@ def test_auto_archival(tmp_path: Path):
# 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))

# Test auto snapshot clearing
auto_archive_service._try_clear_snapshot()

# Check if variant snapshots with date older than variant_snapshot_lifespan_days argument are cleared

0 comments on commit 627c295

Please sign in to comment.