mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-02-16 14:24:29 +00:00
fix: jobs
This commit is contained in:
parent
d6c4d458f7
commit
83cf7cbd6f
|
@ -6,9 +6,12 @@ from selfprivacy_api.graphql import IsAuthenticated
|
||||||
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
||||||
GenericMutationReturn,
|
GenericMutationReturn,
|
||||||
MutationReturnInterface,
|
MutationReturnInterface,
|
||||||
|
GenericJobButationReturn,
|
||||||
)
|
)
|
||||||
|
|
||||||
import selfprivacy_api.actions.system as system_actions
|
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
|
@strawberry.type
|
||||||
|
@ -126,3 +129,14 @@ class SystemMutations:
|
||||||
message=f"Failed to pull repository changes:\n{result.data}",
|
message=f"Failed to pull repository changes:\n{result.data}",
|
||||||
code=500,
|
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),
|
||||||
|
)
|
||||||
|
|
|
@ -67,8 +67,8 @@ def move_folder(
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data_path.mkdir(mode=0o750, parents=True, exist_ok=True)
|
data_path.mkdir(mode=0o750, parents=True, exist_ok=True)
|
||||||
except Exception as e:
|
except Exception as error:
|
||||||
print(f"Error creating data path: {e}")
|
print(f"Error creating data path: {error}")
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from selfprivacy_api.jobs import JobStatus, Jobs
|
from selfprivacy_api.jobs import JobStatus, Jobs, Job
|
||||||
from selfprivacy_api.utils.huey import huey
|
|
||||||
|
|
||||||
|
# from selfprivacy_api.utils.huey import huey
|
||||||
|
|
||||||
|
|
||||||
COMPLETED_WITH_ERROR = "Completed with an error"
|
COMPLETED_WITH_ERROR = "Completed with an error"
|
||||||
|
@ -22,25 +24,9 @@ def run_nix_collect_garbage():
|
||||||
).stdout
|
).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):
|
def parse_line(line):
|
||||||
pattern = re.compile(r"[+-]?\d+\.\d+ \w+(?= freed)")
|
pattern = re.compile(r"[+-]?\d+\.\d+ \w+(?= freed)")
|
||||||
match = re.search(
|
match = re.search(pattern, line)
|
||||||
pattern,
|
|
||||||
line,
|
|
||||||
)
|
|
||||||
|
|
||||||
if match is None:
|
if match is None:
|
||||||
return (
|
return (
|
||||||
|
@ -60,9 +46,9 @@ def parse_line(line):
|
||||||
|
|
||||||
|
|
||||||
def stream_process(
|
def stream_process(
|
||||||
|
job,
|
||||||
stream,
|
stream,
|
||||||
total_dead_packages,
|
total_dead_packages,
|
||||||
set_job_status,
|
|
||||||
):
|
):
|
||||||
completed_packages = 0
|
completed_packages = 0
|
||||||
|
|
||||||
|
@ -71,7 +57,8 @@ def stream_process(
|
||||||
completed_packages += 1
|
completed_packages += 1
|
||||||
percent = int((completed_packages / total_dead_packages) * 100)
|
percent = int((completed_packages / total_dead_packages) * 100)
|
||||||
|
|
||||||
set_job_status(
|
Jobs.update(
|
||||||
|
job=job,
|
||||||
status=JobStatus.RUNNING,
|
status=JobStatus.RUNNING,
|
||||||
progress=percent,
|
progress=percent,
|
||||||
status_text="Сleaning...",
|
status_text="Сleaning...",
|
||||||
|
@ -79,7 +66,8 @@ def stream_process(
|
||||||
|
|
||||||
elif "store paths deleted," in line:
|
elif "store paths deleted," in line:
|
||||||
status = parse_line(line)
|
status = parse_line(line)
|
||||||
set_job_status(
|
Jobs.update(
|
||||||
|
job=job,
|
||||||
status=status[0],
|
status=status[0],
|
||||||
progress=status[1],
|
progress=status[1],
|
||||||
status_text=status[2],
|
status_text=status[2],
|
||||||
|
@ -95,13 +83,9 @@ def get_dead_packages(output):
|
||||||
return dead, percent
|
return dead, percent
|
||||||
|
|
||||||
|
|
||||||
@huey.task()
|
def calculate_and_clear_dead_packages(job: Jobs):
|
||||||
def nix_collect_garbage(
|
Jobs.update(
|
||||||
job,
|
job=job,
|
||||||
):
|
|
||||||
set_job_status = set_job_status_wrapper(Jobs, job)
|
|
||||||
|
|
||||||
set_job_status(
|
|
||||||
status=JobStatus.RUNNING,
|
status=JobStatus.RUNNING,
|
||||||
progress=0,
|
progress=0,
|
||||||
status_text="Сalculate the number of dead packages...",
|
status_text="Сalculate the number of dead packages...",
|
||||||
|
@ -112,7 +96,8 @@ def nix_collect_garbage(
|
||||||
)
|
)
|
||||||
|
|
||||||
if dead_packages == 0:
|
if dead_packages == 0:
|
||||||
set_job_status(
|
Jobs.update(
|
||||||
|
job=job,
|
||||||
status=JobStatus.FINISHED,
|
status=JobStatus.FINISHED,
|
||||||
progress=100,
|
progress=100,
|
||||||
status_text="Nothing to clear",
|
status_text="Nothing to clear",
|
||||||
|
@ -120,10 +105,21 @@ def nix_collect_garbage(
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
set_job_status(
|
Jobs.update(
|
||||||
|
job=job,
|
||||||
status=JobStatus.RUNNING,
|
status=JobStatus.RUNNING,
|
||||||
progress=0,
|
progress=0,
|
||||||
status_text=f"Found {dead_packages} packages to remove!",
|
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
|
||||||
|
|
|
@ -25,8 +25,8 @@ class CheckForFailedBindsMigration(Migration):
|
||||||
):
|
):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
except Exception as e:
|
except Exception as error:
|
||||||
print(e)
|
print(error)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def migrate(self):
|
def migrate(self):
|
||||||
|
@ -43,6 +43,6 @@ class CheckForFailedBindsMigration(Migration):
|
||||||
with WriteUserData() as userdata:
|
with WriteUserData() as userdata:
|
||||||
userdata["useBinds"] = False
|
userdata["useBinds"] = False
|
||||||
print("Done")
|
print("Done")
|
||||||
except Exception as e:
|
except Exception as error:
|
||||||
print(e)
|
print(error)
|
||||||
print("Error mounting volume")
|
print("Error mounting volume")
|
||||||
|
|
|
@ -5,6 +5,7 @@ let
|
||||||
portalocker
|
portalocker
|
||||||
pytz
|
pytz
|
||||||
pytest
|
pytest
|
||||||
|
pytest-asyncio
|
||||||
pytest-mock
|
pytest-mock
|
||||||
pytest-datadir
|
pytest-datadir
|
||||||
huey
|
huey
|
||||||
|
|
|
@ -20,6 +20,8 @@ from selfprivacy_api.jobs.nix_collect_garbage import (
|
||||||
RESULT_WAS_NOT_FOUND_ERROR,
|
RESULT_WAS_NOT_FOUND_ERROR,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
pytest_plugins = ("pytest_asyncio",)
|
||||||
|
|
||||||
|
|
||||||
OUTPUT_PRINT_DEAD = """
|
OUTPUT_PRINT_DEAD = """
|
||||||
finding garbage collector roots...
|
finding garbage collector roots...
|
||||||
|
|
Loading…
Reference in a new issue