From 5c42d7a907e018045612cec82a68af2befa5b54a Mon Sep 17 00:00:00 2001 From: dettlaff Date: Thu, 12 Oct 2023 02:01:31 +0400 Subject: [PATCH] refactor: problems from pr --- .../graphql/mutations/system_mutations.py | 2 +- selfprivacy_api/jobs/nix_collect_garbage.py | 24 ++++++++++++------- .../test_graphql/test_nix_collect_garbage.py | 15 ++---------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/selfprivacy_api/graphql/mutations/system_mutations.py b/selfprivacy_api/graphql/mutations/system_mutations.py index aea3bd5..f286828 100644 --- a/selfprivacy_api/graphql/mutations/system_mutations.py +++ b/selfprivacy_api/graphql/mutations/system_mutations.py @@ -137,6 +137,6 @@ class SystemMutations: return GenericJobMutationReturn( success=True, code=200, - message="Cleaning started..,", + message="Garbage collector started...", job=job_to_api_job(job), ) diff --git a/selfprivacy_api/jobs/nix_collect_garbage.py b/selfprivacy_api/jobs/nix_collect_garbage.py index 9b14084..cd6dfed 100644 --- a/selfprivacy_api/jobs/nix_collect_garbage.py +++ b/selfprivacy_api/jobs/nix_collect_garbage.py @@ -1,25 +1,31 @@ import re import subprocess +from subprocess import Popen, PIPE from selfprivacy_api.utils.huey import huey from selfprivacy_api.jobs import JobStatus, Jobs, Job -COMPLETED_WITH_ERROR = "Completed with an error" -RESULT_WAS_NOT_FOUND_ERROR = "We are sorry, result was not found :(" +COMPLETED_WITH_ERROR = ( + "We are sorry, сompleted with an error, report it to support chat" +) +RESULT_WAS_NOT_FOUND_ERROR = "We are sorry, garbage collection result was not found, something went wrong, report it to support chat :(" CLEAR_COMPLETED = "Cleaning completed." -def run_nix_store_print_dead(): - subprocess.run(["nix-env", "-p", "/nix/var/nix/profiles/system", "--delete-generations old"], check=False) +def run_nix_store_print_dead() -> PIPE: + subprocess.run( + ["nix-env", "-p", "/nix/var/nix/profiles/system", "--delete-generations old"], + check=False, + ) return subprocess.check_output(["nix-store", "--gc", "--print-dead"]).decode( "utf-8" ) -def run_nix_collect_garbage(): +def run_nix_collect_garbage() -> subprocess.Popen: return subprocess.Popen( ["nix-store", "--gc"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT ).stdout @@ -46,7 +52,7 @@ def parse_line(line): ) -def stream_process(job, stream, total_dead_packages): +def process_stream(job, stream, total_dead_packages): completed_packages = 0 prev_progress = 0 @@ -86,7 +92,7 @@ def get_dead_packages(output): @huey.task() -def calculate_and_clear_dead_packages(job: Jobs): +def calculate_and_clear_dead_packages(job: Job): Jobs.update( job=job, status=JobStatus.RUNNING, @@ -94,7 +100,7 @@ def calculate_and_clear_dead_packages(job: Jobs): status_text="Calculate the number of dead packages...", ) - dead_packages, package_equal_to_percent = get_dead_packages( + dead_packages, package_equal_to_percent = get_dead_packages(stream_process run_nix_store_print_dead() ) @@ -115,7 +121,7 @@ def calculate_and_clear_dead_packages(job: Jobs): status_text=f"Found {dead_packages} packages to remove!", ) - stream_process(job, run_nix_collect_garbage(), package_equal_to_percent) + process_stream(job, run_nix_collect_garbage(), package_equal_to_percent) def start_nix_collect_garbage() -> Job: diff --git a/tests/test_graphql/test_nix_collect_garbage.py b/tests/test_graphql/test_nix_collect_garbage.py index 703dbed..340292f 100644 --- a/tests/test_graphql/test_nix_collect_garbage.py +++ b/tests/test_graphql/test_nix_collect_garbage.py @@ -64,16 +64,6 @@ determining live/dead paths... log_event = [] -@pytest.fixture -def mock_set_job_status(mocker): - mock = mocker.patch( - "selfprivacy_api.jobs.nix_collect_garbage.set_job_status_wrapper", - autospec=True, - return_value=set_job_status, - ) - return mock - - @pytest.fixture def job_reset(): Jobs.reset() @@ -118,7 +108,7 @@ mutation CollectGarbage { nixCollectGarbage { success message - code + code } } } @@ -135,7 +125,6 @@ def test_graphql_nix_collect_garbage(authorized_client): assert response.status_code == 200 assert response.json().get("data") is not None - assert response.json()["data"]["system"]["nixCollectGarbage"]["success"] is True assert response.json()["data"]["system"]["nixCollectGarbage"]["message"] is not None - assert response.json()["data"]["system"]["nixCollectGarbage"]["success"] == True + assert response.json()["data"]["system"]["nixCollectGarbage"]["success"] is True assert response.json()["data"]["system"]["nixCollectGarbage"]["code"] == 200