ColdFusion Muse

ASK-A-Muse: Checking for Site Availability

Mark Kruger June 3, 2011 6:34 PM ColdFusion Comments (8)

Muse Reader James Asks:
I have the following situation. I need to run a .cfm page that has a redirect in it to another server that serves Joomla pages. I need to find a way to test and see if that server is down or not serving pages. If it is down or not serving pages, I want to stop the redirect to that server and send the user to a page on my server. Any suggestions?

This is fairly easily accomplished using CFHTTP. When the page loads you can hit your Joomla server with a CFHTTP call and a timeout value. You could, for example, use code like this:

<!--- if this timesouts or errors you
        will stay here on this server --->

<cftry>
<cfhttp url="http://www.myjooomlaserver.com" timeout="10"/>
<Cfcatch>
    <cflocation url="someFileOnYourServer.cfm"/>
</CFCATCH>
</cftry>
<!--- Else we'll send you on ahead. --->
<cflocation url="http://www.myjooomlaserver.com"/>

But you probably don't want to do this for the simple fact that it doubles the amount of traffic your Joomla server will experience. For every request to your page you will generate a "test" request as well as the request that you "forward". A better solution would be to create a scheduled task that checks the page every minute or 2 and sets an application variable like so:

<!--- Check to see if it's up and insure variable is set. --->

<cftry>
<cfhttp url="http://www.myjooomlaserver.com" timeout="10"/>
<cfif NOT application.isJoomlaUp>
    <cfset application.isJoomlaUp = true/>
</cfif>

<Cfcatch>
    <cfset application.isJoomlaUp = false/>
</CFCATCH>
</cftry>

Then of course in your main page you only need to check your application variable like so:

<!--- check the app variable. --->
<cfif application.isJoomlaUp>
    <!--- send user on --->
    <cflocation url="http://www.myjooomlaserver.com"/>
<cfelse>
    <!--- Stay here --->
    <cflocation url="someFileOnYourServer.cfm"/>
</cfif>

Of course this still affects traffic but potentially much less so. If you are concerned about stats another approach would be to create a page on the server that is only for your test request. I would suggest that it be a page that includes a selection from the database since that is one of the items that often "brings down" a server. And of course my readers also have additional suggestions.

  • Share:

8 Comments

  • Dan G. Switzer, II's Gravatar
    Posted By
    Dan G. Switzer, II | 6/3/11 6:52 PM
    Actually, the better practice would be to use the method="HEAD" on your CFHTTP call.

    The "HEAD" method works just like the "GET", but the server shouldn't send back a message response, which reduces the bandwidth used.

    Also, you probably want to check to see what the status code returned from the CFHTTP operation was too, to ensure that you're getting a 200 success message.
  • Mark A Kruger's Gravatar
    Posted By
    Mark A Kruger | 6/3/11 6:55 PM
    @Dan,

    Ok.. but will HEAD actually cause the page to execute? For example, will database calls still be made?

    If so it's a great solution since minimizes bandwidth.

    -Mark
  • WilGeno's Gravatar
    Posted By
    WilGeno | 6/3/11 8:10 PM
    That depends Mark - if the server being tested the cfhttp "head" will receive a "50x" status code. If the site is working you should get a "200 OK". But a status "200 OK" may not mean that a connected DB server is working. If there is sufficient error catching on the site being tested it could server up a friendly "Sorry we're down" page and still give 200 OK. But, I have seen it that without that error handling if a connect DB erred then you should get a 503 status.
  • Mark A Kruger's Gravatar
    Posted By
    Mark A Kruger | 6/3/11 8:42 PM
    @Wil,

    Hmm... ok thanks. I guess the appropriate code would actually switch through the various status codes then and make appropriate assumptions. Sounds like a follow up post :)

    -Mark
  • Dan G. Switzer, II's Gravatar
    Posted By
    Dan G. Switzer, II | 6/3/11 11:50 PM
    @Mark:

    I just want to point out that your code doesn't even test if the page executes properly--all it does is makes sure that the server returns something within 10 seconds. The page could still be returning an error.

    Ideallistically all sites would return a non-20x status code if a problem has occurred, but often sites still return a 200 status ok even if an error has occurred (because they're catching the error and throwing a friendly error.)
  • Mark A Kruger's Gravatar
    Posted By
    Mark A Kruger | 6/4/11 12:10 AM
    @Dan,

    Yeah... I was sort of considering timeouts in my head instead of "all" the possibilities. I "assumed" the user meant that the Joomla server would periodically hang - but as has been mentioned by you and Wil - there are a number of other test conditions of which to take note.
  • Larry C. Lyons's Gravatar
    Posted By
    Larry C. Lyons | 6/9/11 3:42 PM
    Just to continue with Dan's comment. Regardless of whether the page is there or not, the server returns a 200 status code.

    One approach, mentioned before, was to use an application scoped variable to reduce server trips. That's a good step, but instead of looking at just the status code, grab the page's content and test for whether a phrase or word is there. If so the page is alive. If the find returns a 0 then obviously the page is down. Then put that result in the application scoped variable.
  • James's Gravatar
    Posted By
    James | 6/27/11 6:29 PM
    I have read all the comments. The assumption is that the Joomla server will hang. The server is all the way up or all the way down. I did not want test for any intermediate states.