From 51f4a40cd48539993026639e11b1cbde23b76ada Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 10 Feb 2020 15:33:16 -0500 Subject: [PATCH] Allow preserving schedules with prevent_teardown --- awxkit/awxkit/api/pages/schedules.py | 13 +++++++++++++ .../awxkit/api/pages/unified_job_templates.py | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/awxkit/awxkit/api/pages/schedules.py b/awxkit/awxkit/api/pages/schedules.py index c2da143708..9bc280b7bf 100644 --- a/awxkit/awxkit/api/pages/schedules.py +++ b/awxkit/awxkit/api/pages/schedules.py @@ -2,7 +2,9 @@ from contextlib import suppress from awxkit.api.pages import UnifiedJob from awxkit.api.resources import resources +from awxkit.config import config import awxkit.exceptions as exc + from . import page from . import base @@ -11,6 +13,17 @@ class Schedule(UnifiedJob): NATURAL_KEY = ('unified_job_template', 'name') + def silent_delete(self): + """If we are told to prevent_teardown of schedules, then keep them + but do not leave them activated, or system will be swamped quickly""" + try: + if not config.prevent_teardown: + return self.delete() + else: + self.patch(enabled=False) + except (exc.NoContent, exc.NotFound, exc.Forbidden): + pass + page.register_page([resources.schedule, resources.related_schedule], Schedule) diff --git a/awxkit/awxkit/api/pages/unified_job_templates.py b/awxkit/awxkit/api/pages/unified_job_templates.py index 22a7a70106..06b99abfdc 100644 --- a/awxkit/awxkit/api/pages/unified_job_templates.py +++ b/awxkit/awxkit/api/pages/unified_job_templates.py @@ -1,6 +1,7 @@ from awxkit.api.resources import resources from awxkit.utils import random_title, update_payload from awxkit.api.mixins import HasStatus +from awxkit.config import config from . import base from . import page @@ -42,7 +43,22 @@ class UnifiedJobTemplate(HasStatus, base.Base): update_payload(payload, self.optional_schedule_fields, kwargs) - return self.related.schedules.post(payload) + schedule = self.related.schedules.post(payload) + # register schedule in temporary dependency store as means of + # getting its teardown method to run on cleanup + if not hasattr(self, '_schedules_store'): + self._schedules_store = set() + if schedule not in self._schedules_store: + self._schedules_store.add(schedule) + return schedule + + def silent_delete(self): + if hasattr(self, '_schedules_store') and config.prevent_teardown: + # when prevent_teardown is off, we rely on cascade deletes + # in this case, looping is needed to turn them off + for schedule in self._schedules_store: + schedule.silent_delete() + return super(UnifiedJobTemplate, self).silent_delete() @property def is_successful(self):