Skip to content

Commit e7d7287

Browse files
committed
added retry to handle scenario when function is not available yet.
1 parent 5233103 commit e7d7287

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

setup.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
import time
55
import requests
66
import argparse
7+
from tenacity import retry, wait_fixed, stop_after_delay
78
from azure.storage.blob import BlobServiceClient
89
from azure.identity import DefaultAzureCredential
910
from azure.mgmt.web import WebSiteManagementClient
1011
from azure.mgmt.storage import StorageManagementClient
1112
logging.getLogger('azure').setLevel(logging.WARNING)
1213

1314
def call_search_api(search_service, search_api_version, resource_type, resource_name, method, credential, body=None):
14-
1515
# get the token
1616
token = credential.get_token("https://search.azure.com/.default").token
1717

@@ -40,6 +40,13 @@ def call_search_api(search_service, search_api_version, resource_type, resource_
4040
logging.error(f"Error when calling search API {method} {resource_type} {resource_name}. Error: {error_message}")
4141
return response
4242

43+
@retry(stop=stop_after_delay(10*60), wait=wait_fixed(60), before_sleep=lambda _: logging.info('Will attempt again in a minute as the function may not yet be available for use. Appreciate your patience....'))
44+
def get_function_key(subscription_id, resource_group, function_app_name):
45+
credential = DefaultAzureCredential()
46+
web_mgmt_client = WebSiteManagementClient(credential, subscription_id)
47+
keys = web_mgmt_client.web_apps.list_function_keys(resource_group, function_app_name, 'document_chunkings')
48+
function_ley = keys.additional_properties["default"]
49+
4350
def execute_setup(subscription_id, resource_group, function_app_name):
4451

4552
logging.info(f"Getting function app {function_app_name} properties.")
@@ -56,9 +63,8 @@ def execute_setup(subscription_id, resource_group, function_app_name):
5663
storage_container_chunks = function_app_settings.properties["STORAGE_CONTAINER_CHUNKS"]
5764
storage_account_name = function_app_settings.properties["STORAGE_ACCOUNT_NAME"]
5865

59-
logging.info(f"Getting function app {function_app_name} key.")
60-
keys = web_mgmt_client.web_apps.list_function_keys(resource_group, function_app_name, 'document_chunking')
61-
function_ley = keys.additional_properties["default"]
66+
logging.info(f"Getting function app {function_app_name} key.")
67+
function_ley = get_function_key(subscription_id, resource_group, function_app_name)
6268

6369
logging.info(f"Getting {function_app_name} storage connection string.")
6470
storage_client = StorageManagementClient(credential, subscription_id)

0 commit comments

Comments
 (0)