Răsfoiți Sursa

Replace retrying with tenacity and retry azure storage account

Nuwan Goonasekera 7 ani în urmă
părinte
comite
f1ab169b19
2 a modificat fișierele cu 20 adăugiri și 20 ștergeri
  1. 19 19
      cloudbridge/cloud/providers/azure/provider.py
  2. 1 1
      setup.py

+ 19 - 19
cloudbridge/cloud/providers/azure/provider.py

@@ -1,6 +1,8 @@
 import logging
 import os
 
+import tenacity
+
 from cloudbridge.cloud.base import BaseCloudProvider
 from cloudbridge.cloud.providers.azure.azure_client import AzureClient
 from cloudbridge.cloud.providers.azure.services \
@@ -110,23 +112,21 @@ class AzureCloudProvider(BaseCloudProvider):
             resource_group_params = {'location': self.region_name}
             self._azure_client.create_resource_group(self.resource_group,
                                                      resource_group_params)
+        # Create a storage account. To prevent a race condition, try
+        # to get or create at least twice
+        self._get_or_create_storage_account()
 
-        for i in range(5):
-            try:
-                self._azure_client.get_storage_account(self.storage_account)
-                break
-            except CloudError:
-                storage_account_params = {
-                    'sku': {
-                        'name': 'Standard_LRS'
-                    },
-                    'kind': 'storage',
-                    'location': self.region_name,
-                }
-                try:
-                    self._azure_client. \
-                        create_storage_account(self.storage_account,
-                                               storage_account_params)
-                    break
-                except CloudError:
-                    pass
+    @tenacity.retry(stop=tenacity.stop_after_attempt(2))
+    def _get_or_create_storage_account(self):
+        try:
+            return self._azure_client.get_storage_account(self.storage_account)
+        except CloudError:
+            storage_account_params = {
+                'sku': {
+                    'name': 'Standard_LRS'
+                },
+                'kind': 'storage',
+                'location': self.region_name,
+            }
+            self._azure_client.create_storage_account(self.storage_account,
+                                                      storage_account_params)

+ 1 - 1
setup.py

@@ -21,7 +21,7 @@ with open(os.path.join('cloudbridge', '__init__.py')) as f:
 REQS_BASE = [
     'bunch>=1.0.1',
     'six>=1.10.0',
-    'retrying>=1.3.3'
+    'tenacity>=4.12.0'
 ]
 REQS_AWS = ['boto3']
 REQS_AZURE = ['msrest>=0.4.7',