diff --git a/backend/djangoindia/bg_tasks/common.py b/backend/djangoindia/bg_tasks/common.py new file mode 100644 index 00000000..e53dd03c --- /dev/null +++ b/backend/djangoindia/bg_tasks/common.py @@ -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 diff --git a/backend/djangoindia/db/admin.py b/backend/djangoindia/db/admin.py index ddbd5152..894ab105 100644 --- a/backend/djangoindia/db/admin.py +++ b/backend/djangoindia/db/admin.py @@ -33,6 +33,7 @@ Update, User, Volunteer, + BackgroundTaskLog ) from .forms import ( @@ -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") diff --git a/backend/djangoindia/db/migrations/0003_backgroundtasklog.py b/backend/djangoindia/db/migrations/0003_backgroundtasklog.py new file mode 100644 index 00000000..b681a35e --- /dev/null +++ b/backend/djangoindia/db/migrations/0003_backgroundtasklog.py @@ -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, + }, + ), + ] diff --git a/backend/djangoindia/db/models/__init__.py b/backend/djangoindia/db/models/__init__.py index aeff3c02..c9cf4272 100644 --- a/backend/djangoindia/db/models/__init__.py +++ b/backend/djangoindia/db/models/__init__.py @@ -4,6 +4,7 @@ from .update import Update from .user import SocialLoginConnection, User from .volunteer import Volunteer +from .common import BackgroundTaskLog __all__ = [ @@ -20,4 +21,5 @@ "Sponsor", "EventUserRegistration", "EventCommunication", + "BackgroundTaskLog", ] diff --git a/backend/djangoindia/db/models/common.py b/backend/djangoindia/db/models/common.py new file mode 100644 index 00000000..f5d2600c --- /dev/null +++ b/backend/djangoindia/db/models/common.py @@ -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)