Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions backend/djangoindia/bg_tasks/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from functools import wraps

from django.utils import timezone
from djangoindia.db.models.common import BackgroundTaskLog


def log_bg_task(func):
"""
A decorator to log background task details.

Usage:
-------
>>> from djangoindia.utils.common import log_bg_task
>>> @log_bg_task
>>> def my_celery_task(*args, **kwargs):
>>> # Task logic here
>>> pass
"""

@wraps(func)
def wrapper(*args, **kwargs):
task_log = BackgroundTaskLog.objects.create(
name=func.__name__,
start_datetime=timezone.now(),
args=list(args),
kwargs=kwargs
)
try:
task_log.status = BackgroundTaskLog.StatusChoices.STARTED
result = func(*args, **kwargs)
task_log.status = BackgroundTaskLog.StatusChoices.SUCCESSFULL
task_log.log = str(result)
except Exception as e:
task_log.status = BackgroundTaskLog.StatusChoices.FAILURE
task_log.log = str(e)
finally:
task_log.end_datetime = timezone.now()
task_log.save()
return wrapper
8 changes: 8 additions & 0 deletions backend/djangoindia/db/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Update,
User,
Volunteer,
BackgroundTaskLog
)

from .forms import (
Expand Down Expand Up @@ -684,3 +685,10 @@ def recipient_count(self, obj):
return obj.recipient.count()

recipient_count.short_description = "Recipients"


@admin.register(BackgroundTaskLog)
class BackgroundTaskLogAdmin(admin.ModelAdmin):
list_display = ("name", "start_datetime", "end_datetime", "args", "kwargs", "status", "log")
list_filter = ("name", "status", "start_datetime", "end_datetime")
search_fields = ("name", "status")
32 changes: 32 additions & 0 deletions backend/djangoindia/db/migrations/0003_backgroundtasklog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 4.2.5 on 2025-04-15 15:26

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('db', '0002_alter_eventregistration_options_and_more'),
]

operations = [
migrations.CreateModel(
name='BackgroundTaskLog',
fields=[
('id', models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=256)),
('start_datetime', models.DateTimeField()),
('end_datetime', models.DateTimeField(blank=True, null=True)),
('args', models.JSONField(blank=True, default=dict, help_text='default arguments present in task', null=True)),
('kwargs', models.JSONField(blank=True, default=dict, help_text='Additional arguments', null=True)),
('status', models.CharField(choices=[('successful', 'Successfull'), ('failure', 'Failure'), ('pending', 'Pending'), ('started', 'Started')], default='pending', max_length=32)),
('log', models.CharField(blank=True, max_length=512, null=True)),
],
options={
'abstract': False,
},
),
]
2 changes: 2 additions & 0 deletions backend/djangoindia/db/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .update import Update
from .user import SocialLoginConnection, User
from .volunteer import Volunteer
from .common import BackgroundTaskLog


__all__ = [
Expand All @@ -20,4 +21,5 @@
"Sponsor",
"EventUserRegistration",
"EventCommunication",
"BackgroundTaskLog",
]
22 changes: 22 additions & 0 deletions backend/djangoindia/db/models/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from .base import BaseModel
from django.db import models


class BackgroundTaskLog(BaseModel):
"""
model to hold the information about the background task that runs.
"""
class StatusChoices(models.TextChoices):
SUCCESSFULL = "successful"
FAILURE = "failure"
PENDING = "pending"
STARTED = "started"


name = models.CharField(max_length=256, null=False, blank=False)
start_datetime = models.DateTimeField(null=False)
end_datetime = models.DateTimeField(null=True, blank=True)
args = models.JSONField(default=dict, null=True, blank=True, help_text="default arguments present in task")
kwargs = models.JSONField(default=dict, null=True, blank=True, help_text="Additional arguments")
status = models.CharField(max_length=32, default=StatusChoices.PENDING, null=False, blank=False, choices=StatusChoices.choices)
log = models.CharField(max_length=512, null=True, blank=True)