Compare commits
2 Commits
a11627da7d
...
3d4d05ff11
Author | SHA1 | Date |
---|---|---|
Houkime | 3d4d05ff11 | |
Houkime | d7316f8e79 |
|
@ -52,24 +52,36 @@ class Backups:
|
|||
def enable_autobackup(service: Service):
|
||||
Storage.set_autobackup(service)
|
||||
|
||||
@staticmethod
|
||||
def _service_ids_to_back_up(time: datetime) -> List[str]:
|
||||
services = Storage.services_with_autobackup()
|
||||
return [id for id in services if Backups.is_time_to_backup_service(id, time)]
|
||||
|
||||
# untestable until the dummy service is registered
|
||||
@staticmethod
|
||||
def services_to_back_up(time: datetime) -> List[Service]:
|
||||
result = []
|
||||
for id in Backups._service_ids_to_back_up(time):
|
||||
service = get_service_by_id(id)
|
||||
if service is None:
|
||||
raise ValueError("Cannot look up a service scheduled for backup!")
|
||||
result.append(service)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def is_time_to_backup(time: datetime) -> bool:
|
||||
"""
|
||||
Intended as a time validator for huey cron scheduler of automatic backups
|
||||
"""
|
||||
|
||||
enabled_services = Storage.services_with_autobackup()
|
||||
for service_id in enabled_services:
|
||||
if Backups.is_time_to_backup_service(service_id, time):
|
||||
return True
|
||||
return False
|
||||
return Backups._service_ids_to_back_up(time) != []
|
||||
|
||||
@staticmethod
|
||||
def is_time_to_backup_service(service_id: str, time: datetime):
|
||||
period = Backups.autobackup_period_minutes()
|
||||
if period is None:
|
||||
return False
|
||||
if not Storage.is_autobackup_set_by_name(service_id) is None:
|
||||
if not Storage.is_autobackup_set(service_id):
|
||||
return False
|
||||
|
||||
last_backup = Storage.get_last_backup_time(service_id)
|
||||
|
|
|
@ -122,7 +122,7 @@ class Storage:
|
|||
redis.delete(Storage.__autobackup_key(service.get_id()))
|
||||
|
||||
@staticmethod
|
||||
def is_autobackup_set(service_name: str):
|
||||
def is_autobackup_set(service_name: str) -> bool:
|
||||
return redis.exists(Storage.__autobackup_key(service_name))
|
||||
|
||||
@staticmethod
|
||||
|
|
|
@ -1,9 +1,31 @@
|
|||
from datetime import datetime
|
||||
|
||||
from selfprivacy_api.utils.huey import huey
|
||||
from selfprivacy_api.services.service import Service
|
||||
from selfprivacy_api.backup import Backups
|
||||
|
||||
|
||||
def validate_datetime(dt: datetime):
|
||||
# dt = datetime.now(timezone.utc)
|
||||
if dt.timetz is None:
|
||||
raise ValueError(
|
||||
"""
|
||||
huey passed in the timezone-unaware time!
|
||||
Post it in support chat or maybe try uncommenting a line above
|
||||
"""
|
||||
)
|
||||
return Backups.is_time_to_backup(dt)
|
||||
|
||||
|
||||
# huey tasks need to return something
|
||||
@huey.task()
|
||||
def start_backup(service: Service) -> bool:
|
||||
Backups.back_up(service)
|
||||
return True
|
||||
|
||||
|
||||
@huey.periodic_task(validate_datetime=validate_datetime)
|
||||
def automatic_backup():
|
||||
time = datetime.now()
|
||||
for service in Backups.services_to_back_up(time):
|
||||
start_backup(service)
|
||||
|
|
|
@ -198,6 +198,17 @@ def test_autobackup_enable_service(backups, dummy_service):
|
|||
assert not Backups.is_autobackup_enabled(dummy_service)
|
||||
|
||||
|
||||
def test_autobackup_enable_service_storage(backups, dummy_service):
|
||||
assert len(Storage.services_with_autobackup()) == 0
|
||||
|
||||
Backups.enable_autobackup(dummy_service)
|
||||
assert len(Storage.services_with_autobackup()) == 1
|
||||
assert Storage.services_with_autobackup()[0] == dummy_service.get_id()
|
||||
|
||||
Backups.disable_autobackup(dummy_service)
|
||||
assert len(Storage.services_with_autobackup()) == 0
|
||||
|
||||
|
||||
def test_set_autobackup_period(backups):
|
||||
assert Backups.autobackup_period_minutes() is None
|
||||
|
||||
|
@ -220,6 +231,70 @@ def test_set_autobackup_period(backups):
|
|||
assert Backups.autobackup_period_minutes() is None
|
||||
|
||||
|
||||
def test_no_default_autobackup(backups, dummy_service):
|
||||
now = datetime.now(timezone.utc)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert not Backups.is_time_to_backup(now)
|
||||
|
||||
|
||||
def test_autobackup_timer_periods(backups, dummy_service):
|
||||
now = datetime.now(timezone.utc)
|
||||
backup_period = 13 # minutes
|
||||
|
||||
Backups.enable_autobackup(dummy_service)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert not Backups.is_time_to_backup(now)
|
||||
|
||||
Backups.set_autobackup_period_minutes(backup_period)
|
||||
assert Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert Backups.is_time_to_backup(now)
|
||||
|
||||
Backups.set_autobackup_period_minutes(0)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert not Backups.is_time_to_backup(now)
|
||||
|
||||
|
||||
def test_autobackup_timer_enabling(backups, dummy_service):
|
||||
now = datetime.now(timezone.utc)
|
||||
backup_period = 13 # minutes
|
||||
|
||||
Backups.set_autobackup_period_minutes(backup_period)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert not Backups.is_time_to_backup(now)
|
||||
|
||||
Backups.enable_autobackup(dummy_service)
|
||||
assert Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert Backups.is_time_to_backup(now)
|
||||
|
||||
Backups.disable_autobackup(dummy_service)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert not Backups.is_time_to_backup(now)
|
||||
|
||||
|
||||
def test_autobackup_timing(backups, dummy_service):
|
||||
backup_period = 13 # minutes
|
||||
now = datetime.now(timezone.utc)
|
||||
|
||||
Backups.enable_autobackup(dummy_service)
|
||||
Backups.set_autobackup_period_minutes(backup_period)
|
||||
assert Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert Backups.is_time_to_backup(now)
|
||||
|
||||
Backups.back_up(dummy_service)
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), now)
|
||||
assert not Backups.is_time_to_backup(now)
|
||||
|
||||
past = datetime.now(timezone.utc) - timedelta(minutes=1)
|
||||
assert not Backups.is_time_to_backup_service(dummy_service.get_id(), past)
|
||||
assert not Backups.is_time_to_backup(past)
|
||||
|
||||
future = datetime.now(timezone.utc) + timedelta(minutes=backup_period + 2)
|
||||
assert Backups.is_time_to_backup_service(dummy_service.get_id(), future)
|
||||
assert Backups.is_time_to_backup(future)
|
||||
|
||||
|
||||
# Storage
|
||||
def test_snapshots_caching(backups, dummy_service):
|
||||
Backups.back_up(dummy_service)
|
||||
|
|
Loading…
Reference in New Issue