Merge pull request #9944 from AlanCoding/schedule_teardown

Allow stable use of AWXKIT_PREVENT_TEARDOWN by disabling schedules at the end
This commit is contained in:
Shane McDonald 2021-07-22 20:19:35 -04:00 committed by GitHub
commit c09050d1f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -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)

View File

@ -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):