Monday, May 18, 2009

Preventing Application Scope Conflicts with Dynamic Application Names

Today I was setting up a client's application to have separate Production and Staging environments; the team is already running our own local Development environments, but we didn't yet have a non-live location to show off current changes without dragging people over to one of the dev boxes.

Due to various restrictions, the staging environment needed to be on the same box (Running CF8 Standard) as the production app.  After setting up a separate database, dsn, etc, I ran into one more issue: Application variables.

CF stores application data based on the name of the application.  I now had two copies of the site on the same box, both using the same application name.  When I configured Staging to use the new DSN (stored via application.dsn), Production's DSN was also changed.

I needed to differentiate the applications' names based on their purpose.  I already had an external config file, but it was only being loaded on application start, and I didn't like the idea of running a CFINCLUDE on every page load just to get the "mode" of the current install.  Plus, this config file was written to set application variables... which wouldn't yet exist if I were to make this change, requiring more refactoring.

I thought about using the CGI scope to detect the requested domain name, but this particular client/application makes it likely that URLs could change.

I realized that the only thing I could use to uniquely identify a collection of files was the path to the actual location of those files.  Which led me to:

<cfset this.name = "myapp-#hash( getCurrentTemplatePath() )#">

getCurrentTemplatePath() returns the path to the currently executing CF file, which will always be the Application.cfc.  I then hash() that value to remove odd characters and standardize the length.

The added benefit of this is that EVERY installation of this application will have a unique application name, and it doesn't have to know anything about the other applications or what "mode" it is running under.  This could be helpful as I might need to add another copy of the site to the same server, as a sort of remote-hosted development location for a specific dev.

The other thing I learned today? I can't blog short even for simple topics.

No comments:

Post a Comment