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:
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:
Then of course in your main page you only need to check your application variable like so:
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.
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.
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
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
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.)
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.
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.