Skip to content

Commit

Permalink
fix mgmt commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Mar 11, 2024
1 parent 6819c19 commit 98eb3c7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
18 changes: 15 additions & 3 deletions api/tacticalrmm/core/management/commands/post_update_tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import base64

from django.core.management import call_command
from django.core.management.base import BaseCommand

from accounts.models import User
from agents.models import Agent
from autotasks.models import AutomatedTask
from checks.models import Check, CheckHistory
from core.models import CoreSettings
from core.tasks import remove_orphaned_history_results, sync_mesh_perms_task
from scripts.models import Script
from tacticalrmm.constants import AGENT_DEFER, ScriptType

Expand Down Expand Up @@ -55,6 +56,17 @@ def handle(self, *args, **kwargs) -> None:

agent.save(update_fields=["goarch"])

call_command("remove_orphaned_history_results")
call_command("sync_mesh_with_trmm")
self.stdout.write(
self.style.SUCCESS("Checking for orphaned history results...")
)
count = remove_orphaned_history_results()
if count:
self.stdout.write(
self.style.SUCCESS(f"Removed {count} orphaned history results.")
)

core = CoreSettings.objects.first()
if core.sync_mesh_with_trmm:
sync_mesh_perms_task()

self.stdout.write("Post update tasks finished")
27 changes: 22 additions & 5 deletions api/tacticalrmm/core/tasks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import asyncio
import logging
import traceback
from contextlib import suppress
from time import sleep
from typing import TYPE_CHECKING, Any

import nats
from django.conf import settings
from django.core.management import call_command
from django.db import transaction
from django.db.models import Prefetch
from django.db.utils import DatabaseError
from django.utils import timezone as djangotime
Expand All @@ -20,7 +19,7 @@
from alerts.models import Alert
from alerts.tasks import prune_resolved_alerts
from autotasks.models import AutomatedTask, TaskResult
from checks.models import Check, CheckResult
from checks.models import Check, CheckHistory, CheckResult
from checks.tasks import prune_check_history
from clients.models import Client, Site
from core.mesh_utils import (
Expand Down Expand Up @@ -51,6 +50,7 @@
TaskType,
)
from tacticalrmm.helpers import make_random_password, setup_nats_options
from tacticalrmm.logger import logger
from tacticalrmm.nats_utils import a_nats_cmd
from tacticalrmm.permissions import _has_perm_on_agent
from tacticalrmm.utils import redis_lock
Expand All @@ -59,7 +59,24 @@
from django.db.models import QuerySet
from nats.aio.client import Client as NATSClient

logger = logging.getLogger("trmm")

def remove_orphaned_history_results() -> int:
try:
with transaction.atomic():
check_hist_agentids = CheckHistory.objects.values_list(
"agent_id", flat=True
).distinct()
current_agentids = set(Agent.objects.values_list("agent_id", flat=True))
orphaned_agentids = [
i for i in check_hist_agentids if i not in current_agentids
]
count, _ = CheckHistory.objects.filter(
agent_id__in=orphaned_agentids
).delete()
return count
except Exception as e:
logger.error(str(e))
return 0


@app.task
Expand All @@ -68,7 +85,7 @@ def core_maintenance_tasks() -> None:
remove_if_not_scheduled=True, expire_date__lt=djangotime.now()
).delete()

call_command("remove_orphaned_history_results")
remove_orphaned_history_results()

core = get_core_settings()

Expand Down

0 comments on commit 98eb3c7

Please sign in to comment.