Make a Startup Task Perform Different Actions on the Compute Emulator and the Cloud
Updated: May 9, 2014
You can have your startup task perform different steps when it is operating on the cloud compared to when it is on the compute emulator.
It is occasionally necessary for your startup task to perform different tasks at startup when you are running on the cloud or on the compute emulator. For example, you may want to use a fresh copy of your SQL data at startup when running on the compute emulator, but not perform that step when running on the cloud. For another example, you may wish to perform debugging steps on the compute emulator that you would not want to perform on the cloud, and there may be performance optimization steps on the cloud that are not useful or necessary on the compute emulator. And while much effort has gone into making the compute emulator behave essentially the same as the cloud, there are still subtle differences between the two which could require that different steps be performed during startup when running on the compute emulator than on the cloud. These differences between the compute emulator and the cloud are described in the article Differences Between the Compute Emulator and Windows Azure.
This ability to perform different actions on the compute emulator and the cloud can be accomplished by creating an environment variable in the ServiceDefinition.csdef file, then testing the environment variable in your startup task.
To create the environment variable, add an Environment section and a Variable section to the Startup section of the ServiceDefinition.csdef file. The Variable element must have a unique name for the environment variable that will be used in the startup task. In this example, the environment variable is named ComputeEmulatorRunning. Within the Variable section of the ServiceDefinition.csdef file, add the RoleInstanceValue element with an xPath attribute equal to
"/RoleEnvironment/Deployment/@emulated". This xPath attribute is functionally equivalent to the statement
String ComputeEmulatorRunning = RoleEnvironment.IsEmulated.ToString().ToLower();
In this example, The value of the ComputeEmulatorRunning environment variable will be
"true" when running on the compute emulator, and
"false" when running on the cloud.
<Startup> <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple"> <Environment> <!-- Create the environment variable that informs the startup task whether it is running in the Compute Emulator or in the cloud. "%ComputeEmulatorRunning%"=="true" when running in the Compute Emulator, "%ComputeEmulatorRunning%"=="false" when running in the cloud. --> <Variable name="ComputeEmulatorRunning"> <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" /> </Variable> </Environment> </Task> </Startup>
The Startup.cmd batch file uses the ComputeEmulatorRunning environment variable to perform different actions based on whether the Startup.cmd batch file is running on the cloud or the compute emulator.
REM Check if this task is running on the compute emulator. IF "%ComputeEmulatorRunning%" == "true" ( REM This task is running on the compute emulator. Perform tasks that must be run only in the compute emulator. ) ELSE ( REM This task is running on the cloud. Perform tasks that must be run only in the cloud. )
ConceptsRun Startup Tasks in Azure