2023-07-20 15:24:26 +00:00
|
|
|
"""
|
|
|
|
The tasks module contains the worker tasks that are used to back up and restore
|
|
|
|
"""
|
2023-07-20 16:39:10 +00:00
|
|
|
from datetime import datetime, timezone
|
2023-04-10 16:35:35 +00:00
|
|
|
|
2023-11-17 15:22:21 +00:00
|
|
|
from selfprivacy_api.graphql.common_types.backup import (
|
|
|
|
RestoreStrategy,
|
|
|
|
BackupReason,
|
|
|
|
)
|
2023-07-07 12:49:52 +00:00
|
|
|
|
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-09-01 10:41:27 +00:00
|
|
|
from huey import crontab
|
2023-03-29 11:45:52 +00:00
|
|
|
from selfprivacy_api.services.service import Service
|
|
|
|
from selfprivacy_api.backup import Backups
|
2023-11-17 15:22:21 +00:00
|
|
|
from selfprivacy_api.jobs import Jobs, JobStatus, Job
|
|
|
|
|
2023-03-29 11:45:52 +00:00
|
|
|
|
2023-09-01 10:41:27 +00:00
|
|
|
SNAPSHOT_CACHE_TTL_HOURS = 6
|
|
|
|
|
2023-09-01 10:41:27 +00:00
|
|
|
SNAPSHOT_CACHE_TTL_HOURS = 6
|
|
|
|
|
2023-04-10 16:35:35 +00:00
|
|
|
|
2023-07-20 15:24:26 +00:00
|
|
|
def validate_datetime(dt: datetime) -> bool:
|
|
|
|
"""
|
2023-09-01 10:41:27 +00:00
|
|
|
Validates that it is time to back up.
|
|
|
|
Also ensures that the timezone-aware time is used.
|
2023-07-20 15:24:26 +00:00
|
|
|
"""
|
2023-07-20 16:39:10 +00:00
|
|
|
if dt.tzinfo is None:
|
|
|
|
return Backups.is_time_to_backup(dt.replace(tzinfo=timezone.utc))
|
2023-04-10 16:35:35 +00:00
|
|
|
return Backups.is_time_to_backup(dt)
|
|
|
|
|
|
|
|
|
2023-03-29 11:45:52 +00:00
|
|
|
# huey tasks need to return something
|
|
|
|
@huey.task()
|
2023-08-21 11:11:56 +00:00
|
|
|
def start_backup(
|
|
|
|
service: Service, reason: BackupReason = BackupReason.EXPLICIT
|
|
|
|
) -> bool:
|
2023-07-20 15:24:26 +00:00
|
|
|
"""
|
|
|
|
The worker task that starts the backup process.
|
|
|
|
"""
|
2023-08-21 11:11:56 +00:00
|
|
|
Backups.back_up(service, reason)
|
2023-03-29 11:45:52 +00:00
|
|
|
return True
|
2023-04-10 16:35:35 +00:00
|
|
|
|
|
|
|
|
2023-11-17 15:22:21 +00:00
|
|
|
@huey.task()
|
2023-11-17 15:53:57 +00:00
|
|
|
def prune_autobackup_snapshots(job: Job) -> bool:
|
2023-11-17 15:39:21 +00:00
|
|
|
"""
|
|
|
|
Remove all autobackup snapshots that do not fit into quotas set
|
|
|
|
"""
|
2023-11-17 15:22:21 +00:00
|
|
|
Jobs.update(job, JobStatus.RUNNING)
|
|
|
|
try:
|
2023-11-17 15:39:21 +00:00
|
|
|
Backups.prune_all_autosnaps()
|
2023-11-17 15:22:21 +00:00
|
|
|
except Exception as e:
|
|
|
|
Jobs.update(job, JobStatus.ERROR, error=type(e).__name__ + ":" + str(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
Jobs.update(job, JobStatus.FINISHED)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-04-19 15:09:06 +00:00
|
|
|
@huey.task()
|
2023-07-07 12:49:52 +00:00
|
|
|
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.
|
|
|
|
"""
|
2023-07-07 12:49:52 +00:00
|
|
|
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-07-20 16:42:44 +00:00
|
|
|
time = datetime.utcnow().replace(tzinfo=timezone.utc)
|
2023-04-10 16:35:35 +00:00
|
|
|
for service in Backups.services_to_back_up(time):
|
2023-08-21 11:11:56 +00:00
|
|
|
start_backup(service, BackupReason.AUTO)
|
2023-09-01 10:41:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@huey.periodic_task(crontab(hour=SNAPSHOT_CACHE_TTL_HOURS))
|
|
|
|
def reload_snapshot_cache():
|
|
|
|
Backups.force_snapshot_cache_reload()
|