From 33e97d5c5a09688fa5f10fa62eef0d6c7fdb3ec9 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Fri, 27 Jan 2017 09:31:11 -0500 Subject: [PATCH] add notifications to cleanup_jobs --- awx/main/access.py | 4 +-- awx/main/management/commands/cleanup_jobs.py | 30 ++++++++++++++++++-- awx/main/tasks.py | 4 ++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/awx/main/access.py b/awx/main/access.py index aeeef9fa54..457c84763d 100644 --- a/awx/main/access.py +++ b/awx/main/access.py @@ -2024,9 +2024,9 @@ class NotificationAccess(BaseAccess): model = Notification def get_queryset(self): - qs = self.model.objects.all() + qs = self.model.objects.prefetch_related('notification_template') if self.user.is_superuser or self.user.is_system_auditor: - return qs + return qs.all() return self.model.objects.filter( Q(notification_template__organization__in=self.user.admin_of_organizations) | Q(notification_template__organization__in=self.user.auditor_of_organizations) diff --git a/awx/main/management/commands/cleanup_jobs.py b/awx/main/management/commands/cleanup_jobs.py index 536ce61e39..ead5ef50d9 100644 --- a/awx/main/management/commands/cleanup_jobs.py +++ b/awx/main/management/commands/cleanup_jobs.py @@ -12,7 +12,7 @@ from django.db import transaction from django.utils.timezone import now # AWX -from awx.main.models import Job, AdHocCommand, ProjectUpdate, InventoryUpdate, SystemJob, WorkflowJob +from awx.main.models import Job, AdHocCommand, ProjectUpdate, InventoryUpdate, SystemJob, WorkflowJob, Notification class Command(NoArgsCommand): @@ -43,6 +43,9 @@ class Command(NoArgsCommand): make_option('--management-jobs', default=False, action='store_true', dest='only_management_jobs', help='Remove management jobs'), + make_option('--notifications', dest='only_notifications', + action='store_true', default=False, + help='Remove notifications'), make_option('--workflow-jobs', default=False, action='store_true', dest='only_workflow_jobs', help='Remove workflow jobs') @@ -194,6 +197,28 @@ class Command(NoArgsCommand): deleted += 1 return skipped, deleted + def cleanup_notifications(self): + skipped, deleted = 0, 0 + for notification in Notification.objects.all(): + notification_display = '"{}" (started {}, {} type, {} sent)'.format( + unicode(notification), unicode(notification.created), + notification.notification_type, notification.notifications_sent) + if notification.status in ('pending',): + action_text = 'would skip' if self.dry_run else 'skipping' + self.logger.debug('%s %s notification %s', action_text, notification.status, notification_display) + skipped += 1 + elif notification.created >= self.cutoff: + action_text = 'would skip' if self.dry_run else 'skipping' + self.logger.debug('%s %s', action_text, notification_display) + skipped += 1 + else: + action_text = 'would delete' if self.dry_run else 'deleting' + self.logger.info('%s %s', action_text, notification_display) + if not self.dry_run: + notification.delete() + deleted += 1 + return skipped, deleted + @transaction.atomic def handle_noargs(self, **options): self.verbosity = int(options.get('verbosity', 1)) @@ -204,7 +229,8 @@ class Command(NoArgsCommand): self.cutoff = now() - datetime.timedelta(days=self.days) except OverflowError: raise CommandError('--days specified is too large. Try something less than 99999 (about 270 years).') - model_names = ('jobs', 'ad_hoc_commands', 'project_updates', 'inventory_updates', 'management_jobs', 'workflow_jobs') + model_names = ('jobs', 'ad_hoc_commands', 'project_updates', 'inventory_updates', + 'management_jobs', 'workflow_jobs', 'notifications') models_to_cleanup = set() for m in model_names: if options.get('only_%s' % m, False): diff --git a/awx/main/tasks.py b/awx/main/tasks.py index d59b3b246b..12d3ed48ae 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -1879,7 +1879,9 @@ class RunSystemJob(BaseTask): if 'days' in json_vars and system_job.job_type != 'cleanup_facts': args.extend(['--days', str(json_vars.get('days', 60))]) if system_job.job_type == 'cleanup_jobs': - args.extend(['--jobs', '--project-updates', '--inventory-updates', '--management-jobs', '--ad-hoc-commands', '--workflow-jobs']) + args.extend(['--jobs', '--project-updates', '--inventory-updates', + '--management-jobs', '--ad-hoc-commands', '--workflow-jobs', + '--notifications']) if system_job.job_type == 'cleanup_facts': if 'older_than' in json_vars: args.extend(['--older_than', str(json_vars['older_than'])])