Skip to content

Commit f085f85

Browse files
committed
Async task configuration for automation
1 parent 3685c51 commit f085f85

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

management/core/api/viewset.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class TicketAssignview(APIView):
237237
API endpoint to assign a ticket to an available agent.
238238
239239
Request Method: GET
240-
URL: /app/ticket/<id>/assign/
240+
URL: /app/ticket/`str:id`/assign/
241241
242242
Path Parameter:
243243
- id: Ticket ID to be assigned.
@@ -330,7 +330,20 @@ def get(self, request, id, format=None):
330330

331331
class TicketReopen(APIView):
332332
"""
333-
API endpoint to reopen a ticket.
333+
API endpoint to reopen the ticket.
334+
335+
Request Method: GET
336+
URL: /app/ticket/`str:id`/reopen/
337+
338+
Path Parameter:
339+
- id: Ticket ID to be reopened.
340+
341+
Responses:
342+
- 200 OK:
343+
- Ticket successfully reopend.
344+
- 400 Bad Request: Invalid ticket ID or Status is not closed.
345+
- 404 Not Found: Ticket with the given ID does not exist.
346+
- 500 Internal Server Error: Server side error.
334347
"""
335348

336349
permission_classes = [IsAuthenticated | CanEditOwnOrAdmin]

management/core/tasks.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import logging
12
from datetime import timedelta
23

34
from celery import shared_task
4-
from core.automation.state_machine import TicketStateMachine
5+
from core.automation.rule_runner import RuleEngine
56
from core.constants import Status
67
from core.models import Agent, Ticket
78
from django.db import transaction
89
from django.db.models import Q
910
from django.utils import timezone
1011

12+
logger = logging.getLogger(__name__)
13+
1114

1215
@shared_task(bind=True)
1316
def process_ticket_queue(self):
@@ -37,7 +40,7 @@ def process_ticket_queue(self):
3740
agent.max_customers -= 1
3841
agent.save()
3942

40-
print(f"Ticket {ticket.ticket_id} assigned to {agent.user.username}")
43+
logger.info(f"Ticket {ticket.ticket_id} assigned to {agent.user.username}")
4144

4245

4346
@shared_task(bind=True)
@@ -51,12 +54,27 @@ def delete_completed_tickets(self):
5154
deleted_count, _ = Ticket.objects.filter(
5255
status=Status.COMPLETED | Q(Status.CLOSED), updated_at__lte=cutoff
5356
).delete()
54-
print(f"Deleted {deleted_count} completed tickets older than 60 days.")
57+
logger.info(f"Deleted {deleted_count} completed tickets older than 60 days.")
5558

5659

60+
# Todo: implement the batch processing[!important][Memmory Issue]
5761
@shared_task(bind=True)
58-
def process_state_changed(ticket_id, new_status):
59-
with transaction.atomic:
60-
ticket_id = Ticket.objects.select_for_update.get(ticket_id=ticket_id)
61-
state_machine = TicketStateMachine(ticket_id)
62-
return state_machine.transition_to(new_status)
62+
def apply_rules_to_all_tickets():
63+
"""
64+
Celery task to apply RuleEngine to all tickets in the database.
65+
"""
66+
tickets = Ticket.objects.all()
67+
results = []
68+
69+
for ticket in tickets:
70+
try:
71+
engine = RuleEngine(ticket.ticket_id)
72+
result = engine.run()
73+
results.append({"ticket_id": ticket.ticket_id, "result": result})
74+
logger.info(f"Rules applied to ticket {ticket.ticket_id}: {result}")
75+
except Exception as e:
76+
logger.exception(
77+
f"Failed to apply rules to ticket {ticket.ticket_id}: {str(e)}"
78+
)
79+
80+
return results

management/main/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,8 @@
192192
"task": "core.tasks.delete_completed_tickets",
193193
"schedule": crontab(hour=3, minute=0),
194194
},
195+
"automation_rule_on_every_tickets": {
196+
"task": "core.tasks.apply_rules_to_all_tickets",
197+
"schedule": crontab(hour=1, minute=0),
198+
},
195199
}

0 commit comments

Comments
 (0)