Skip to content

Commit

Permalink
feat(auto_archive_service): add snapshot clearing task and option in …
Browse files Browse the repository at this point in the history
…`config`
  • Loading branch information
maugde committed Oct 7, 2024
1 parent d120a68 commit 289b9c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions antarest/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class StorageConfig:
auto_archive_dry_run: bool = False
auto_archive_sleeping_time: int = 3600
auto_archive_max_parallel: int = 5
variant_snapshot_lifespan_days: int = 1

@classmethod
def from_dict(cls, data: JSON) -> "StorageConfig":
Expand Down Expand Up @@ -184,6 +185,10 @@ def from_dict(cls, data: JSON) -> "StorageConfig":
auto_archive_dry_run=data.get("auto_archive_dry_run", defaults.auto_archive_dry_run),
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),
variant_snapshot_lifespan_days=data.get(
"snapshot_variant_lifespan_days",
defaults.variant_snapshot_lifespan_days,
),
)


Expand Down
20 changes: 18 additions & 2 deletions antarest/study/storage/auto_archive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def _try_archive_studies(self) -> None:
study_ids_to_archive = [
(study.id, isinstance(study, RawStudy))
for study in studies
if (study.last_access or study.updated_at) < old_date
and (isinstance(study, VariantStudy) or not study.archived)
if ((study.last_access or study.updated_at) < old_date)
and isinstance(study, VariantStudy)
or not study.archived
]
for study_id, is_raw_study in study_ids_to_archive[0 : self.max_parallel]:
try:
Expand Down Expand Up @@ -84,10 +85,25 @@ def _try_archive_studies(self) -> None:
exc_info=e,
)

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))
)
# 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:
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
2 changes: 2 additions & 0 deletions tests/storage/business/test_autoarchive_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ 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))

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

0 comments on commit 289b9c5

Please sign in to comment.