Monday, 31 August 2009

CruiseControl.NET failing on SVN (or other source control) failure

Our SVN is offsite and hosted by another company.  It works really well, but occasionally the CruiseControl.NET server is unable to make a connection (for whatever reason) and the build fails.  This happens a lot when we’re doing backups, or if we have an outage or just occasionally for no reason at all.  We do 1440 build tests a day (that’s one a minute) so I suppose it’s no surprise.  We do roughly 2 check ins a day (small team) per project and we get roughly 2 failures a day, so my build history is roughly 50% failure (more if you count the weekends).

Well I’ve finally found out how to stop this from happening.  With the newer version of CruiseControl (well it’s not that new now, but no matter) there is a tag to allow for source control errors and decide what to do when they happen.

The attributes are added to your <project> tag:

   1: <maxSourceControlRetries>10</maxSourceControlRetries>
   2: <stopProjectOnReachingMaxSourceControlRetries>true</stopProjectOnReachingMaxSourceControlRetries>
   3: <sourceControlErrorHandling>ReportOnRetryAmount</sourceControlErrorHandling>

This is fairly straight forward, those tags will allow my source control to retry 10 times.  It will stop building my project when it fails 10 times and it will do nothing but report the number of retries if it fails.  Add these 3 lines to each of your projects and then when you bring down your version control system your build won’t fail.

Monday, 24 August 2009

jQuery Ajax submissions and invalid characters (like single quotes)

I’ve recently posted about submissions to ASMX services using Ajax to allow asynchronous data transfer to the client in a browser.  This works pretty well, but I’ve come across a problem with a particular invalid character when using my method.  The apostrophe.


   1: $.ajax({   
   2:     url: "ReviewHandler.ashx",   
   3:     data: “{   
   4:         'name': '" + $("#ReviewerName").val() + "',
   5:         'location': '" + $("#ReviewerLocation").val() + "'
   6:     }",   
   7:     processData: true,       
   8:     type: "POST",   
   9:     beforeSend: function() { startSubmit(); },  
  10:     complete: function() { submitComplete(); },  
  11:     error: function(xhr, statusText, errorThrown) { alert(errorThrown); },  
  12:     success: function(data) { submitSuccess(data); }  
  13: });

And this works fine.  But if you have an ‘ in your data, then obviously you’re code is going to explode.  Worst part is that it’s not going to be immediately obvious.  Your error text will just give you an internal server error message and you’ll need to use something like fiddler to nail the real reason that it failed unless you have great exception handling.

Well it’s very easy to make your code much more robust.  JSON.js is a set of standard JSON methods that can be used to serialise your objects into JSON strings and escape all your characters correctly for you.  Using JSON.js, the former becomes the following:

   1: var myObj = { 
   2:     name: name: $("#Name").val(),
   3:     location: $("#Location").val()
   4: }
   6: $.ajax({
   7:     url: "ReviewHandler.ashx",
   8:     data: JSON.encode(myObj),
   9:     processData: true,
  10:     type: "POST",
  11:     beforeSend: function() { startSubmit(); },
  12:     complete: function() { submitComplete(); },
  13:     error: function(xhr, statusText, errorThrown) { alert(errorThrown); },
  14:     success: function(data) { submitSuccess(data); }
  15: });

It’s neater for sure, and cleaner.  The encode method will escape all the illegal characters and allow it to work nicely with ASMX web service parameters.