Application status page and continuous delivery

If you use Sitefinity CMS in continuous integration, testing, or delivery scenarios, you must modify your process. Because of the changes in the bootstrap of a Sitefinity CMS system, you may need to integrate a verification step that checks whether the system is up and running.

The bootstrap process of the system is done asynchronously and the application status page is being displayed while the system is still starting up. This means that page status code that is returned is 200, although Sitefinity CMS is in a process of starting up. This can break your integration tests and deployments, if you rely on successful response as a proof for a running Sitefinity CMS instance.

To ensure that your Sitefinity CMS application is running you should periodically request /appstatus route during application start, while the route is available. This route returns the same status code as the application status page - 200. When this route becomes unavailable - it returns status code 404, it means your application is up and running.

Following are two implementations of this periodical check:

C# implementation

C#
using Microsoft.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;

namespace CiUtilities
{
   public static class SitefinityStatusHelper
   {
       /// <summary>
       /// Waits for sitefintiy to start.
       /// </summary>
       /// <param name="testingInstanceUrl">The testing Sitefinity instance url.</param>
       /// <param name="totalWaitSeconds">the maximum wait time in seconds.</param>
       public static void WaitForSitefinityToStart(string testingInstanceUrl, double totalWaitSeconds)
       {
           // While /appstatus service is responding, Sitefinity is either in Initialize or Upgrade state.
           // Since Sitefinity Bootstrap is running on a different than main thread, getting of the tests is postoned until Sitefinity is up and running
           DateTime startTime = DateTime.UtcNow;
           TimeSpan elapsedTime = new TimeSpan();
           HttpResponseMessage response;
           HttpClient client = new HttpClient();

           do
           {                
               response = client.Get(testingInstanceUrl + "/appstatus");
               Thread.Sleep(1000);
               elapsedTime = DateTime.UtcNow.Subtract(startTime);

           } while (response.StatusCode == HttpStatusCode.OK && elapsedTime.TotalSeconds < totalWaitSeconds);

           if (elapsedTime.TotalSeconds > totalWaitSeconds)
           {
               throw new Exception("Sitefinity did not start in less than " + totalWaitSeconds / 60 + " minultes.");
           }
       }
   }
}

PowerShell script implementation

PowerShell
param([String]$url="http://localhost", [String]$successOuput="SUCCESS", [Int32]$totalWaitMinutes=10)

$elapsed = [System.Diagnostics.Stopwatch]::StartNew()

$statusUrl = ($url + "/appstatus")
Write-Warning ("Attempt to start Sitefinity up: " + $statusUrl)
$retryCount = 0;

try 
{ 
 Write-Warning ("Retry: " + $retryCount)
 $retryCount++;
 $response = Invoke-WebRequest $statusUrl -TimeoutSec 1800 -UseBasicParsing

 if($response.StatusCode -eq 200)
 {
   Write-Warning ("Sitefinity is starting ..." + $statusUrl)
 }  

 while($response.StatusCode -eq 200)
 {    
   Write-Warning ("Checking Sitefinity status: " + $statusUrl)Write-Warning ("Retry: " + $retryCount)$retryCount++;
   $response = Invoke-WebRequest $statusUrl -TimeoutSec 1800 -UseBasicParsing

   if($elapsed.Elapsed.TotalMinutes > $totalWaitMinutes)
   {  Write-Warning ("Sitefinity did NOT start in the specified maximum time")
     break
   }
Start-Sleep -s 1
 }
} 
catch 
{
 if($_.Exception.Response.StatusCode.Value__ -eq 404)
 {
   $response = Invoke-WebRequest $url -TimeoutSec 120 -UseBasicParsing

   if($response.StatusCode -eq 200)
   {
     Write-Warning ("Sitefinity has started after " + $elapsed.Elapsed.TotalSeconds + " second(s) - " + $successOuput)
   }
   else
   {
     Write-Warning "Sitefinity failed to start"
   }
 }
 else
 {
   Write-Warning ("Sitefinity failed to start - StatusCode: " + $_.Exception.Response.StatusCode.Value__)
   Write-Warning $_|format-list -force  Write-Warning $_.Exception|format-list -force
 }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.