diff --git a/awx/api/views.py b/awx/api/views.py index 074896002f..117cc80346 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -58,7 +58,7 @@ import ansiconv from social.backends.utils import load_backends # AWX -from awx.main.tasks import send_notifications, update_host_smart_inventory_memberships +from awx.main.tasks import send_notifications from awx.main.access import get_user_queryset from awx.main.ha import is_ha_environment from awx.api.authentication import TaskAuthentication, TokenGetAuthentication @@ -2005,14 +2005,6 @@ class HostSmartInventoriesList(SubListAPIView): relationship = 'smart_inventories' new_in_320 = True - def list(self, *args, **kwargs): - try: - if settings.AWX_REBUILD_SMART_MEMBERSHIP: - update_host_smart_inventory_memberships.delay() - return super(HostSmartInventoriesList, self).list(*args, **kwargs) - except Exception as e: - return Response(dict(error=_(unicode(e))), status=status.HTTP_400_BAD_REQUEST) - class HostActivityStreamList(ActivityStreamEnforcementMixin, SubListAPIView): diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index c482e9b848..cb18d4844c 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -384,6 +384,21 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin): self.websocket_emit_status('pending_deletion') delete_inventory.delay(self.pk) + def _update_host_smart_inventory_memeberships(self): + if self.kind == 'smart' and settings.AWX_REBUILD_SMART_MEMBERSHIP: + def on_commit(): + from awx.main.tasks import update_host_smart_inventory_memberships + update_host_smart_inventory_memberships.delay() + connection.on_commit(on_commit) + + def save(self, *args, **kwargs): + self._update_host_smart_inventory_memeberships() + super(Inventory, self).save(*args, **kwargs) + + def delete(self, *args, **kwargs): + self._update_host_smart_inventory_memeberships() + super(Inventory, self).delete(*args, **kwargs) + class SmartInventoryMembership(BaseModel): ''' @@ -568,6 +583,21 @@ class Host(CommonModelNameNotUnique): host_name = self.variables_dict['ansible_host'] return host_name + def _update_host_smart_inventory_memeberships(self): + if settings.AWX_REBUILD_SMART_MEMBERSHIP: + def on_commit(): + from awx.main.tasks import update_host_smart_inventory_memberships + update_host_smart_inventory_memberships.delay() + connection.on_commit(on_commit) + + def save(self, *args, **kwargs): + self._update_host_smart_inventory_memeberships() + super(Host, self).save(*args, **kwargs) + + def delete(self, *args, **kwargs): + self._update_host_smart_inventory_memeberships() + super(Host, self).delete(*args, **kwargs) + class Group(CommonModelNameNotUnique): ''' diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 54401af49f..fbab5c7707 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -347,7 +347,7 @@ def update_inventory_computed_fields(inventory_id, should_update_hosts=True): def update_host_smart_inventory_memberships(): try: with transaction.atomic(): - smart_inventories = Inventory.objects.filter(kind='smart', host_filter__isnull=False) + smart_inventories = Inventory.objects.filter(kind='smart', host_filter__isnull=False, pending_deletion=False) SmartInventoryMembership.objects.all().delete() memberships = [] for smart_inventory in smart_inventories: