Ask a Jedi: Turning a site off

This post is more than 2 years old.

Ryan asks:

I've got a site built for one of my clients, but I'm having a little trouble trying to implement a new function to the site. I need a way to "turn off" or "shut down" the site when I am performing maintenance. I also want the client to be able to perform this function as well. I need to know which type of variables to use. I've dealt mostly with session variables and as I understand it, session variables timeout. I need this variable to last indefinitely, or at least until the site is turned back "on". Is this done with server variables? Do you just set the server variable and then redirect to a maintenance page? I need the end result to be: If anyone visits the site, and it is "down", it redirects to a user-friendly maintenance page. What is the simplest way to achieve this? Thanks for all of your help.

There are a few ways to handle this. First off, you probably want to shut down the public portion of your site and not the entire site. You said you wanted the client to be able to do it. That means you obviously need a way to turn it back on via some admin interface. So the first thing you would to be sure of is that you allow the admin to still run. This could be done by checking the current request. Consider this onRequestStart code block from an Application.cfc file:

<cfif not findNoCase("/admin", arguments.thePage)> <!--- include the 'Sorry, we are down page' ---> <cfabort> </cfif>

Rememeber that onRequestStart is passed the name of the file to process. By checking that we can determine if the request is for the admin or a public page. So this code handles the actual shutdown, but not how it is done.

Most likely you want to use an application variable to handle the "Shutdown" state. So your code could be modified like so:

<cfif application.shutdown and not findNoCase("/admin", arguments.thePage)> <!--- include the 'Sorry, we are down page' ---> <cfabort> </cfif>

Application variables only last as long as the application stays active, and with the site shutdown, that might not be more than the default 2 hours. Obviously you don't want to site to turn back on when it times out. I normally store my application defaults in either an INI or XML file. (See my series on ColdFusion and configuration.) The configuration file is read into Application variables at startup to enhance performance. If you want your "Shutdown" to persist, you will need to ensure your admin tool writes back to the configuration file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Tom Mollerus posted on 8/7/2006 at 8:38 PM

Ryan,

Ray has the technical details covered for turning on what I'll refer to as "maintance mode"; I'll add my two cents on what to show the public. When our site is in maintenance mode, we redirect to an HTML page which shows a nicely-formatted page telling the user that we're undergoing planned maintenance and that they should check back in a little while. Note that I'm calling an HTML page just in case the maintenance is disrupting the performance of the CF server. There's also a meta-refresh in the HTML page set for 10 minutes, so that when we turn maintenance mode back off people who have the maintenance page in their browser will be returned to the working site.

See http://www.mystockoptions.c...

Comment 2 by Raymond Camden posted on 8/7/2006 at 8:47 PM

Tom, that auto-refresh is a darn smart idea.

Comment 3 by Cedric Villat posted on 8/8/2006 at 12:29 AM

You could also create a maintenance.cfm file when you are running your maintenance script, then just check for it's existence. Then, when you are done, simply delete the file. This would also eliminate the timeout problem Ray mentioned.

Plus you save a bit of time, since you don't care about the contents of the file as you would in an .ini or .xml file, you just care about if it exists.

Comment 4 by Raymond Camden posted on 8/8/2006 at 12:32 AM

Well typically you still need an ini/xml file for config information anyway, so I wouldn't say you don't care. The issue with this approach is that it requires the client to ftp, which, for many clients, is too much work. ;)

Comment 5 by Cedric Villat posted on 8/8/2006 at 1:36 AM

You don't HAVE to FTP :) You could have the admin interface create an empty file for you, and then delete it afterwards. But I agree you still have config information, so its not much of a savings, but it's an alternative.

Comment 6 by Dale Fraser posted on 8/8/2006 at 4:35 AM

Here is a real easy way.

In application.cfm

put this
<cfparam name="application.shutdown" default="false" />
<cfif isDefined('url.shutdown')>
<cfset application.shutdown = url.shutdown />
</cfif>
<cfif application.shutdown>
Sorry Site Undergoing Maintenance
<cfabort />
</cfif>

Easy? Then to shutdown your site, from any page just append a url paramater.

&shutdown=true

When it's ready to go again do

&shutdown=false

Now if your worried about security, add some IP checking or similar, but this is real easy, no admin pages etc.

Comment 7 by Raymond Camden posted on 8/8/2006 at 4:54 AM

My objection to this though is that if the server reboots, the application var will reset. I believe the original writer wanted something that would stick until, um, unstuck.

Comment 8 by Dale Fraser posted on 8/8/2006 at 5:07 AM

Raymond,

I don't see any mention of a need to reboot the server. But if you did, you could use the same method and just create a file / delete a file when the url paramater was switched.

Add an additional block using if fileExists to set the initial value.

Comment 9 by Raymond Camden posted on 8/8/2006 at 5:44 AM

The user said, indefinitely, which to me means forever. ;)