onServerStart and handling a bad startup

This post is more than 2 years old.

I spoke with Hemant at CFUNITED about onServerStart and what should happen if something goes wrong in the method. Currently if an error occurs, it gets logged, but the server just carries on. I made the argument that the server should stop loading as something has gone wrong. I also made the argument that it should act like onApplicationStart - where if you intentionally return false, the application will not load.

After speaking with Adam Lehman though, I think he had a good counterpoint. (Oh, and my idea was shut down, so it's not changing in the final release. ;) First, Adam made the point that ColdFusion should always try to start. Fair point. He then made another suggestion which I thought was good. If we check something, like a datasource, and are unable to connect, then it may make more sense to simply use a server variable as a flag. Our applications could then check that flag in onApplicationStart and react accordingly.

I have to say - I didn't really think I'd make much use of onServerStart, but I think I came up with a pretty good use case. If you host your ColdFusion site on a VPS and want to get a notice when the service restarts (in case you aren't the one doing it!) then this simple example will handle that:

component {
public function onServerStart() {

	writeLog(file="myserver",text="Server starting up.");
	myMail = new mail(to="ray@camdenfamily.com", from="ray@camdenfamily.com", subject="Server started", body="The server has started. Woot");


Normally I check the server.log file when I'm worried about my ColdFusion service restarting, but this would be a more in your face type approach.

Does anyone else have any plans for this feature?

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 Scott P posted on 8/20/2009 at 12:48 AM

yeah - I gotta agree with adam/adobe on that one - the server should always start.

one use case - adding the server dynamically to load balancer pools when it starts.

Comment 2 by Mike P posted on 8/20/2009 at 1:27 AM

Is this a cf9 function?

Comment 3 by Raymond Camden posted on 8/20/2009 at 1:29 AM

@MikeP: Yes.

Comment 4 by Tim Cunningham posted on 8/20/2009 at 1:33 AM

We have other servers which may need to know if the CF server has started back up. For instance, we have an external credit card system that lets people pay their insurance bills online, that system can really only work if the coldfusion server for the insurance system is up and running. Currently we poll a heart beat service, but I suppose we could use the startup feature to send notice to the payment system that it is back in business.

Comment 5 by JV posted on 8/20/2009 at 4:22 PM

We use this feature to define several server variables (datasource names, address of other systems to link to, etc), so we can move our apps between production, test, and development regions seemlessly. It has been very useful.

Comment 6 by Ben Nadel posted on 8/20/2009 at 9:48 PM

I always thought it was interesting, but never something that would solve a problem that I am currently experiencing.

Comment 7 by Adam Cameron posted on 8/21/2009 at 9:21 PM

Hi Ray
I think you're probably aware of my thoughts on this (via another channel), but I think there's use-cases for both notions that either the onServerStart handler is "fire and forget" (as it is) or "halt if it doesn't run". However the way it currently is allows one to emulate the latter easily enough by setting SERVER.bStartedOk as the last step of the handler. Slightly hokey, but enables both approaches.

My only use-case (so far) for onServerStart is to email me when it runs. Purely because of the implication that if the server has just started... it must have stopped recently. That's actually very important info to know, so just that by itself is enough validation for the functionality in my view.


Comment 8 by Raymond Camden posted on 8/21/2009 at 9:23 PM

Yeah - you know, the more I think of it, the more I think I'll definitely put an email me cfc on this server itself (I'm running 9 here).