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.
Archived Comments
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...
Tom, that auto-refresh is a darn smart idea.
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.
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. ;)
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.
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.
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.
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.
The user said, indefinitely, which to me means forever. ;)