2023-04-10 16:35:35 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
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
|
2023-06-07 15:05:58 +00:00
|
|
|
from selfprivacy_api.services import get_service_by_id
|
2023-03-29 11:45:52 +00:00
|
|
|
from selfprivacy_api.services.service import Service
|
|
|
|
from selfprivacy_api.backup import Backups
|
2023-06-07 15:05:58 +00:00
|
|
|
from selfprivacy_api.backup.jobs import add_backup_job, add_restore_job
|
2023-03-29 11:45:52 +00:00
|
|
|
|
2023-04-10 16:35:35 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2023-03-29 11:45:52 +00:00
|
|
|
# huey tasks need to return something
|
|
|
|
@huey.task()
|
|
|
|
def start_backup(service: Service) -> bool:
|
2023-04-24 17:03:56 +00:00
|
|
|
# Backups can create the job, but doing this here
|
|
|
|
# allows us to see the job as queued before it is actually executed
|
2023-04-21 11:59:15 +00:00
|
|
|
add_backup_job(service)
|
2023-04-24 17:03:56 +00:00
|
|
|
|
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) -> bool:
|
2023-06-07 15:05:58 +00:00
|
|
|
add_restore_job(snapshot)
|
|
|
|
|
2023-04-19 15:09:06 +00:00
|
|
|
Backups.restore_snapshot(snapshot)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-04-10 16:35:35 +00:00
|
|
|
@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)
|