【问题标题】:How to programmatically restart Application Pool when 503 occurs from Azure Web Role从 Azure Web 角色发生 503 时如何以编程方式重新启动应用程序池
【发布时间】:2012-11-21 13:12:47
【问题描述】:
【问题讨论】:
标签:
asp.net
iis
azure
azure-web-roles
【解决方案1】:
您可以从启动任务中运行the following script(确保创建提升的后台任务):
Timeout= 30000
set events = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecNotificationQuery("select * from __instancecreationevent where targetinstance isa 'Win32_NTLogEvent' and TargetInstance.LogFile='System' and TargetInstance.EventCode=5002")
Do
WScript.Echo "==========================================================================="
WScript.Echo "Listening for IIS Rapid Fail Protection Events"
Set objLatestEvent = events.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.Message
' get the AppPool name from the Eventlog message
appPool = objLatestEvent.TargetInstance.InsertionStrings(0)
WScript.Echo "Restarting Application Pool '" & appPool & "' in " & Timeout & " milliseconds"
WScript.Sleep(Timeout)
'construct ADSI path to failed AppPool and start by setting AppPoolCommand to 1
set pool = GetObject("IIS://localhost/w3svc/AppPools/" & appPool)
pool.AppPoolCommand = 1
pool.SetInfo
WScript.Echo "AppPool " & appPool & " restarted"
WScript.Echo "==========================================================================="
WScript.Echo
Loop
使用 WMI,它将侦听 IIS RFP 事件。这是通过将ExecNotificationQuery 与NextEvent 组合在一起来完成的。对NextEvent 的调用将阻塞,直到新事件到达。发生这种情况时,脚本会等待 30 秒并重新启动应用程序池。
无论如何,如果 RFP 启动,可能更适合了解您的流程为何一次又一次地崩溃。