Skip to content

Commit

Permalink
Age limit parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
islean committed Jan 11, 2024
1 parent 8ce1ab2 commit 268aa98
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
8 changes: 3 additions & 5 deletions cg/cli/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,19 @@ def clean_flow_cells(context: CGConfig, dry_run: bool):

@clean.command("retrieved-spring-files")
@click.option(
"--days-since-retrieval",
"--age-limit",
type=int,
default=7,
help="Clean all Spring files which were retrieved more than given amount of days ago.",
)
@DRY_RUN
@click.pass_obj
def clean_retrieved_spring_files(context: CGConfig, days_since_retrieval: int, dry_run: bool):
def clean_retrieved_spring_files(context: CGConfig, age_limit: int, dry_run: bool):
"""Clean Spring files which were retrieved more than given amount of days ago."""
clean_retrieved_spring_files_api = CleanRetrievedSpringFilesAPI(
housekeeper_api=context.housekeeper_api, dry_run=dry_run
)
clean_retrieved_spring_files_api.clean_retrieved_spring_files(
days_since_retrieval=days_since_retrieval
)
clean_retrieved_spring_files_api.clean_retrieved_spring_files(age_limit)


def _get_confirm_question(bundle, file_obj) -> str:
Expand Down
8 changes: 4 additions & 4 deletions cg/meta/clean/clean_retrieved_spring_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def __init__(self, housekeeper_api: HousekeeperAPI, dry_run: bool):
self.housekeeper_api: HousekeeperAPI = housekeeper_api
self.dry_run = dry_run

def _get_files_to_remove(self, days_since_retrieval: int) -> list[File]:
def _get_files_to_remove(self, age_limit: int) -> list[File]:
"""Returns all Spring files which were retrieved more than given amount of days ago."""
return self.housekeeper_api.get_spring_files_retrieved_before(
date=datetime.now() - timedelta(days=days_since_retrieval)
date=datetime.now() - timedelta(days=age_limit)
)

def _unlink_files(self, files_to_unlink: list[File]) -> None:
Expand All @@ -30,12 +30,12 @@ def _unlink_files(self, files_to_unlink: list[File]) -> None:
LOG.info(f"Unlinking {file_path}")
Path(file_path).unlink(missing_ok=True)

def clean_retrieved_spring_files(self, days_since_retrieval: int):
def clean_retrieved_spring_files(self, age_limit: int):
"""Removes Spring files retrieved more than given amount of days ago from Hasta,
and resets retrieval data in Housekeeper."""

LOG.info("Starting cleaning of retrieved Spring files.")
files_to_remove: list[File] = self._get_files_to_remove(days_since_retrieval)
files_to_remove: list[File] = self._get_files_to_remove(age_limit)
self._unlink_files(files_to_remove)
if not self.dry_run:
self.housekeeper_api.reset_retrieved_archive_data(files_to_remove)
Expand Down
4 changes: 1 addition & 3 deletions tests/meta/clean/test_clean_retrieved_spring_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def test_get_files_to_remove(
# WHEN getting files to remove when cleaning retrieved spring files
files_to_remove: list[
File
] = populated_clean_retrieved_spring_files_api_dry_run._get_files_to_remove(
days_since_retrieval=7
)
] = populated_clean_retrieved_spring_files_api_dry_run._get_files_to_remove(age_limit=7)

# THEN only the file with an old enough 'retrieved_at' should be returned
assert [file.path for file in files_to_remove] == [
Expand Down

0 comments on commit 268aa98

Please sign in to comment.