Files
bugsink/bsmain/tasks.py
Klaas van Schelven 5895253803 Count view: async slow counts
when you count, it's usually because there are many, so this
extra complication is probaly going to be required
2025-04-17 22:03:23 +02:00

27 lines
722 B
Python

from django.apps import apps
from django.utils import timezone
from snappea.decorators import shared_task
from bugsink.transaction import durable_atomic, immediate_atomic
from .models import CachedModelCount
@shared_task
def count_model(app_label, model_name):
ModelClass = apps.get_model(app_label, model_name)
# separate transaction for the expensive counting
with durable_atomic():
count = ModelClass.objects.count()
with immediate_atomic():
CachedModelCount.objects.update_or_create(
app_label=app_label,
model_name=model_name,
defaults={
'count': count,
'last_updated': timezone.now(),
},
)