azure是否阻止同时回收角色实例



我有一个web角色部署到两个实例,应用程序池回收超时设置为默认值29小时,应用程序池空闲超时

azure是否注意不同时回收多个实例的应用程序池?否则:我该如何防止这种情况发生?

Azure不监控w3wp或应用程序池,也不协调不同实例之间的回收时间。为了防止应用程序池同时在多个实例之间循环使用,您应该修改每个实例的时间,例如<29小时+IN_#*1小时>,使得IN_0将下注设置为29小时,IN_1将下注30小时,IN_2将下注31小时,等等。

我的一位同事提供了这个代码:

using System;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.Web.Administration;
namespace RoleEntry
{
    public class Role : RoleEntryPoint
    {
        public override bool OnStart()
        {
            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
            int instanceScheduleTime = 0;
            int.TryParse(RoleEnvironment.CurrentRoleInstance.Id.Substring(RoleEnvironment.CurrentRoleInstance.Id.LastIndexOf("_") + 1),out instanceScheduleTime);
            string roleId = string.Format("{0:D2}",(instanceScheduleTime % 24));
            TimeSpan scheduledTime = TimeSpan.Parse(roleId + ":00:00");
            using (ServerManager serverManager = new ServerManager())
            {
                 Configuration config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
                ConfigurationElement applicationPoolDefaultsElement = applicationPoolsSection.GetChildElement("applicationPoolDefaults");
                ConfigurationElement recyclingElement = applicationPoolDefaultsElement.GetChildElement("recycling");
                ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
                ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");     
                bool alreadyScheduled = false;
                foreach (ConfigurationElement innerSchedule in scheduleCollection)
                {
                    if ((TimeSpan)innerSchedule["value"] == scheduledTime)
                        alreadyScheduled = true;
                }
                if (!alreadyScheduled)
                {
                    ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
                    addElement1["value"] = scheduledTime;
                    scheduleCollection.Add(addElement1);
                    serverManager.CommitChanges();
                }
            }
           return base.OnStart();
        }
    }
}

最新更新