Coverage for celery_tasks/maintenance_tasks.py: 0.00%

25 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-04-15 17:36 +0000

1""" 

2Maintenance Tasks 

3 

4Tasks for routine maintenance operations like cleaning up expired data. 

5These tasks are typically scheduled to run periodically via Celery Beat. 

6""" 

7 

8from celery import Task 

9 

10from celery_app import app 

11from dao.match_dao import SupabaseConnection 

12from dao.match_event_dao import MatchEventDAO 

13from logging_config import get_logger 

14 

15logger = get_logger(__name__) 

16 

17 

18class MaintenanceTask(Task): 

19 """ 

20 Base task class for maintenance operations. 

21 

22 Provides lazy-loaded database access for maintenance tasks. 

23 """ 

24 

25 _connection = None 

26 _match_event_dao = None 

27 

28 @property 

29 def match_event_dao(self): 

30 """Lazy initialization of MatchEventDAO.""" 

31 if self._match_event_dao is None: 

32 self._connection = SupabaseConnection() 

33 self._match_event_dao = MatchEventDAO(self._connection) 

34 return self._match_event_dao 

35 

36 

37@app.task( 

38 bind=True, 

39 base=MaintenanceTask, 

40 name="celery_tasks.maintenance_tasks.cleanup_expired_match_events", 

41) 

42def cleanup_expired_match_events(self): 

43 """ 

44 Delete match event messages that have expired (older than 10 days). 

45 

46 This task is scheduled to run daily via Celery Beat. 

47 Only message events are deleted - goals and status_change events are preserved. 

48 

49 Returns: 

50 dict: Summary of cleanup operation with count of deleted messages. 

51 """ 

52 logger.info("Starting cleanup of expired match events") 

53 

54 try: 

55 deleted_count = self.match_event_dao.cleanup_expired_messages() 

56 

57 logger.info( 

58 "Cleanup completed", 

59 deleted_count=deleted_count, 

60 ) 

61 

62 return { 

63 "success": True, 

64 "deleted_count": deleted_count, 

65 "message": f"Deleted {deleted_count} expired match event messages", 

66 } 

67 

68 except Exception as e: 

69 logger.exception("Error during match events cleanup") 

70 return { 

71 "success": False, 

72 "deleted_count": 0, 

73 "error": str(e), 

74 }