From 9a0df887691798da6062df4c4a32d9dbfab34b3e Mon Sep 17 00:00:00 2001 From: Chris Church Date: Thu, 1 Dec 2016 00:14:18 -0500 Subject: [PATCH] Add output of skipped/deleted to cleanup_jobs, so there is always some kind of output. --- awx/main/management/commands/cleanup_jobs.py | 37 ++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/awx/main/management/commands/cleanup_jobs.py b/awx/main/management/commands/cleanup_jobs.py index 68f428447f..3f7270c9b2 100644 --- a/awx/main/management/commands/cleanup_jobs.py +++ b/awx/main/management/commands/cleanup_jobs.py @@ -48,6 +48,7 @@ class Command(NoArgsCommand): def cleanup_jobs(self): #jobs_qs = Job.objects.exclude(status__in=('pending', 'running')) #jobs_qs = jobs_qs.filter(created__lte=self.cutoff) + skipped, deleted = 0, 0 for job in Job.objects.all(): job_display = '"%s" (started %s, %d host summaries, %d events)' % \ (unicode(job), unicode(job.created), @@ -55,16 +56,21 @@ class Command(NoArgsCommand): if job.status in ('pending', 'waiting', 'running'): action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s job %s', action_text, job.status, job_display) + skipped += 1 elif job.created >= self.cutoff: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, job_display) + skipped += 1 else: action_text = 'would delete' if self.dry_run else 'deleting' self.logger.info('%s %s', action_text, job_display) if not self.dry_run: job.delete() + deleted += 1 + return skipped, deleted def cleanup_ad_hoc_commands(self): + skipped, deleted = 0, 0 for ad_hoc_command in AdHocCommand.objects.all(): ad_hoc_command_display = '"%s" (started %s, %d events)' % \ (unicode(ad_hoc_command), unicode(ad_hoc_command.created), @@ -72,65 +78,86 @@ class Command(NoArgsCommand): if ad_hoc_command.status in ('pending', 'waiting', 'running'): action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s ad hoc command %s', action_text, ad_hoc_command.status, ad_hoc_command_display) + skipped += 1 elif ad_hoc_command.created >= self.cutoff: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, ad_hoc_command_display) + skipped += 1 else: action_text = 'would delete' if self.dry_run else 'deleting' self.logger.info('%s %s', action_text, ad_hoc_command_display) if not self.dry_run: ad_hoc_command.delete() + deleted += 1 + return skipped, deleted def cleanup_project_updates(self): + skipped, deleted = 0, 0 for pu in ProjectUpdate.objects.all(): pu_display = '"%s" (started %s)' % (unicode(pu), unicode(pu.created)) if pu.status in ('pending', 'waiting', 'running'): action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s project update %s', action_text, pu.status, pu_display) - if pu in (pu.project.current_update, pu.project.last_update) and pu.project.scm_type: + skipped += 1 + elif pu in (pu.project.current_update, pu.project.last_update) and pu.project.scm_type: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, pu_display) + skipped += 1 elif pu.created >= self.cutoff: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, pu_display) + skipped += 1 else: action_text = 'would delete' if self.dry_run else 'deleting' self.logger.info('%s %s', action_text, pu_display) if not self.dry_run: pu.delete() + deleted += 1 + return skipped, deleted def cleanup_inventory_updates(self): + skipped, deleted = 0, 0 for iu in InventoryUpdate.objects.all(): iu_display = '"%s" (started %s)' % (unicode(iu), unicode(iu.created)) if iu.status in ('pending', 'waiting', 'running'): action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s inventory update %s', action_text, iu.status, iu_display) - if iu in (iu.inventory_source.current_update, iu.inventory_source.last_update) and iu.inventory_source.source: + skipped += 1 + elif iu in (iu.inventory_source.current_update, iu.inventory_source.last_update) and iu.inventory_source.source: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, iu_display) + skipped += 1 elif iu.created >= self.cutoff: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, iu_display) + skipped += 1 else: action_text = 'would delete' if self.dry_run else 'deleting' self.logger.info('%s %s', action_text, iu_display) if not self.dry_run: iu.delete() + deleted += 1 + return skipped, deleted def cleanup_management_jobs(self): + skipped, deleted = 0, 0 for sj in SystemJob.objects.all(): sj_display = '"%s" (started %s)' % (unicode(sj), unicode(sj.created)) if sj.status in ('pending', 'waiting', 'running'): action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s system_job %s', action_text, sj.status, sj_display) + skipped += 1 elif sj.created >= self.cutoff: action_text = 'would skip' if self.dry_run else 'skipping' self.logger.debug('%s %s', action_text, sj_display) + skipped += 1 else: action_text = 'would delete' if self.dry_run else 'deleting' self.logger.info('%s %s', action_text, sj_display) if not self.dry_run: sj.delete() + deleted += 1 + return skipped, deleted def init_logging(self): log_levels = dict(enumerate([logging.ERROR, logging.INFO, @@ -161,4 +188,8 @@ class Command(NoArgsCommand): models_to_cleanup.update(model_names) for m in model_names: if m in models_to_cleanup: - getattr(self, 'cleanup_%s' % m)() + skipped, deleted = getattr(self, 'cleanup_%s' % m)() + if self.dry_run: + self.logger.log(99, '%s: %d would be deleted, %d would be skipped.', m.replace('_', ' '), deleted, skipped) + else: + self.logger.log(99, '%s: %d deleted, %d skipped.', m.replace('_', ' '), deleted, skipped)