June 16, 2022
Adding an Emergency Shutoff Switch to Your Applications
Comments
(0)
June 16, 2022
Adding an Emergency Shutoff Switch to Your Applications
Newbie 29 posts
Followers: 6 people
(0)

Have you ever been to a gas station and noticed the emergency shut off switch?  It’s usually near the door to go inside the gas station and bright red to grab your attention.  That button is for emergencies, like say a fire, and will shut off all electricity to the pumps so the gas no longer flows.  This will mitigate any further damage from happening if an issue comes up or could even prevent one.  Wouldn’t it be nice to have one of those for our applications if we need it for some reason.  A nice shiny button we can hit to shut down access to the application for some planned or unplanned reason.  That could definitely come in handy.

In my case, our database servers are managed by another group and our applications rely on those databases to keep them available to the users.  Once a quarter it all goes offline for system maintenance to install security patches, software upgrades, hardware upgrades and replacements, etc. etc.  That means my apps are left hanging out there and only useful for generating timeout after timeout as users try to use the apps but the databases are not there.  It would be really handy if I could just flip a switch to put my applications in “maintenance” mode and give the users a nice friendly message about what is going on and how long to expect the application to be down.

Here is how I built my emergency shut off switch.

First I made a directory named “emergencyShutoff” on my server.  Next I went into the ColdFusion Administrator and created a new mapping named “emergencyShutoff”.  “Mappings” are listed under “Server Settings” in the left hand menu of the administrator.  Once on the mappings page you can see how the default mappings are entered as a guide to making your new “emergencyShutoff” mapping.

Once made, that mapping is now available to every application on my server to use.  The next step I make a “emergencyShutoff.cfm” file and place it in that “emergencyShutoff” directory on the server I just created.  In that emergencyShutoff.cfm file I have the simple block of code below.

<cfscript>
shutoff.active = 1;
shutoff.startDate = ‘6/18/22’;
shutoff.endDate = ‘6/19/22’;
</cfscript>

That’s all I need in that file.  It is pretty self explanatory, first I indicate if the switch is on or off with the “active” key in the structure.  The “startDate” and “endDate” just define the window that the application will be unavailable.

Next, the application needs to check the shutoff to see if it’s active and if we are within the start and end date window.  The application needs to check this on every request.  The reason I say every request is that in my case, during the quarterly maintenance, the servers come back and then go back down several times over the window.  So one minute the app is back up and the next it is back down.  I am using FW/1 as my framework in my example so for me the place to put that check is in the application.cfc and the setupRequest() function.  If you are using a different framework there should be a similar location.  If you are not using a framework it would still be the application.cfc file but the onRequestStart() function.  This is the code.

function setupRequest() {
        // Don’t check if we are on the maintenance page already.
        if ( !findNoCase( ‘maintenance’, cgi.QUERY_STRING ) ) {
            // Check the shutoff switch
            include “/emergencyShutoff/emergencyShutoff.cfm”;
            // if the shutoff switch is on and we are within the date range send them to the maintenance message
            if ( shutoff.active && now() >= shutoff.startDate && now() <= shutoff.endDate ) {
                location( ‘?action=maintenance’, 0 );
            }
        }
    }

On every request the emergency shutoff will be checked and if it is turned on then the user is redirected to a page of my choosing where I can display a friendly message. Nothing else runs so no errors are generated. With a flip of the “active” key in the “shutoff” structure the app is shut off. By specifying the start and end dates I am covered when the system maintenance window is over because the application automatically turns back on. I don’t have to be available to turn the switch back off if I am out of the office, in a meeting, or if it is later in the evening.  Same with taking the apps offline, I can set that ahead of time.

It is important to note this check: “if ( !findNoCase( ‘maintenance’, cgi.QUERY_STRING ) ) ” in my function. The only time I don’t want to check the shutoff switch is if I am already on the maintenance page. I am using the CGI variable because in my framework I do not have access to my normal variables that hold the URL values since I am checking the shutoff switch before anything else runs.

The shutoff switch could be expanded to also include time in the start and end date entries so the app could be shut off for just a few hours or more specific times over a few days. In my case the windows are full days so midnight to midnight works for me. This switch is for use across all of my applications but I could easily make it app specific by putting a specific shutoff file for each application and including that specific file in the setupRequest() function of the application.

I generally don’t like running something every request that may or may not be used but I think it is warranted here. However, I happen to know that our system maintenance windows always occur over a weekend on Saturday’s and Sunday’s. So I expanded my function just a little to only check the shutoff switch on those days. It probably doesn’t cut down on much processing time but it makes me feel better. My final version is below.

function setupRequest() {

// Only check if today is Saturday or Sunday as these are days when maintenance can occur
// Don’t check if we are on the maintenance page.
if ( ( dayOfWeek( now() ) == 7 or dayOfWeek( now() ) == 1 ) && !findNoCase( ‘maintenance’, cgi.QUERY_STRING ) ) {

// Check the shutoff switch
include “/emergencyShutoff/emergencyShutoff.cfm”;

// if the shutoff switch is on and we are within the date range send them to the maintenance message
if ( shutoff.active && now() >= shutoff.startDate && now() <= shutoff.endDate ) {
location( ‘?action=maintenance’, 0 );
}

}

}

I hope this helps and comes in handy for someone who has a need sometimes just to take an application offline for some reason and for only a specific time period.

0 Comments
Add Comment