如何使启动任务幂? [英] How to make startup tasks idempotent?

查看:214
本文介绍了如何使启动任务幂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在批处理文件的一些启动任务。我特别呼吁IIS的 Appcmd.exe的配置IIS。在天青启动任务理应幂等(即,能够被具有相同的结果重复运行)的情况下的作用被重新启动的某些原因。不幸的是我的许多IIS配置命令都将因为他们删除配置节点第一次是那么不后续运行present围绕失败,第二次,如:

我的问题是,如何让这些启动任务幂?有没有一种方法,使Appcmd.exe的不是抛出错误?有没有一种方法,使外壳赶上错误?有没有一种方法,使Azure的架构忽略错误?

下面是我的启动任务的例子。这是所有包含在一个命令文件, configiis.cmd

  @REM启用应用程序/ JSON的MIME类型IIS COM pression
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置-section:system.webServer / httpCom pression /+\"dynamicTypes.[mimeType='application/json',enabled='True']/提交:APPHOST
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置-section:system.webServer / httpCom pression /+\"dynamicTypes.[mimeType='application/json;字符集= utf-8',使=真 ]/提交:APPHOST@REM IIS设置为自动启动AppPools
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置-section:applicationPools -applicationPoolDefaults.startMode:AlwaysRunning /提交:APPHOST@REM设置IIS不关闭空闲AppPools
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ appcmd设置配置-section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 /承诺:APPHOST@REM但不会自动启动,我们不使用AppPools,也不要关闭这些闲置时
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置-section:system.applicationHost / applicationPools/ [名称='经典.net程序池'] STARTMODE:按需/ [名称='经典.net程序池'] .autoStart:假/ [名称='经典.net程序池'] processModel.idleTimeout:00:01:00/提交:APPHOST
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置-section:system.applicationHost / applicationPools/[name='ASP.NET 4.0版'] STARTMODE:按需,/[name='ASP.NET V4 0.0]将自动启动:假/[name='ASP.NET 4.0版'] processModel.idleTimeout:00:01:00/提交:APPHOST
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置-section:system.applicationHost / applicationPools/[name='ASP.NET V4.0经典'] STARTMODE:按需/[name='ASP.NET V4.0经典]将自动启动:假/[name='ASP.NET V4.0经典'] processModel.idleTimeout:00:01:00/提交:APPHOST
@REM删除IIS响应头
%WINDIR%\\ SYSTEM32 \\ INETSRV \\ Appcmd.exe的设置配置/部分:httpProtocol /-customHeaders.[name='X-Powered-By']


解决方案

从@ Syntaxc4的回答旁白:考虑使用面包屑(文件)本地的。在脚本中,检查已知的文件存在(您创建)。如果它不存在,通过你的启动脚本,也创造了面包屑文件。下一次虚拟机启动时,它会再次检查面包屑文件是否存在,如果存在,退出CMD文件。如果面包屑文件中消失,这通常意味着你的虚拟机已经被重组的其他地方,将需要(或者一个新的实例或者在不同硬件上重生实例)和IIS配置。

I have a number of startup tasks in batch files. In particular I call IIS's appcmd.exe to configure IIS. Startup tasks in Azure are supposed to idempotent (ie, able to be run repeatedly with the same results), in case the role is restarted for some reason. Unfortunately many of my IIS configuration commands will fail the second time around, eg because they delete a configuration node the first time which is then not present on subsequent runs.

My question is, how do I make these startup tasks idempotent? Is there a way to make appcmd.exe not throw errors? Is there a way to make the shell catch the errors? Is there a way to make the Azure framework ignore the errors?

Here's an example of my startup tasks. This is all contained in a command file, configiis.cmd.

@REM Enable IIS compression for application/json MIME type
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost

@REM Set IIS to automatically start AppPools
%windir%\system32\inetsrv\appcmd.exe set config -section:applicationPools -applicationPoolDefaults.startMode:AlwaysRunning /commit:apphost

@REM Set IIS to not shut down idle AppPools
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00 /commit:apphost

@REM But don't automatically start the AppPools that we don't use, and do shut them down when idle
%windir%\system32\inetsrv\appcmd.exe set config  -section:system.applicationHost/applicationPools "/[name='Classic .NET AppPool'].startMode:OnDemand" "/[name='Classic .NET AppPool'].autoStart:False" "/[name='Classic .NET AppPool'].processModel.idleTimeout:00:01:00" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config  -section:system.applicationHost/applicationPools "/[name='ASP.NET v4.0'].startMode:OnDemand" "/[name='ASP.NET v4.0'].autoStart:False" "/[name='ASP.NET v4.0'].processModel.idleTimeout:00:01:00" /commit:apphost
%windir%\system32\inetsrv\appcmd.exe set config  -section:system.applicationHost/applicationPools "/[name='ASP.NET v4.0 Classic'].startMode:OnDemand" "/[name='ASP.NET v4.0 Classic'].autoStart:False" "/[name='ASP.NET v4.0 Classic'].processModel.idleTimeout:00:01:00" /commit:apphost


@REM remove IIS response headers
%windir%\system32\inetsrv\appcmd.exe set config /section:httpProtocol /-customHeaders.[name='X-Powered-By']

解决方案

Aside from @Syntaxc4's answer: Consider the use of a breadcrumb (file) locally. In your script, check for existence of a known file (that you create). If it doesn't exist, go through your startup script, also creating a breadcrumb file. Next time the vm starts up, it would again check for existence of the breadcrumb file and, if it exists, exit the cmd file. If the breadcrumb file disappears, this typically means your vm has been reconstituted somewhere else (either a new instance or a respawned instance maybe on different hardware) and IIS configuration would be needed.

这篇关于如何使启动任务幂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆