Friday, 11 October 2013

Server.cfc and onServerStart()

G'day:
Another interesting thread cropped up on the Railo Google Group today: Micha was asking about Server.cfc, and how people use it (if at all).

This is not very widely used code, so I'll link to the docs for onServerStart() and quote a para from it:
ColdFusion now supports a CFC with an onServerStart method that runs only when the server starts. The onServerStart method takes no parameters, and is the only function in the CFC. The function is useful for application-independent tasks, such as instantiating the applications, configuring logging, or setting up the scheduler.
By default, ColdFusion looks for the onServerStart method in cf_webroot/Server.cfc.
Apparently Railo does not offer this functionality. But also have never had anyone notice this.

One thing that I always found wrong about ColdFusion's implementation is that they provide onServerStart(), but not onServerStop(). The supposed theory here is that a start is always a guaranteed operation, but a stop is not. IE: the server could crash, not shut down. This is a bit specious because the same could apply to onApplicationEnd(), onSessionEnd(), and onRequestEnd() too. This is just another case of Adobe second-guessing requirements, and - I suspect - in an effort to avoid work. Surely it's better to have the option of onServerEnd() (onServerShutdown(), surely), but just be aware that it might not run. Just like... any other code we write. But no.

Anyway, the only usage I ever heard was for people to warm their applications so that people's first requests of the application would not be slow as everything loads up. This is a bit daft for a coupla reasons:

  1. when the server starts, whether the application warms via onServerStart() or the more logical onApplicationStart(), they're still gonna have to wait until it's done. There is no gain to be had here.
  2. By its very nature, it's an application-specific event, not a server-specific one. So that functionality doesn't belong in onServerStart(). It belongs in onApplicationStart().
There's a case for if all applications on a server share some sort of resource which needs warming, then it's appropriate to do this in onServerStart(). Fair enough. I have not ever come across this situation though. That's not to say there aren't a plethora of them out there, just that I ain't come across one.

So here's my question to you: do you use onServerStart()? If so: for what?

Another question: would you use onServerEnd() if it existed?

What about any other server-centric events you'd like to have handlers for?

I'll feed any answers I get back to Railo, and Adobe as well.

Cheers.

--
Adam