Skip to content

Commit 9b27a4a

Browse files
authored
Merge pull request #4 from omidcodes/004-add-celery-background-task
add celery task to update logs/task_activity.log on each Task object …
2 parents 9b20c3b + 059d199 commit 9b27a4a

9 files changed

Lines changed: 49 additions & 4 deletions

File tree

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,8 @@ cython_debug/
175175

176176

177177
# vscode
178-
.vscode
178+
.vscode
179+
180+
# logs folder remains, but the contents will be ignored.
181+
logs/*
182+
!logs/.gitkeep

logs/.gitkeep

Whitespace-only changes.

requirements.txt

-4 Bytes
Binary file not shown.

taskflow_api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .celery import app as celery_app
2+
3+
__all__ = ['celery_app']

taskflow_api/celery.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
from celery import Celery
3+
4+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'taskflow_api.settings')
5+
6+
app = Celery('taskflow_api')
7+
app.config_from_object('django.conf:settings', namespace='CELERY')
8+
app.autodiscover_tasks()

taskflow_api/settings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from pathlib import Path
32
from decouple import config, Csv
43

@@ -126,4 +125,7 @@
126125
}
127126
}
128127

129-
CELERY_BROKER_URL = config('CELERY_BROKER_URL', default='amqp://guest:guest@localhost:5672//')
128+
CELERY_BROKER_URL = config('CELERY_BROKER_URL', default='amqp://guest:guest@localhost:5672//')
129+
130+
CELERY_ACCEPT_CONTENT = ['json']
131+
CELERY_TASK_SERIALIZER = 'json'

tasks/apps.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.apps import AppConfig
22

3-
43
class TasksConfig(AppConfig):
54
default_auto_field = 'django.db.models.BigAutoField'
65
name = 'tasks'
6+
7+
def ready(self):
8+
import tasks.signals

tasks/signals.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.db.models.signals import post_save
2+
from django.dispatch import receiver
3+
from tasks.models import Task
4+
from tasks.tasks import log_task_action
5+
6+
7+
@receiver(post_save, sender=Task)
8+
def task_created_handler(sender, instance, created, **kwargs):
9+
10+
if created:
11+
log_task_action.delay(instance.id, "created")

tasks/tasks.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from celery import shared_task
2+
from django.utils.timezone import now
3+
from tasks.models import Task
4+
5+
@shared_task
6+
def log_task_action(task_id, action):
7+
"""Log task ID and title to a file."""
8+
try:
9+
task = Task.objects.get(id=task_id)
10+
log_line = f"[{now()}] Logging Celery Task : Task #{task.id} ('{task.title}') was {action} using celery.\n"
11+
except Task.DoesNotExist:
12+
log_line = f"[{now()}] ERROR: Task {task_id} not found for action '{action}'\n"
13+
14+
with open("logs/task_activity.log", "a") as f:
15+
f.write(log_line)

0 commit comments

Comments
 (0)