I would like to have a onServerStart() event, similar to onApplicationStart() but for server wide functions. There I could set global fucntions, variables and also start vital applications in a controlled fashion.
We’ve been discussing this for 4.0.
Bruce Kirkpatrick commented
Michael told me in a group thread that you can use the event gateway features for this because it runs before any requests do. If you CFLOCK the server scope, you should be able to do all your startup routine. The exact code and documentation is something I'd have to locate if I do this though. Allow me to provide a code example of how to achieve this with just a simple application.cfm or cfc though:
<cfif structkeyexists(server,'appinited') EQ false or (isDefined('zreset') and (zreset EQ 'server' or zreset EQ 'all'))>
<cflock scope="server" type="exclusive" timeout="60">
<cfif structkeyexists(server,'appinited') EQ false or zreset EQ 'server' or zreset EQ 'all'>
<!--- init here --->
By having the same logic tests outside AND inside the lock, you guarantee that no 2 requests will do the heavy work at the same time. I've been using this for years with success. It does make logical sense to have an onServerStart function, but keep in mind that you can make one yourself right now and it will work with Application.cfc if you call the method manually in your component - I did this already. You might have more then one app using server scope, so I usually prefix my server variables with an app id string.
GX, as I see it I would put it in the Railo install package, a Server.cfc is not so far away from an Application.cfc
The idea is to be able to give more than one application access to certain common data and/or functionality without having them all trying to initialize it at the same time when the server comes alive. Currently you need to do quite a bit of locking and checking before using common resources.
This would also be very useful for virtual hosting since the applications could rely on the existence of certain object and certain data.
Just like onServerStart() in Adobe ColdFusion, I expect. Although we'd probably implement it per web context rather than server-wide because of the way Railo deals with web contexts.
Interesting idea but where would you put it? onApplicationStart goes in application.cfc of relative application would you put this in one of any of the applications on the server? how would this impact shared virtual hosting? It sounds like you have a shared library and need a way to always include and call some basic init methods.