authentik/passbook/core/tasks.py

27 lines
914 B
Python
Raw Normal View History

"""passbook core tasks"""
from django.utils.timezone import now
2019-10-01 09:24:10 +01:00
from structlog import get_logger
from passbook.core.models import ExpiringModel
2020-10-16 10:28:54 +01:00
from passbook.lib.tasks import MonitoredTask, TaskResult, TaskResultStatus
from passbook.root.celery import CELERY_APP
LOGGER = get_logger()
2019-12-31 11:51:16 +00:00
2020-10-16 10:28:54 +01:00
@CELERY_APP.task(bind=True, base=MonitoredTask)
def clean_expired_models(self: MonitoredTask):
"""Remove expired objects"""
2020-10-16 10:28:54 +01:00
messages = []
for cls in ExpiringModel.__subclasses__():
cls: ExpiringModel
amount, _ = (
cls.objects.all()
.exclude(expiring=False)
.exclude(expiring=True, expires__gt=now())
.delete()
)
LOGGER.debug("Deleted expired models", model=cls, amount=amount)
2020-10-16 10:28:54 +01:00
messages.append(f"Deleted {amount} expired {cls._meta.verbose_name_plural}")
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, messages))