Reset Your Application - wget and Ant

I'm using Ant now to do all my deployment to our servers. Today I decided to see if I could use wget to reset my application after a build. I like to do this just to make sure after I deploy a new build - that everything is reset in case I added something that needs to be set/reset in the application scope.

First I downloaded wget for Windows. There seem to be a lot of versions of wget floating around but I picked this version because it has SSL/HTTPS support compiled in and I need to run this against a secure server.

Simply copy the wget.exe executable into your C:\WINNT dir (on Windows XP) so that you can execute wget from any location.

In my application I have a bit of code which will reset my application if I pass a query string in the URL:

http://localhost/index.cfm?reset=true

And in my Application.cfc I have:

CODE:
  1. <cfif StructKeyExists(URL,"reset")>
  2.   <cfset this.onApplicationStart()>
  3.   <cflocation url="reset.html" addtoken="false">
  4. </cfif>

So I initially tried to run the same thing using wget:

C:\>wget http://localhost/survey/?reset --spider -d

A few notes on the command line switches: --spider prevents wget from downloading anything, -d - enables debug which is useful during initial testing, and -- help will display all available options.

This worked on my localhost - but when I tried it on the live server I received "ERROR 500: Internal Server Error".

After a bit of trial and error the culprit turned out to be the cflocation after the reset. When I removed that - everything worked!

So in order to work around that I added another parameter to wget - a user-agent string:

C:\>wget https://my.secure.url/index.cfm?reset --http-user=MyUsername --http-passwd=${my.password} --spider --no-check-certificate --user-agent=wgetreset --no-verbose

I'm passing in my authentication using --http-user and --http-passwd. I'm setting the value of the 'my.password' property earlier when I checkout my code from SVN - see this post for more information. I also set the user-agent string using --user-agent=wgetreset.

Now I simply modified my application.cfc to prevent the cflocation from firing if the request was made via wget:

CODE:
  1. <cfif StructKeyExists(URL,"reset")>
  2.  <!--- need to lock this call to prevent any race conditions --->
  3.  <cflock scope="application" timeout="10" type="exclusive">
  4.   <!--- restart the application --->
  5.   <cfset this.onApplicationStart()>
  6.   <cfif NOT cgi.HTTP_USER_AGENT IS "wgetreset">
  7.     <cflocation url="reset.html" addtoken="false">
  8.   </cfif>
  9.  </cflock>
  10. </cfif>

And finally in my build.xml file I added a new target to perform the reset:

CODE:
  1. <target name="cleanUp" description="Resets the application">   
  2. <exec executable="wget" outputproperty="wget.output" resultproperty="wget.result">
  3. <arg line="https://my.secure.server/index.cfm?reset --http-user=${svn.username} --http-passwd=${svn.password} --spider --no-check-certificate --user-agent=wgetreset --no-verbose"/>
  4. </exec>
  5. <!-- Check the status of the reset operation -->
  6. <condition property="status" value="FAIL">
  7.  <contains string="${wget.output}" substring="ERROR 500"/>
  8. </condition>
  9. <condition property="status" value="SUCCESS">
  10.   <contains string="${wget.output}" substring="200 OK"/>
  11. </condition>
  12.  
  13. <echo>
  14. ------------------------------------------------
  15. Files moved to ${project.build.final} successfully!
  16. ------------------------------------------------
  17. Application reset: ${status}
  18. ------------------------------------------------
  19. </echo>
  20.  
  21. </target>

This runs at the end of my build. It executes wget and dumps the results of the command into a property ${wget.output} which I then check to see if it ran OK or if there was an error.

4 Comments

  1. Posted June 11, 2007 at 12:32 pm | Permalink

    (Had to redo this with no start and end brackets) Just FYI you can do this without the need for any special download:

    exec executable=”C:/Program Files/Mozilla Firefox/firefox.exe”
    arg value=”http://mysite.com/myapp/index.cfm?reload=true”
    /exec

  2. Posted June 11, 2007 at 12:51 pm | Permalink

    Brian - I did see that in the Ant docs but there is no way to pass the HTTPS info using that method unless I missed something?

  3. Posted June 11, 2007 at 1:05 pm | Permalink

    Ahh, I never tried it with https, but I would think that just using https in the URL you give Firefox would work? If not, then yep, the wget is probably the way to go.

  4. Posted June 12, 2007 at 8:47 am | Permalink

    Sorry - I had a case of protocol confusion yesterday!

    Yes - that would work for HTTPS - I also had to authenticate on our server hence the username/password and wget…

Creative Commons License

Copyright © 2007 thecrumb.com. All rights reserved.