From 267cdd391b8728326b1c683b61777813188da95c Mon Sep 17 00:00:00 2001
From: Houkime <>
Date: Wed, 11 Oct 2023 17:34:53 +0000
Subject: [PATCH] fix(backup): do not store maybe unpicklable service on the
 queue

---
 selfprivacy_api/backup/tasks.py                  |  7 ++++++-
 .../graphql/mutations/backup_mutations.py        |  2 +-
 tests/test_graphql/test_backup.py                | 16 +++++++++++++---
 3 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/selfprivacy_api/backup/tasks.py b/selfprivacy_api/backup/tasks.py
index 5b36252..a948bff 100644
--- a/selfprivacy_api/backup/tasks.py
+++ b/selfprivacy_api/backup/tasks.py
@@ -11,7 +11,9 @@ from selfprivacy_api.graphql.common_types.backup import (
 from selfprivacy_api.models.backup.snapshot import Snapshot
 from selfprivacy_api.utils.huey import huey
 from huey import crontab
+
 from selfprivacy_api.services.service import Service
+from selfprivacy_api.services import get_service_by_id
 from selfprivacy_api.backup import Backups
 from selfprivacy_api.jobs import Jobs, JobStatus, Job
 
@@ -34,11 +36,14 @@ def validate_datetime(dt: datetime) -> bool:
 # huey tasks need to return something
 @huey.task()
 def start_backup(
-    service: Service, reason: BackupReason = BackupReason.EXPLICIT
+    service_id: str, reason: BackupReason = BackupReason.EXPLICIT
 ) -> bool:
     """
     The worker task that starts the backup process.
     """
+    service = get_service_by_id(service_id)
+    if service is None:
+        raise ValueError(f"No such service: {service_id}")
     Backups.back_up(service, reason)
     return True
 
diff --git a/selfprivacy_api/graphql/mutations/backup_mutations.py b/selfprivacy_api/graphql/mutations/backup_mutations.py
index cc1538e..820564c 100644
--- a/selfprivacy_api/graphql/mutations/backup_mutations.py
+++ b/selfprivacy_api/graphql/mutations/backup_mutations.py
@@ -148,7 +148,7 @@ class BackupMutations:
             )
 
         job = add_backup_job(service)
-        start_backup(service)
+        start_backup(service_id)
 
         return GenericJobMutationReturn(
             success=True,
diff --git a/tests/test_graphql/test_backup.py b/tests/test_graphql/test_backup.py
index 187ce11..bb9e217 100644
--- a/tests/test_graphql/test_backup.py
+++ b/tests/test_graphql/test_backup.py
@@ -14,6 +14,8 @@ from selfprivacy_api.utils.huey import huey
 
 import tempfile
 
+from selfprivacy_api.utils.huey import huey
+
 from tests.test_common import dummy_service, raw_dummy_service
 
 from selfprivacy_api.services import Service, get_all_services
@@ -69,7 +71,15 @@ def backups_local(tmpdir):
 
 @pytest.fixture(scope="function")
 def backups(tmpdir):
-    # for those tests that are supposed to pass with any repo
+    """
+    For those tests that are supposed to pass with
+    both local and cloud repos
+    """
+
+    # Sometimes this is false. Idk why.
+    huey.immediate = True
+    assert huey.immediate is True
+
     Backups.reset()
     if BACKUP_PROVIDER_ENVS["kind"] in os.environ.keys():
         Backups.set_provider_from_envs()
@@ -736,7 +746,7 @@ def simulated_service_stopping_delay(request) -> float:
 def test_backup_service_task(backups, dummy_service, simulated_service_stopping_delay):
     dummy_service.set_delay(simulated_service_stopping_delay)
 
-    handle = start_backup(dummy_service)
+    handle = start_backup(dummy_service.get_id())
     handle(blocking=True)
 
     snaps = Backups.get_snapshots(dummy_service)
@@ -781,7 +791,7 @@ def test_backup_larger_file(backups, dummy_service):
     mega = 2**20
     make_large_file(dir, 100 * mega)
 
-    handle = start_backup(dummy_service)
+    handle = start_backup(dummy_service.get_id())
     handle(blocking=True)
 
     # results will be slightly different on different machines. if someone has troubles with it on their machine, consider dropping this test.