问题
I have a web role deployed to two instances with the app pool recycle time-out set to the default of 29 hours, and the app pool idle timeout set to zero. I want to keep this app pool recycle time out to make sure that my application remains healthy over time. However I do not want that my two instances (accidentally) recycle at the same time to make sure that my application remains responsive to users.
Does azure take care that the application pools of multiple instances are not recycled at that same time? Or else: how can I prevent this situation?
回答1:
Azure does not monitor w3wp or your application pool, nor does it coordinate recycle times between the different instances. In order to prevent the application pool recycling between multiple instances at once you should modify the time for each instance, for example something like <29 hours + IN_# * 1 hour> such that IN_0 would bet set at 29 hours, IN_1 at 30, IN_2 at 31, etc.
A colleague of mine provided this code:
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();
}
}
}
来源:https://stackoverflow.com/questions/28674888/does-azure-prevent-that-role-instances-are-recycled-at-the-same-time