|
6 | 6 |
|
7 | 7 | import nats
|
8 | 8 | from django.conf import settings
|
| 9 | +from django.db import transaction |
9 | 10 | from django.db.models import Prefetch
|
10 | 11 | from django.db.utils import DatabaseError
|
11 | 12 | from django.utils import timezone as djangotime
|
|
18 | 19 | from alerts.models import Alert
|
19 | 20 | from alerts.tasks import prune_resolved_alerts
|
20 | 21 | from autotasks.models import AutomatedTask, TaskResult
|
21 |
| -from checks.models import Check, CheckResult |
| 22 | +from checks.models import Check, CheckHistory, CheckResult |
22 | 23 | from checks.tasks import prune_check_history
|
23 | 24 | from clients.models import Client, Site
|
24 | 25 | from core.mesh_utils import (
|
@@ -66,6 +67,16 @@ def core_maintenance_tasks() -> None:
|
66 | 67 | remove_if_not_scheduled=True, expire_date__lt=djangotime.now()
|
67 | 68 | ).delete()
|
68 | 69 |
|
| 70 | + with transaction.atomic(): |
| 71 | + check_hist_agentids = CheckHistory.objects.values_list( |
| 72 | + "agent_id", flat=True |
| 73 | + ).distinct() |
| 74 | + current_agentids = set(Agent.objects.values_list("agent_id", flat=True)) |
| 75 | + orphaned_agentids = [ |
| 76 | + i for i in check_hist_agentids if i not in current_agentids |
| 77 | + ] |
| 78 | + CheckHistory.objects.filter(agent_id__in=orphaned_agentids).delete() |
| 79 | + |
69 | 80 | core = get_core_settings()
|
70 | 81 |
|
71 | 82 | # remove old CheckHistory data
|
|
0 commit comments