Coverage for src/backend/InvenTree/InvenTree/status.py: 76%

40 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Provides system status functionality checks.""" 

2 

3from datetime import timedelta 

4 

5from django.conf import settings 

6from django.utils import timezone 

7 

8import structlog 

9from django_q.models import Success 

10from django_q.status import Stat 

11 

12import InvenTree.helpers_email 

13import InvenTree.ready 

14 

15logger = structlog.get_logger('inventree') 

16 

17 

18def is_worker_running(**kwargs): 

19 """Return True if the background worker process is operational.""" 

20 clusters = Stat.get_all() 

21 

22 if len(clusters) > 0: 22 ↛ 23line 22 didn't jump to line 23 because the condition on line 22 was never true

23 return True 

24 

25 """ 

26 Sometimes Stat.get_all() returns []. 

27 In this case we have the 'heartbeat' task running every 5 minutes. 

28 Check to see if we have any successful result within the last 10 minutes 

29 """ 

30 

31 now = timezone.now() 

32 past = now - timedelta(minutes=10) 

33 

34 results = Success.objects.filter(started__gte=past) 

35 

36 # If any results are returned, then the background worker is running! 

37 try: 

38 result = results.exists() 

39 except Exception: 

40 # We may throw an exception if the database is not ready, 

41 # or if the django_q table is not yet created (i.e. in CI testing) 

42 result = False 

43 

44 return result 

45 

46 

47def check_system_health(**kwargs): 

48 """Check that the InvenTree system is running OK. 

49 

50 Returns True if all system checks pass. 

51 """ 

52 result = True 

53 

54 if InvenTree.ready.isInTestMode(): 54 ↛ 56line 54 didn't jump to line 56 because the condition on line 54 was never true

55 # Do not perform further checks if we are running unit tests 

56 return False 

57 

58 if InvenTree.ready.isImportingData(): 58 ↛ 60line 58 didn't jump to line 60 because the condition on line 58 was never true

59 # Do not perform further checks if we are importing data 

60 return False 

61 

62 if not is_worker_running(**kwargs): # pragma: no cover 62 ↛ 67line 62 didn't jump to line 67 because the condition on line 62 was always true

63 result = False 

64 if not settings.DEBUG: 64 ↛ 67line 64 didn't jump to line 67 because the condition on line 64 was always true

65 logger.warning('Background worker check failed') 

66 

67 if not InvenTree.helpers_email.is_email_configured(): # pragma: no cover 67 ↛ 72line 67 didn't jump to line 72 because the condition on line 67 was always true

68 result = False 

69 if not settings.DEBUG: 69 ↛ 72line 69 didn't jump to line 72 because the condition on line 69 was always true

70 logger.warning('INVE-W7: Email backend not configured') 

71 

72 if not result: # pragma: no cover 72 ↛ 76line 72 didn't jump to line 76 because the condition on line 72 was always true

73 if not settings.DEBUG: 73 ↛ 76line 73 didn't jump to line 76 because the condition on line 73 was always true

74 logger.warning('InvenTree system health checks failed') 

75 

76 return result