2019-02-26 13:07:47 +00:00
|
|
|
"""passbook core tasks"""
|
2020-10-23 16:32:28 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from boto3.exceptions import Boto3Error
|
|
|
|
from botocore.exceptions import BotoCoreError, ClientError
|
|
|
|
from django.contrib.humanize.templatetags.humanize import naturaltime
|
|
|
|
from django.core import management
|
2020-09-15 10:53:02 +00:00
|
|
|
from django.utils.timezone import now
|
2019-10-01 08:24:10 +00:00
|
|
|
from structlog import get_logger
|
2019-02-26 13:07:47 +00:00
|
|
|
|
2020-07-20 08:57:12 +00:00
|
|
|
from passbook.core.models import ExpiringModel
|
2020-10-16 09:28:54 +00:00
|
|
|
from passbook.lib.tasks import MonitoredTask, TaskResult, TaskResultStatus
|
2019-06-25 16:00:54 +00:00
|
|
|
from passbook.root.celery import CELERY_APP
|
2019-02-26 13:07:47 +00:00
|
|
|
|
2019-10-04 08:08:53 +00:00
|
|
|
LOGGER = get_logger()
|
2019-02-26 13:07:47 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2020-10-16 09:28:54 +00:00
|
|
|
@CELERY_APP.task(bind=True, base=MonitoredTask)
|
|
|
|
def clean_expired_models(self: MonitoredTask):
|
2020-07-20 08:57:12 +00:00
|
|
|
"""Remove expired objects"""
|
2020-10-16 09:28:54 +00:00
|
|
|
messages = []
|
2020-07-20 08:57:12 +00:00
|
|
|
for cls in ExpiringModel.__subclasses__():
|
|
|
|
cls: ExpiringModel
|
2020-09-15 10:53:02 +00:00
|
|
|
amount, _ = (
|
|
|
|
cls.objects.all()
|
|
|
|
.exclude(expiring=False)
|
|
|
|
.exclude(expiring=True, expires__gt=now())
|
|
|
|
.delete()
|
|
|
|
)
|
2020-07-20 08:57:12 +00:00
|
|
|
LOGGER.debug("Deleted expired models", model=cls, amount=amount)
|
2020-10-16 09:28:54 +00:00
|
|
|
messages.append(f"Deleted {amount} expired {cls._meta.verbose_name_plural}")
|
|
|
|
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))
|
2020-10-23 16:32:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@CELERY_APP.task(bind=True, base=MonitoredTask)
|
|
|
|
def backup_database(self: MonitoredTask): # pragma: no cover
|
|
|
|
"""Database backup"""
|
|
|
|
try:
|
|
|
|
start = datetime.now()
|
|
|
|
out = StringIO()
|
|
|
|
management.call_command("dbbackup", quiet=True, stdout=out)
|
|
|
|
self.set_status(
|
|
|
|
TaskResult(
|
|
|
|
TaskResultStatus.SUCCESSFUL,
|
|
|
|
[
|
|
|
|
f"Successfully finished database backup {naturaltime(start)}",
|
|
|
|
out.getvalue(),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
LOGGER.info("Successfully backed up database.")
|
|
|
|
except (IOError, BotoCoreError, ClientError, Boto3Error) as exc:
|
|
|
|
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))
|