internal static async Task WaitUntilFunctionAppReadyAsync()

in src/Cli/func/Helpers/AzureHelper.cs [512:571]


        internal static async Task WaitUntilFunctionAppReadyAsync(Site functionApp, string accessToken, string managementURL, HttpMessageHandler messageHandler = null)
        {
            var masterKey = await GetMasterKeyAsync(functionApp.SiteId, accessToken, managementURL);

            if (masterKey is null)
            {
                throw new CliException($"The masterKey is null. hostname: {functionApp.HostName}.");
            }

            HttpMessageHandler handler = messageHandler ?? new HttpClientHandler();
            if (StaticSettings.IsDebug)
            {
                handler = new LoggingHandler(handler);
            }

            var functionAppReadyClient = new HttpClient(handler);
            const string jsonContentType = "application/json";
            functionAppReadyClient.DefaultRequestHeaders.Add("User-Agent", Constants.CliUserAgent);
            functionAppReadyClient.DefaultRequestHeaders.Add("Accept", jsonContentType);

            await RetryHelper.Retry(
                async () =>
                {
                    functionAppReadyClient.DefaultRequestHeaders.Add("x-ms-request-id", Guid.NewGuid().ToString());
                    var uri = new Uri($"https://{functionApp.HostName}/admin/host/status?code={masterKey}");
                    var request = new HttpRequestMessage()
                    {
                        RequestUri = uri,
                        Method = HttpMethod.Get
                    };

                    var response = await functionAppReadyClient.SendAsync(request);
                    ColoredConsole.Write(".");

                    if (response.IsSuccessStatusCode)
                    {
                        var json = await response.Content.ReadAsAsync<JToken>();
                        var processUpTime = json["processUptime"]?.ToObject<int>() ?? 0;

                        // Wait for 30 sec after Restart is happening.
                        // We assume that we need to wait until the restart happens if the ProcessUpTime is greater than 45sec.
                        if (processUpTime >= Constants.DefaultGetFunctionReadinessTime &&
                            processUpTime < Constants.DefaultRestartedWorkerProcessUptimeWithin)
                        {
                            ColoredConsole.WriteLine(" done");
                            return;
                        }
                        else
                        {
                            throw new Exception($"GetFunctions is not ready. Hostname: {functionApp.HostName}");
                        }
                    }
                    else
                    {
                        throw new Exception($"Status check returns unsuccessful status. Hostname: {functionApp.HostName}");
                    }
                },
                18,
                TimeSpan.FromSeconds(3));
        }