Skip to content

Commit

Permalink
Cleanup_host_metric command with default params
Browse files Browse the repository at this point in the history
  • Loading branch information
slemrmartin committed Aug 9, 2023
1 parent 3c82008 commit 5be38d0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 48 deletions.
26 changes: 12 additions & 14 deletions awx/main/management/commands/cleanup_host_metrics.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
from django.core.management.base import BaseCommand
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from awx.main.tasks.host_metrics import HostMetricTask


class Command(BaseCommand):
"""
Run soft/hard-deleting of HostMetrics
This command provides cleanup task for HostMetric model.
There are two modes, which run in following order:
- soft cleanup
- - Perform soft-deletion of all host metrics last automated 12 months ago or before.
This is the same as issuing a DELETE request to /api/v2/host_metrics/N/ for all host metrics that match the criteria.
- - updates columns delete, deleted_counter and last_deleted
- hard cleanup
- - Permanently erase from the database all host metrics last automated 36 months ago or before.
This operation happens after the soft deletion has finished.
"""

help = 'Run soft/hard-deleting of HostMetrics'

def add_arguments(self, parser):
parser.add_argument('--soft', type=int, nargs='?', default=None, const=0, help='Threshold in months for soft-deleting')
parser.add_argument('--hard', type=int, nargs='?', default=None, const=0, help='Threshold in months for hard-deleting')
help = 'Run soft and hard-deletion of HostMetrics'

def handle(self, *args, **options):
soft_threshold = options.get('soft')
hard_threshold = options.get('hard')

if soft_threshold is None and hard_threshold is None:
return _("Specify either --soft or --hard argument")

HostMetricTask().cleanup(soft_threshold=soft_threshold, hard_threshold=hard_threshold)
HostMetricTask().cleanup(soft_threshold=settings.CLEANUP_HOST_METRICS_SOFT_THRESHOLD, hard_threshold=settings.CLEANUP_HOST_METRICS_HARD_THRESHOLD)
18 changes: 0 additions & 18 deletions awx/main/models/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import os.path
from urllib.parse import urljoin

import dateutil.relativedelta
import yaml

# Django
Expand Down Expand Up @@ -890,23 +889,6 @@ def soft_restore(self):
self.deleted = False
self.save(update_fields=['deleted'])

@classmethod
def cleanup_task(cls, months_ago):
try:
months_ago = int(months_ago)
if months_ago <= 0:
raise ValueError()

last_automation_before = now() - dateutil.relativedelta.relativedelta(months=months_ago)

logger.info(f'cleanup_host_metrics: soft-deleting records last automated before {last_automation_before}')
HostMetric.active_objects.filter(last_automation__lt=last_automation_before).update(
deleted=True, deleted_counter=models.F('deleted_counter') + 1, last_deleted=now()
)
settings.CLEANUP_HOST_METRICS_LAST_TS = now()
except (TypeError, ValueError):
logger.error(f"cleanup_host_metrics: months_ago({months_ago}) has to be a positive integer value")


class HostMetricSummaryMonthly(models.Model):
"""
Expand Down
8 changes: 4 additions & 4 deletions awx/main/tasks/host_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ def soft_cleanup(threshold=None):
threshold = getattr(settings, 'CLEANUP_HOST_METRICS_SOFT_THRESHOLD', 12)

last_automation_before = now() - relativedelta(months=threshold)
logger.info(f'cleanup_host_metrics: soft-deleting records last automated before {last_automation_before}')
HostMetric.active_objects.filter(last_automation__lt=last_automation_before).update(
rows = HostMetric.active_objects.filter(last_automation__lt=last_automation_before).update(
deleted=True, deleted_counter=F('deleted_counter') + 1, last_deleted=now()
)
logger.info(f'cleanup_host_metrics: soft-deleted records last automated before {last_automation_before}, affected rows: {rows}')

@staticmethod
def hard_cleanup(threshold=None):
if not threshold:
threshold = getattr(settings, 'CLEANUP_HOST_METRICS_HARD_THRESHOLD', 36)

last_deleted_before = now() - relativedelta(months=threshold)
logger.info(f'cleanup_host_metrics: hard-deleting records last deleted before {last_deleted_before}')
queryset = HostMetric.objects.filter(deleted=True, last_deleted__lt=last_deleted_before)
queryset._raw_delete(queryset.db)
rows = queryset.delete()
logger.info(f'cleanup_host_metrics: hard-deleted records which were soft deleted before {last_deleted_before}, affected rows: {rows[0]}')


class HostMetricSummaryMonthlyTask:
Expand Down
19 changes: 7 additions & 12 deletions awx/main/tests/functional/commands/test_cleanup_host_metrics.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import pytest

from awx.main.management.commands.cleanup_host_metrics import Command
from awx.main.tasks.host_metrics import HostMetricTask
from awx.main.models.inventory import HostMetric
from awx.main.tests.factories.fixtures import mk_host_metric
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.utils import timezone
from django.utils.translation import gettext_lazy as _


def test_no_args():
assert Command().handle() == _("Specify either --soft or --hard argument")


@pytest.mark.django_db
def test_no_host_metrics():
"""No-crash test"""
assert HostMetric.objects.count() == 0
Command().handle(soft=0, hard=0)
Command().handle(soft=24, hard=42)
HostMetricTask().cleanup(soft_threshold=0, hard_threshold=0)
HostMetricTask().cleanup(soft_threshold=24, hard_threshold=42)
assert HostMetric.objects.count() == 0


Expand All @@ -40,9 +35,9 @@ def test_soft_delete(threshold):

for i in range(2):
if threshold == settings.CLEANUP_HOST_METRICS_SOFT_THRESHOLD:
Command().handle(soft=0) # default value should be loaded from settings
HostMetricTask().cleanup(soft_threshold=0)
else:
Command().handle(soft=threshold)
HostMetricTask().cleanup(soft_threshold=threshold)
assert HostMetric.objects.count() == 8

hostnames = []
Expand Down Expand Up @@ -71,9 +66,9 @@ def test_hard_delete(threshold):

for i in range(2):
if threshold == settings.CLEANUP_HOST_METRICS_HARD_THRESHOLD:
Command().handle(hard=0) # default value should be loaded from settings
HostMetricTask().cleanup(hard_threshold=0)
else:
Command().handle(hard=threshold)
HostMetricTask().cleanup(hard_threshold=threshold)
assert HostMetric.objects.count() == 6

hostnames = []
Expand Down

0 comments on commit 5be38d0

Please sign in to comment.