Using Windows Azure SDK 1.3 or later we have the ability to launch a process (called a startup task) like:
- Before a role starts and the role will be started after the completion of the task.
- A Role will not wait for a startup task to finish, instead it will launch just after the startup task is executed. Both role and startup task would be independent.
- We can run a role as long as the startup task is running.
To add a startup task, we need to include the following lines of code in the service definition file (.csdef) as shown below.
- <ServiceDefinition name="MyService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
- <WebRole name="WebRole1">
- <Startup>
- <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple">
- </Task>
- </Startup>
- </WebRole>
- </ServiceDefinition>
In the preceding example, Startup.cmd is CMD file (ANSI) or any executable or batch file.
The command line attribute is the executable File Name, that will execute as the startup task.
Execution Context
- Elevated: This will cause the startup task to run in Administrator mode.
- Limited: This will cause the startup task to run with the same privileges as the role does.
Task Types
- Simple
This task would need to be complete before the role starts. If the task will not complete, then the role will be stuck and wait until the task completion.
- Background
The task would be independent of the role. First the startup task will execute and immediately after the role and even when the task is not completed, the role will start separately from the startup task.
- Foreground
This would behave the same as a background task. However the role will stay as long as startup task is running. We can choose it, if we want the role instance to stay up as long as the task is running.
Do you know that, IIS will shut down the site upon the following events?
- After being idle for 20 minutes.
- Every 29 hours (due to recycling).
App pool Idle Timeout
The default idle timeout for IIS application pools in the windows Azure is twenty minutes. (The default IIS behavior.) That means the app pool would shut down after twenty minutes if it is not being used which means that no event single request has been sent. So, the application would be slower after twenty minutes of inactivity.
To change the idle timeout to zero, add the following startup task declaration in the service definition file.
- <Startup>
- <Task commandLine="disableTimeout.cmd" executionContext="elevated" />
- </Startup>
Add the following line of code in the cmd file named disabletimeout.cmd.
Ensure that disabletimeout.cmd's Copy to Output Directory setting is “Copy Always”.
%windir%\system32\inetsrv\appcmd set config -section:applicationPools-applicationPoolDefaults.processModel.idleTimeout:00:00:00
Periodic Restart (http://msdn.microsoft.com/en-us/library/bb386463(v=vs.90).aspx)
By default the worker process in the application pool will be recycled after a specified amount of time.
The specified amount of time would be 29 hours.
To set the recycle time, add the following lines of code in the service definition file.
- <?xml version="1.0" encoding="utf-8"?>
- <ServiceDefinition name="MyWindowsAzureProject" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
- <WebRole name="MyWebRole" vmsize="Small">
- ...
- <Startup>
- <Task commandLine="Startup\RecycleTime.cmd" executionContext="elevated" />
- </Startup>
- </WebRole>
- </ServiceDefinition>
Add the following lines of code in the RecycleTime.cmd (or .exe or .batch) file.
REM *** Prevent IIS app pool recycles from recycling on the default schedule of 1740 minutes (29 hours).
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00.
Both command lines for idle timeout and periodic recycle time can be included in a single file and can be executed at a time.