Setting Application Pool Idle and Recycle Timeout Period Using Startup Tasks - Windows Azure

Using Windows Azure SDK 1.3 or later we have the ability to launch a process (called a startup task) like:

  1. Before a role starts and the role will be started after the completion of the task.
  2. 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.
  3. 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.

  1. <ServiceDefinition name="MyService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">  
  2. <WebRole name="WebRole1">  
  3. <Startup>  
  4. <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple">  
  5. </Task>  
  6. </Startup>  
  7. </WebRole>  
  8. </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

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

Task Types

  1. 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.
     
  2. 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.
     
  3. 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?

  1. After being idle for 20 minutes.
  2. 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.

  1. <Startup>  
  2. <Task commandLine="disableTimeout.cmd" executionContext="elevated" />  
  3. </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.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ServiceDefinition name="MyWindowsAzureProject" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">  
  3. <WebRole name="MyWebRole" vmsize="Small">  
  4. ...  
  5. <Startup>  
  6. <Task commandLine="Startup\RecycleTime.cmd" executionContext="elevated" />  
  7. </Startup>  
  8. </WebRole>  
  9. </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.

Up Next
    Ebook Download
    View all
    Learn
    View all