Azure: How to execute a startup task delayed?

风格不统一 提交于 2020-01-04 05:59:20

问题


I have two Sites within my WebRole and have defined a Startup task.

The first line works fine, it creates a new App pool for me:

%WINDIR%\system32\inetsrv\appcmd.exe add apppool /name:"VCPool" /managedRuntimeVersion:"v4.0" /managedPipelineMode:"Integrated"

Now I would like to change my second to this new created AppPool, but adding another line right after, doesn't help.

%WINDIR%\system32\inetsrv\appcmd.exe set app "WebRole_IN_0_VC/" /applicationPool:"VCPool"

It seems the second site is somehow not yet ready.

How can I delay my task by 30 seconds or delay appcmd.exe slightly? Unless there is a way to create dependencies for this startup task that it shall only be executed when that second site is up and running?

Any help would be highly appreciated, Many Thanks,

Is there a way to delay this execution by 30 seconds to make sure the second site is up and can be changed?

Update:

Thanks for the hints. I have made further investigation into this matter. I have found OnStart() event.

1) But since I am using silverlight and simply wrap the existing Web project in the Cloud Roles project, I wouldn't have a WebRole.cs as such. Can I just add it to my Silverlight Web project and use it there? Or is it recommended creating a WebRole project from scratch and make it to replace the Silverlight Web project alltogether?

2) Regarding the <Runtime/> tag in the service definition, do I simply add it like this? Would it have any security implications when the webrole runs elevated?

<WebRole name="WebRole" enableNativeCodeExecution="true" vmsize="ExtraSmall">
    <Runtime executionContext="elevated"/>
    <Sites>
      <Site name="WebRole" physicalDirectory="./WebRole">
      ...
    </Sites>
</WebRole>

3) Last but not least how do I run a cmd file or in fact this line

%WINDIR%\system32\inetsrv\appcmd.exe set site /site.name:"WebRole_IN_0_VC" /[Path='/'].applicationPool:"ASP.NET v4.0" >>Log.txt

in the OnStart() method?


回答1:


Can you explore using PowerShell scripts for performing these tasks. PowerShell has a way to sleep thread. Something like

Batch file

%WINDIR%\system32\inetsrv\appcmd.exe add apppool /name:"VCPool" /managedRuntimeVersion:"v4.0" /managedPipelineMode:"Integrated"
powershell -command "Set-ExecutionPolicy Unrestricted" 2>> err.out 
powershell .\sleep.ps1 2>> err.out
%WINDIR%\system32\inetsrv\appcmd.exe set app "WebRole_IN_0_VC/" /applicationPool:"VCPool"

I have not tried but it should work. See this post to know about Powershell integration.




回答2:


The site, as you have discovered, is created after the 'Task' section runs. Instead, run your code from the 'OnStart' event. The site will have been created by that point. You may need to update your Service Definition file to run your role 'elevated' This can be done by adding this tag:

<Runtime executionContext="elevated"/>

Edited

To answer your further questions:

1) Whatever project you have, you should just be able to add the RoleEntryPoint class. You may have to do this manually.

2) Adding the runtime tag won't add any significant risk to your deployment.

3) Create a cmd file to put your command in (i.e. OnStart.cmd) and use some code like this:

System.Diagnostics.Process.Start("OnStart.cmd")

More information on starting a process here: http://msdn.microsoft.com/en-us/library/53ezey2s.aspx




回答3:


For those interested ... I just blogged about how to execute AppCmd startup tasks on Windows Azure configure IIS websites - including finding the site by web role name, and executing it delayed so that the site config is already created by Azure's IISConfigurator.exe.

More here: http://mvolo.com/configure-iis-websites-windows-azure-startup-tasks-appcmd/.

Thanks, Mike



来源:https://stackoverflow.com/questions/8287088/azure-how-to-execute-a-startup-task-delayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!