1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 23:51:09 +03:00

Merge pull request #1409 from chrismeyersfsu/labels-cleanup2

label cleanup periodic job
This commit is contained in:
Chris Meyers 2016-04-05 15:27:47 -04:00
commit be0ee97fb3
7 changed files with 85 additions and 1 deletions

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
class Migration(migrations.Migration):
dependencies = [
('main', '0012_v300_create_labels'),
]
operations = [
migrations.AlterField(
model_name='label',
name='organization',
field=models.ForeignKey(related_name='labels', on_delete=django.db.models.deletion.SET_NULL, default=None, blank=True, to='main.Organization', help_text='Organization this label belongs to.', null=True),
),
]

View File

@ -24,10 +24,21 @@ class Label(CommonModelNameNotUnique):
organization = models.ForeignKey( organization = models.ForeignKey(
'Organization', 'Organization',
related_name='labels', related_name='labels',
blank=True,
null=True,
default=None,
help_text=_('Organization this label belongs to.'), help_text=_('Organization this label belongs to.'),
on_delete=models.CASCADE, on_delete=models.SET_NULL,
) )
def get_absolute_url(self): def get_absolute_url(self):
return reverse('api:label_detail', args=(self.pk,)) return reverse('api:label_detail', args=(self.pk,))
@staticmethod
def get_orphaned_labels():
return \
Label.objects.filter(
organization=None,
jobtemplate_labels__isnull=True
)

View File

@ -46,6 +46,7 @@ from django.contrib.auth.models import User
from awx.lib.metrics import task_timer from awx.lib.metrics import task_timer
from awx.main.constants import CLOUD_PROVIDERS from awx.main.constants import CLOUD_PROVIDERS
from awx.main.models import * # noqa from awx.main.models import * # noqa
from awx.main.models.label import Label
from awx.main.queue import FifoQueue from awx.main.queue import FifoQueue
from awx.main.conf import tower_settings from awx.main.conf import tower_settings
from awx.main.task_engine import TaskSerializer, TASK_TIMEOUT_INTERVAL from awx.main.task_engine import TaskSerializer, TASK_TIMEOUT_INTERVAL
@ -109,6 +110,13 @@ def run_administrative_checks(self):
tower_admin_emails, tower_admin_emails,
fail_silently=True) fail_silently=True)
@task(bind=True)
def run_label_cleanup(self):
qs = Label.get_orphaned_labels()
labels_count = qs.count()
qs.delete()
return labels_count
@task(bind=True) @task(bind=True)
def tower_periodic_scheduler(self): def tower_periodic_scheduler(self):
def get_last_run(): def get_last_run():

View File

@ -0,0 +1,10 @@
from awx.main.models.label import Label
def test_get_orphaned_labels(mocker):
mock_query_set = mocker.MagicMock()
Label.objects.filter = mocker.MagicMock(return_value=mock_query_set)
ret = Label.get_orphaned_labels()
assert mock_query_set == ret
Label.objects.filter.assert_called_with(organization=None, jobtemplate_labels__isnull=True)

View File

@ -0,0 +1,18 @@
import pytest
from django.conf import settings
from datetime import timedelta
@pytest.mark.parametrize("job_name,function_path", [
('label_cleanup', 'awx.main.tasks.run_label_cleanup'),
('admin_checks', 'awx.main.tasks.run_administrative_checks'),
('tower_scheduler', 'awx.main.tasks.tower_periodic_scheduler'),
])
def test_CELERYBEAT_SCHEDULE(mocker, job_name, function_path):
assert job_name in settings.CELERYBEAT_SCHEDULE
assert 'schedule' in settings.CELERYBEAT_SCHEDULE[job_name]
assert type(settings.CELERYBEAT_SCHEDULE[job_name]['schedule']) is timedelta
assert settings.CELERYBEAT_SCHEDULE[job_name]['task'] == function_path
# Ensures that the function exists
mocker.patch(function_path)

View File

@ -0,0 +1,12 @@
from awx.main.tasks import run_label_cleanup
def test_run_label_cleanup(mocker):
qs = mocker.Mock(**{'count.return_value': 3, 'delete.return_value': None})
mock_label = mocker.patch('awx.main.models.label.Label.get_orphaned_labels',return_value=qs)
ret = run_label_cleanup()
mock_label.assert_called_with()
qs.delete.assert_called_with()
assert 3 == ret

View File

@ -345,6 +345,10 @@ CELERYBEAT_SCHEDULE = {
'task': 'awx.main.tasks.run_administrative_checks', 'task': 'awx.main.tasks.run_administrative_checks',
'schedule': timedelta(days=30) 'schedule': timedelta(days=30)
}, },
'label_cleanup': {
'task': 'awx.main.tasks.run_label_cleanup',
'schedule': timedelta(days=7)
},
} }
# Social Auth configuration. # Social Auth configuration.