<commit_before>﻿// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Microsoft.Azure.WebJobs.Script.WebHost.Handlers
{
    public class EnsureHostRunningHandler : DelegatingHandler
    {
        private readonly TimeSpan _hostTimeout = new TimeSpan(0, 0, 30);
        private readonly int _hostRunningPollIntervalMs = 500;
        private WebScriptHostManager _scriptHostManager;
 
        public EnsureHostRunningHandler(HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _scriptHostManager = (WebScriptHostManager)config.DependencyResolver.GetService(typeof(WebScriptHostManager));
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // some routes do not require the host to be running (most do)
            bool bypassHostCheck = request.RequestUri.LocalPath.Trim('/').ToLowerInvariant().EndsWith("admin/host/status");

            if (!bypassHostCheck)
            {
                // If the host is not running, we'll wait a bit for it to fully
                // initialize. This might happen if http requests come in while the
                // host is starting up for the first time, or if it is restarting.
                TimeSpan timeWaited = TimeSpan.Zero;
                while (!_scriptHostManager.IsRunning && (timeWaited < _hostTimeout))
                {
                    await Task.Delay(_hostRunningPollIntervalMs);
                    timeWaited += TimeSpan.FromMilliseconds(_hostRunningPollIntervalMs);
                }

                // if the host is not running after or wait time has expired
                // return a 503
                if (!_scriptHostManager.IsRunning)
                {
                    return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable);
                }
            }

            return await base.SendAsync(request, cancellationToken);
        }
    }
}<commit_msg>Adding a response message for host timeout responses (503s)<commit_after>﻿// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;

namespace Microsoft.Azure.WebJobs.Script.WebHost.Handlers
{
    public class EnsureHostRunningHandler : DelegatingHandler
    {
        private readonly TimeSpan _hostTimeout = new TimeSpan(0, 0, 30);
        private readonly int _hostRunningPollIntervalMs = 500;
        private WebScriptHostManager _scriptHostManager;
 
        public EnsureHostRunningHandler(HttpConfiguration config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _scriptHostManager = (WebScriptHostManager)config.DependencyResolver.GetService(typeof(WebScriptHostManager));
        }

        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // some routes do not require the host to be running (most do)
            bool bypassHostCheck = request.RequestUri.LocalPath.Trim('/').ToLowerInvariant().EndsWith("admin/host/status");

            if (!bypassHostCheck)
            {
                // If the host is not running, we'll wait a bit for it to fully
                // initialize. This might happen if http requests come in while the
                // host is starting up for the first time, or if it is restarting.
                TimeSpan timeWaited = TimeSpan.Zero;
                while (!_scriptHostManager.IsRunning && (timeWaited < _hostTimeout))
                {
                    await Task.Delay(_hostRunningPollIntervalMs);
                    timeWaited += TimeSpan.FromMilliseconds(_hostRunningPollIntervalMs);
                }

                // if the host is not running after our wait time has expired
                // return a 503
                if (!_scriptHostManager.IsRunning)
                {
                    return new HttpResponseMessage(HttpStatusCode.ServiceUnavailable)
                    {
                        Content = new StringContent("Function host is not running.")
                    };
                }
            }

            return await base.SendAsync(request, cancellationToken);
        }
    }
}
{'fraction_non_alphanumeric': 0.07021188201080183, 'fraction_numerical': 0.004777731616119651, 'mean_word_length': 2.11046511627907, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5935473', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1107, 'n_words': 345}