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:
commit
be0ee97fb3
21
awx/main/migrations/0013_v300_label_changes.py
Normal file
21
awx/main/migrations/0013_v300_label_changes.py
Normal 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),
|
||||
),
|
||||
]
|
@ -24,10 +24,21 @@ class Label(CommonModelNameNotUnique):
|
||||
organization = models.ForeignKey(
|
||||
'Organization',
|
||||
related_name='labels',
|
||||
blank=True,
|
||||
null=True,
|
||||
default=None,
|
||||
help_text=_('Organization this label belongs to.'),
|
||||
on_delete=models.CASCADE,
|
||||
on_delete=models.SET_NULL,
|
||||
)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('api:label_detail', args=(self.pk,))
|
||||
|
||||
@staticmethod
|
||||
def get_orphaned_labels():
|
||||
return \
|
||||
Label.objects.filter(
|
||||
organization=None,
|
||||
jobtemplate_labels__isnull=True
|
||||
)
|
||||
|
||||
|
@ -46,6 +46,7 @@ from django.contrib.auth.models import User
|
||||
from awx.lib.metrics import task_timer
|
||||
from awx.main.constants import CLOUD_PROVIDERS
|
||||
from awx.main.models import * # noqa
|
||||
from awx.main.models.label import Label
|
||||
from awx.main.queue import FifoQueue
|
||||
from awx.main.conf import tower_settings
|
||||
from awx.main.task_engine import TaskSerializer, TASK_TIMEOUT_INTERVAL
|
||||
@ -109,6 +110,13 @@ def run_administrative_checks(self):
|
||||
tower_admin_emails,
|
||||
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)
|
||||
def tower_periodic_scheduler(self):
|
||||
def get_last_run():
|
||||
|
10
awx/main/tests/unit/models/test_label.py
Normal file
10
awx/main/tests/unit/models/test_label.py
Normal 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)
|
18
awx/main/tests/unit/settings/test_defaults.py
Normal file
18
awx/main/tests/unit/settings/test_defaults.py
Normal 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)
|
12
awx/main/tests/unit/test_tasks.py
Normal file
12
awx/main/tests/unit/test_tasks.py
Normal 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
|
||||
|
@ -345,6 +345,10 @@ CELERYBEAT_SCHEDULE = {
|
||||
'task': 'awx.main.tasks.run_administrative_checks',
|
||||
'schedule': timedelta(days=30)
|
||||
},
|
||||
'label_cleanup': {
|
||||
'task': 'awx.main.tasks.run_label_cleanup',
|
||||
'schedule': timedelta(days=7)
|
||||
},
|
||||
}
|
||||
|
||||
# Social Auth configuration.
|
||||
|
Loading…
Reference in New Issue
Block a user