fix: jobs

This commit is contained in:
dettlaff 2023-06-18 08:37:27 +04:00
parent d6c4d458f7
commit 83cf7cbd6f
6 changed files with 51 additions and 38 deletions

View file

@ -6,9 +6,12 @@ from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.graphql.mutations.mutation_interface import (
GenericMutationReturn,
MutationReturnInterface,
GenericJobButationReturn,
)
import selfprivacy_api.actions.system as system_actions
from selfprivacy_api.graphql.common_types.jobs import job_to_api_job
from selfprivacy_api.jobs.nix_collect_garbage import start_nix_collect_garbage
@strawberry.type
@ -126,3 +129,14 @@ class SystemMutations:
message=f"Failed to pull repository changes:\n{result.data}",
code=500,
)
@strawberry.mutation(permission_classes=[IsAuthenticated])
def nix_collect_garbage(self) -> GenericJobButationReturn:
job = start_nix_collect_garbage()
return GenericJobButationReturn(
success=True,
code=200,
message="Cleaning started..,",
job=job_to_api_job(job),
)

View file

@ -67,8 +67,8 @@ def move_folder(
try:
data_path.mkdir(mode=0o750, parents=True, exist_ok=True)
except Exception as e:
print(f"Error creating data path: {e}")
except Exception as error:
print(f"Error creating data path: {error}")
return
try:

View file

@ -1,8 +1,10 @@
import re
import subprocess
from selfprivacy_api.jobs import JobStatus, Jobs
from selfprivacy_api.utils.huey import huey
from selfprivacy_api.jobs import JobStatus, Jobs, Job
# from selfprivacy_api.utils.huey import huey
COMPLETED_WITH_ERROR = "Completed with an error"
@ -22,25 +24,9 @@ def run_nix_collect_garbage():
).stdout
def set_job_status_wrapper(Jobs, job):
def set_job_status(status, progress, status_text, result="Default result"):
Jobs.update(
job=job,
status=status,
progress=progress,
status_text=status_text,
result=result,
)
return set_job_status
def parse_line(line):
pattern = re.compile(r"[+-]?\d+\.\d+ \w+(?= freed)")
match = re.search(
pattern,
line,
)
match = re.search(pattern, line)
if match is None:
return (
@ -60,9 +46,9 @@ def parse_line(line):
def stream_process(
job,
stream,
total_dead_packages,
set_job_status,
):
completed_packages = 0
@ -71,7 +57,8 @@ def stream_process(
completed_packages += 1
percent = int((completed_packages / total_dead_packages) * 100)
set_job_status(
Jobs.update(
job=job,
status=JobStatus.RUNNING,
progress=percent,
status_text="Сleaning...",
@ -79,7 +66,8 @@ def stream_process(
elif "store paths deleted," in line:
status = parse_line(line)
set_job_status(
Jobs.update(
job=job,
status=status[0],
progress=status[1],
status_text=status[2],
@ -95,13 +83,9 @@ def get_dead_packages(output):
return dead, percent
@huey.task()
def nix_collect_garbage(
job,
):
set_job_status = set_job_status_wrapper(Jobs, job)
set_job_status(
def calculate_and_clear_dead_packages(job: Jobs):
Jobs.update(
job=job,
status=JobStatus.RUNNING,
progress=0,
status_text="Сalculate the number of dead packages...",
@ -112,7 +96,8 @@ def nix_collect_garbage(
)
if dead_packages == 0:
set_job_status(
Jobs.update(
job=job,
status=JobStatus.FINISHED,
progress=100,
status_text="Nothing to clear",
@ -120,10 +105,21 @@ def nix_collect_garbage(
)
return
set_job_status(
Jobs.update(
job=job,
status=JobStatus.RUNNING,
progress=0,
status_text=f"Found {dead_packages} packages to remove!",
)
stream_process(run_nix_collect_garbage(), package_equal_to_percent, set_job_status)
stream_process(job, run_nix_collect_garbage(), package_equal_to_percent)
def start_nix_collect_garbage() -> Job:
job = Jobs.add(
type_id="maintenance.collect_nix_garbage",
name="Collect garbage",
description=".",
)
calculate_and_clear_dead_packages(job=job)
return job

View file

@ -25,8 +25,8 @@ class CheckForFailedBindsMigration(Migration):
):
return True
return False
except Exception as e:
print(e)
except Exception as error:
print(error)
return False
def migrate(self):
@ -43,6 +43,6 @@ class CheckForFailedBindsMigration(Migration):
with WriteUserData() as userdata:
userdata["useBinds"] = False
print("Done")
except Exception as e:
print(e)
except Exception as error:
print(error)
print("Error mounting volume")

View file

@ -5,6 +5,7 @@ let
portalocker
pytz
pytest
pytest-asyncio
pytest-mock
pytest-datadir
huey

View file

@ -20,6 +20,8 @@ from selfprivacy_api.jobs.nix_collect_garbage import (
RESULT_WAS_NOT_FOUND_ERROR,
)
pytest_plugins = ("pytest_asyncio",)
OUTPUT_PRINT_DEAD = """
finding garbage collector roots...