Using Startup Task in Windows Azure detailed summary

Using Windows Azure SDK 1.3 or later you have ability to launch a process (called startup task) depend on your choices such as:

  • 1. Before your role starts and once task is finished then Role will start
  • 2. Role will not wait for startup task to finish instead it will launch just after startup task is executed. Both role and startup task would be independent in this context
  • 3. Or you can run your role as long as your startup task is running

To add a Startup task to your role you just need to add the <Startup> <task /></Startup> entries in the Service Definition file (ServiceDefinition.csdef) similar to as below:

<ServiceDefinition name="MyService" xmlns="https://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">

   <WebRole name="WebRole1">

      <Startup>

         <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple">

         </Task>

      </Startup>

   </WebRole>

</ServiceDefinition>

 

In above example the Startup.cmd is a properly create CMD file (or it could be a batch file or an EXE itself) which contain the startup application.

The commandLine attribute is the executable File Name, which is going to execute as startup task.

In Above the executionContext attribute can be:

  • 1. Elevated: This will cause startup task to run in Administrator mode.
  • 2. Limited: This will cause startup task to run with the same privilege as the role does

In above setting the taskType attribute can be:

  • 1. Simple: Means this task would needs to be complete before the Role Starts. If task will not complete and exit, the Role will stuck to wait till the task completion. Simple tasks run until they exit, and then the next task (or the RoleEntryPoint if this is the last startup task) is executed.
  • 2. Background: This task would be independent from the Role itself. First startup task will execute and immediately after the Role, and even when this task is not completed, the Role will start separately from the startup task.
  • 3. Foreground: The startup task will run and then Role will start so it does behave same way as the background task, however the role will stay running as long as startup task is running. You can choose it, if you want the role instance to stay up as long as the task is running. Also the role shutdown can only happen when all the foreground tasks exit.

Important Notes: 

  • For task type simple, they are expected to return an exit code 0 to indicate successful completion. Without 0, the next task or role will not start.
  • If the commands in the cmd file do not adhere to convention of returning 0 for success, you may need to add “exit /b 0” to force it. Reference https://blog.smarx.com/posts/asp-net-mvc-in-windows-azure

 

Here are a few scenarios to understand it much better:

[Scenario 1]

<Startup>

  <Task commandLine="Startup1.cmd" executionContext="limited" taskType="simple"/>

  <Task commandLine="Startup2.cmd" executionContext="limited" taskType="simple"/>

</Startup>

 

In this situation First Startup1.cmd task will execute and when this task is complete and exit then Startup2.cmd will executed. Finally when this task is complete exit the Role will start.

[Scenario 2]

<Startup>

  <Task commandLine="Startup1.cmd" executionContext="limited" taskType="background"/>

  <Task commandLine="Startup2.cmd" executionContext="limited" taskType="simple"/>

</Startup>

 

In this situation First Startup1.cmd task will execute and just after Startup2.cmd will execute. Startup2.cmd task will not depend on Startup1.cmd task, however the Role will not start until Startup2.cmd task is complete and exit.

 

Running Startup tasks in User Context:

You must understand that these Startup tasks defined in Service Definition file, does not run in any user context and it is possible that when application you are going to run as Startup task needs an user account to execute otherwise it may return "Access Denied" Error. So you may think how to accomplish it. The only solution is the create a user account first within your startup task and then launch the startup task within the newly created user account.  David Aiken has written great article along with code snippet about running Startup task as a real user as below:

https://www.davidaiken.com/2011/01/19/running-azure-startup-tasks-as-a-real-user/

 

Debugging with Startup task along with Tips and Tricks:

Debug your application in compute emulator along with startup tasks and batch or command files is tricky task and no one knows better that Steve Marx so he created a very nice article about trips and trick using Startup Task as below:

https://blog.smarx.com/posts/windows-azure-startup-tasks-tips-tricks-and-gotchas

 

How to use Startup task in Azure by Windows Azure Documentation Team:

https://msdn.microsoft.com/en-us/library/gg456327.aspx