1
+ import logging
1
2
from datetime import timedelta
2
3
3
4
from celery import shared_task
4
- from core .automation .state_machine import TicketStateMachine
5
+ from core .automation .rule_runner import RuleEngine
5
6
from core .constants import Status
6
7
from core .models import Agent , Ticket
7
8
from django .db import transaction
8
9
from django .db .models import Q
9
10
from django .utils import timezone
10
11
12
+ logger = logging .getLogger (__name__ )
13
+
11
14
12
15
@shared_task (bind = True )
13
16
def process_ticket_queue (self ):
@@ -37,7 +40,7 @@ def process_ticket_queue(self):
37
40
agent .max_customers -= 1
38
41
agent .save ()
39
42
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 } " )
41
44
42
45
43
46
@shared_task (bind = True )
@@ -51,12 +54,27 @@ def delete_completed_tickets(self):
51
54
deleted_count , _ = Ticket .objects .filter (
52
55
status = Status .COMPLETED | Q (Status .CLOSED ), updated_at__lte = cutoff
53
56
).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." )
55
58
56
59
60
+ # Todo: implement the batch processing[!important][Memmory Issue]
57
61
@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
0 commit comments