Azure: How to execute a startup task delayed?
Asked Answered
S

3

2

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?

Separate answered 27/11, 2011 at 16:35 Comment(0)
L
3

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.

Leoni answered 27/11, 2011 at 17:36 Comment(2)
Thanks for your response, so when I put powershell .\sleep.ps1 2>> err.out in there, do I have to populate the sleep.ps1 with this line: Start-Sleep -s 30 ? The question remains though, if running this task as background and having this 30 seconds delay would help, if the <sites> are created after the Tasks have been executed...Separate
the sleep.ps1 would contain code related to delay. You are right even if you run it as background task you cannot be sure if the second site has been created at all. If Powershell allows similar functionality like appcmd.exe. You can build a retry till success mechanism using it.Leoni
S
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

Suffragan answered 27/11, 2011 at 19:55 Comment(5)
So the Task section runs before Site is created? Thats bad news. So the delay won't work then. When you say 'OnStart' event, do you mean in ServiceDefinition file? I think your post got chopped off in the middle.Separate
OnStart is a method you should override in the class implementing RoleEntryPoint in your role (normally WorkerRole or WebRole).Suffragan
Hi Richard, You were right the tasks are really executed before Sites even generated. I can now see it in the log files. It seems your solution might be the most reliable. I even contacted Microsoft Azure support and sofar they couldn't help me. I would really appreciate it if you could read my updated section in the question and clarify it a bit further for me. Highly appreciatedSeparate
Hi @Kave, I have added some additional answers. I hope that helpsSuffragan
Thanks for all your help Richard. Since I am using PHPAzure, which in turn creates the Azure Package from the PHP files, I have no real control to override anything. My Silverlight Web project is simply published into a folder. I add the path of this folder to the physicalPath of the second site. If creating the package was driven from VS, it would have been no problem and your solution would have worked. In my case though unfortunately it doesn't. I had to do the hacky way of delaying the background task by 90 seconds. i wished there was a better way...Separate
B
1

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

Biological answered 16/7, 2012 at 17:25 Comment(1)
You should copy and paste the relevant portion from your blog to this answer. Without that, your answer is useless. When this link dies, your answer becomes useless.Kafir

© 2022 - 2024 — McMap. All rights reserved.