selfprivacy-rest-api/selfprivacy_api/backup/tasks.py

58 lines
1.5 KiB
Python
Raw Normal View History

2023-07-20 15:24:26 +00:00
"""
The tasks module contains the worker tasks that are used to back up and restore
"""
2023-04-10 16:35:35 +00:00
from datetime import datetime
from selfprivacy_api.graphql.common_types.backup import RestoreStrategy
2023-04-19 15:09:06 +00:00
from selfprivacy_api.models.backup.snapshot import Snapshot
2023-03-29 11:45:52 +00:00
from selfprivacy_api.utils.huey import huey
from selfprivacy_api.services.service import Service
from selfprivacy_api.backup import Backups
2023-04-10 16:35:35 +00:00
2023-07-20 15:24:26 +00:00
def validate_datetime(dt: datetime) -> bool:
"""
Validates that the datetime passed in is timezone-aware.
"""
2023-04-10 16:35:35 +00:00
if dt.timetz is None:
raise ValueError(
"""
2023-07-20 15:24:26 +00:00
huey passed in the timezone-unaware time!
2023-04-10 16:35:35 +00:00
Post it in support chat or maybe try uncommenting a line above
"""
)
return Backups.is_time_to_backup(dt)
2023-03-29 11:45:52 +00:00
# huey tasks need to return something
@huey.task()
def start_backup(service: Service) -> bool:
2023-07-20 15:24:26 +00:00
"""
The worker task that starts the backup process.
"""
2023-03-29 11:45:52 +00:00
Backups.back_up(service)
return True
2023-04-10 16:35:35 +00:00
2023-04-19 15:09:06 +00:00
@huey.task()
def restore_snapshot(
snapshot: Snapshot,
strategy: RestoreStrategy = RestoreStrategy.DOWNLOAD_VERIFY_OVERWRITE,
) -> bool:
2023-07-20 15:24:26 +00:00
"""
The worker task that starts the restore process.
"""
Backups.restore_snapshot(snapshot, strategy)
2023-04-19 15:09:06 +00:00
return True
2023-04-10 16:35:35 +00:00
@huey.periodic_task(validate_datetime=validate_datetime)
def automatic_backup():
2023-07-20 15:24:26 +00:00
"""
The worker periodic task that starts the automatic backup process.
"""
2023-04-10 16:35:35 +00:00
time = datetime.now()
for service in Backups.services_to_back_up(time):
start_backup(service)