Guard around race condition (#15452)

I had the luck of running into this race condition that broke my deployment. No instance was ever able to register because on running "awx-manage" in some check of a setting, it would end up failing here with

```
  File "/var/lib/awx/venv/awx/lib64/python3.11/site-packages/awx/conf/license.py", line 10, in _get_validated_license_data
    return get_licenser().validate()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/lib/awx/venv/awx/lib64/python3.11/site-packages/awx/main/utils/licensing.py", line 453, in validate
    automated_since = int(Instance.objects.order_by('id').first().created.timestamp())
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'created'
```
This commit is contained in:
Elijah DeLee 2024-08-20 16:24:56 -04:00 committed by GitHub
parent 39d1922b80
commit c5c617b178
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -450,7 +450,12 @@ class Licenser(object):
if first_host:
automated_since = int(first_host.first_automation.timestamp())
else:
automated_since = int(Instance.objects.order_by('id').first().created.timestamp())
try:
automated_since = int(Instance.objects.order_by('id').first().created.timestamp())
except AttributeError:
# In the odd scenario that create_preload_data was not run, there are no hosts
# Then we CAN end up here before any instance has registered
automated_since = int(time.time())
instance_count = int(attrs.get('instance_count', 0))
attrs['current_instances'] = current_instances
attrs['automated_instances'] = automated_instances