Wed, 26 Jan 2011

Introduction to Windows Azure Startup Tasks

Startup tasks are a simple way to run some code during your Windows Azure role’s initialization. Ryan and I gave a basic introduction to startup tasks in Cloud Cover Episode 31. The basic idea is that you include some sort of executable code in your role (usually a batch file), and then you define a startup task to execute that code.

The purpose of a startup task is generally to set something up before your role actually starts. The example Ryan and I used was running Classic ASP in a web role. That’s quite a simple trick with a startup task. Add a batch file called startup.cmd to your role. Be sure to create it with notepad or another ASCII text editor… batch files created in Visual Studio seem to have a byte order mark at the top that makes them fail when executed. Believe it or not, this is the entirety of the batch file:

start /w pkgmgr /iu:IIS-ASP

Mark the batch file as “Copy to Output Directory” in Visual Studio. This will make sure the batch file ends up in the bin folder of your role, which is where Windows Azure will look for it:image

Create the startup task in by adding this code to ServiceDefinition.csdef in the web role:

<Startup>
  <Task commandLine="startup.cmd" executionContext="elevated" />
</Startup>

Note that this startup task is set to run elevated. Had I chosen the other option (“limited”), the startup task would run as a standard (non-admin) user. Choosing an execution context of “elevated” means the startup task will run as NT AUTHORITY\SYSTEM instead, which gives you plenty of permission to do whatever you need.

There’s another attribute on the <Task /> element, which I haven’t defined here. That’s the taskType attribute, which specifies how the startup task runs with respect to the rest of the role lifecycle. The default is a “simple” task, which runs synchronously, in that nothing else runs until the startup task completes. The other two task types, “background” and “foreground,” run concurrently with other startup tasks and with the rest of your role’s execution. The difference between the two is analogous to the difference between background and foreground threads in .NET. In practice, I have yet to see the “foreground” task type used.

The above code actually works. Add a default.asp, and you’ll see Classic ASP running in Windows Azure.

NOTE: In testing this code today, it seems that this only works if you’ve specified osFamily=”2” in your ServiceDefinition.csdef. (With osFamily=”1”, the behavior I was seeing was that pkgmgr never exited. I’m not sure why this is, and it could be something unrelated that I changed, but I thought I’d point it out.)

Download the Code

If you want to try Classic ASP in Windows Azure, you can download a full working Visual Studio solution here: http://cdn.blog.smarx.com/files/ClassicASP_source.zip

More Information

Wade Wegner (Windows Azure evangelist and new cohost of Cloud Cover) has also been working with startup tasks recently, so he and I decided to do a show dedicated to sharing what we’ve learned. We’ll record that show tomorrow and publish it on Channel 9 this Friday. Be sure to check out that episode.