There is frequently a need for page maintenance or database maintenance, without turning off a server. If you are at a remote location, wouldn't it be nice to be able to shut down access to your pages without having to call someone or pull up a remote desktop? With a couple simple variables and the Application.cfc file (or Application.cfm), you can create the functionality to shut off access to your site and return a simple message to any user that happens across your site while the maintenance is underway.

How it Works

We will have an application variable named maintenance that will be set to false while the application is in use. If this value ever gets set to true, the user sees a simple message or page instead of the page that he requested. This gives you time to do your thing -- update your data, upload your new files, or any other necessary action. Optimally, you would have a backup server to put in place while this happens, but in most cases it is acceptable to shut off the site for a minute or two while you work your magic. We set this application variable by using a URL variable on any page in the site -- if the URL variable exists, the application variable maintenance is toggled true/false, which toggles the functionality on and off.

Application.cfc or Application.cfm?

The Application.cfc is new to CF 7, but mimics some of the functionality that you have come to expect from the Application.cfm file. In that file, everything within the file would be executed upon each page request. In the Application.cfc file, if you want to execute some code upon page request, it would be placed in the onRequest method in Application.cfc.

The following code should be placed somewhere at the top of your Application.cfm file, or in the onRequest method of the Application.cfc, if you are using that in your site. The code is commented inline:

<cfset this.name="myapp">
<cfset this.password="mypassword">
<cfparam name="application.maintenance" type="boolean" default=false>
<!--- Regular maintenance is done when URL variable "maintenance" is used. Tell
people to come back later, and do not process the request further. --->
<cfif isdefined("url.maintenance") AND url.maintenance EQ this.password >
  <cfif application.maintenance EQ false>
    <!--- Maintenance off -- turn it on --->
    <cfset application.maintenance = true>
    <cfset application.maintenanceStart = now()>
    <!--- Log the start of the maintenance session --->
    <cflog file="#this.name#_maintenance" type="information"
text="Maintenance started at #application.MaintenanceStart#">
  <cfelse>
    <!--- Maintenance already set -- turn it off --->
    <cfset application.maintenance = false>
    <cfset totalMaintenanceTime = DateDiff("s", application.MaintenanceStart, now())>
    <!--- Log the end of the maintenance session --->
    <cflog file="#this.name#_maintenance" type="information"
text="Maintenance ended at #now()#: #totalMaintenanceTime# seconds">
  </cfif>
</cfif>
<!--- If the application variable is set to true, end processing here -- don't show the page. --->
<cfif application.maintenance EQ true>
  <cfoutput>The system is undergoing periodic maintenance. Please return again later.</cfoutput>
  <cfreturn false>
<cfelse>
  <cfreturn true>
</cfif>

Using the functionality

With this code in place, you can browse to any page in the site and put a URL variable named maintenance in the address bar with the password you inserted into the code:

http://yoursite/yourpage.cfm?maintenance=mypassword

The first time you use it, the application variable is set to true and the access to the page is cut off. If you use it a second time, the variable is toggled off so that the page is served normally.

Additionally, you can set up your own maintenance page to use instead of the simple message. Simply place a <cfinclude> in place of the message "The system is undergoing...etc".

Conclusion

With a simple variable toggle, you can turn access to your pages on and off.