Thursday, September 13, 2012

Workaround for AppHarbor's 20 minute app pool recycling

AppHarbor is an amazing service put in place for us .Net developers.  Free single processor web instances, built-in SSL, continuous builds with unit testing, and git deployment.  One big downside to their service, however, is that their servers will recycle the app pools after 20 minutes of inactivity.

(Taken from AppHarbor's FAQ page)

This does two negative things to your website.  1) It will timeout any user sessions regardless of what you specify the default timeout to be (again this assumes they're inactive for at least 20 minutes), and 2) it will unload the application completely so that when a user goes to your website, it has to go through all of the initial application load and startup events all over again (for my site this would usually take up to 15 seconds until the page responded).

So it seems the best option to solve this issue is to ping your website every 19 minutes.  But how do you do that with a solution that happens automatically, doesn't depend on any person or local machine with internet, and is cost-effective?

I found a free site monitoring tool called StatusCake that I'm now happily using to ping my website in 15 minute intervals.


I also tried out StillAlive which is an official AppHarbor addon that is free with limited functionality.  The main problem being it only allows you to make a call every 4 hours under the free plan.

Monitor.us is another free service that has tons of features that I looked into.  What I found was that StatusCake was a lot simpler to setup and manage.  Also, the lowest interval I remember seeing was 30 minutes but I could be wrong.

Thursday, March 22, 2012

Using ELMAH with Custom Errors turned on

This article specifically addresses handling uncaught exceptions in your web application. ELMAH can be plugged in pretty quickly with almost all of your changes happening in your web.config and global.asax.cs files.

Here is a slimmed down web.config file. When a 404 error occurs, it will not be logged to Elmah and the user will be redirected to a custom "Not Found" page. All other exceptions will be logged by Elmah and the user is redirected to a custom error page. To view the list of logged exceptions, the url is http://(yoursite)/elmah.axd (note that elmah.axd does not physically exist anywhere). Also, IIS will deny users that are not part of the "Developers" group to see the log page.
<?xml version="1.0"?>
<configuration>
  
  <configSections>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
  </configSections>

  <elmah>
    <security allowRemoteAccess="yes" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah" />
    <errorFilter>
      <test>
        <or>
          <equal binding="HttpStatusCode" value="404" type="Int32" />
          <regex binding="BaseException.Message" pattern="The file '/[^']+' does not exist" />
        </or>
      </test>
    </errorFilter>
  </elmah>
  
  <system.web>
    <customErrors mode="On" defaultRedirect="~/Home/Error">
      <error statusCode="404" redirect="/Home/NotFound" />
    </customErrors>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
      <httpHandlers>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
      </httpHandlers>
  </system.web>

  <location path="elmah.axd">
    <system.web>
      <authorization>
        <allow roles="Developers"/>
        <deny users="*" />
      </authorization>
    </system.web>
  </location>

</configuration>


In global.asax.cs I commented out the filter for the HandleErrorAttribute so that Elmah would handle the exceptions.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    // Commented out so that web.config can have customErrors="true" and still let Elmah handle exceptions
    //filters.Add(new HandleErrorAttribute());
}


So now when I bring up my elmah.axd page I don't see 404 errors when the browser is looking for the favicon.ico file which I was seeing a lot of previously.

Friday, March 16, 2012

Setting up a Color Picker for Sublime Text 2 (on Windows)

Sublime Text 2. You already know so I don't need to say it. Anywho, in Visual Studio one of my favorite extensions is devColor for picking hex colors in my stylesheets. In Sublime Text 2 you need to install one, it's not there by default sadly.

Here are instructions on how to do it step by step.
  1. Fire up Sublime Text 2
  2. Press CTRL+` (the key above the TAB key)
  3. You'll see a console window take up the bottom portion of the window. Paste the following block of code there
  4. import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'
    (This will install the Sublime Text 2 Package Control system which is similar to Gem, NuGet, NPM, etc.)
  5. Close Sublime Text and reopen it
  6. Press CTRL+SHIFT+P to open the Command Palette
  7. Start typing in "Package" and select the Package Control: Install Package option and hit Enter
  8. The dialog changes to a list of available packages. Type in "Color" and select ColorPicker
    (Credit goes out to Weslley Honorato for creating the package)
  9. No restart of Sublime required this time around. It's ready to use
  10. Open a stylesheet or an area where you want to insert a hex color
  11. Press CTRL+SHIFT+C to bring up the color selection dialog
  12. That's it!

Tuesday, February 21, 2012

How I use the web to keep organized

Springpad
This is my new favorite bookmarking and note taking tool.  I used to rely heavily on Evernote to keep notes and thoughts on various topics, as well as Google Docs for some other personal docs.  Springpad has a clever thumbnail layout, the ability to attach images and links to a single note, and a nice Chrome extension.

Minus
This site is pretty fantastic.  You can upload up to 5 GB worth of pictures.  I really like how they let you organize albums and slideshows.

Box
I haven't completely given up using Dropbox but Box.com is giving me a big case to do so.  The interface is very nice and the 5 GB limit by default is better than Dropbox's 2 GB.

BitBucket
Special thanks to Clark Sell for selling me on BitBucket (like what I did there?).  I had heard about BitBucket before but never realized they were giving away free unlimited private repositories.  At one point in time they only supported Mercurial but they now support Git as well.

AppHarbor
JC Grubbs told me about AppHarbor right before one of his talks on MongoDB.  A Heroku alternative for .Net developers, AppHarbor makes this an amazing time to be a developer if you want to vet out ideas for web apps.  Something I've been doing a lot of lately.

TeamBox
Free basic project management and task tracking.  I tried looking at Asana as well but the lack of time tracking was a deal breaker.

Blogger
Unless I've copied and pasted this post blindly, you're reading this post on Blogger.  I've played with Tumblr, Posterous, Weebly, LiveJournal, and Wordpress.  Blogger is what I've decided to settle with (for now) when it comes to technical blogging.

Posterous
For non-technical blogging I prefer Posterous.  They've done a lot of site upgrades recently and added some cool new features.  I especially like how their layouts look if you like your blogs to have a lot of pictures.  This made Posterous ideal for posts about my family.

ReapCast
There are a plethora of free blogs on the web that I'm interested in.  I wrote this site to be able to track the ones I want to listen to as well as have DVR functionality to manage that last listening point I was at for each one, 30 second skip, 7 second rewind, slider time shifting, and synchronizing with my iPhone (which has all the same controls).  Also included are bookmarks with note-taking.  Coming soon will be different playback speeds.

Developer tools

JsFiddle - Quick and dirty way to test out some CSS and/or javascript.
Cloud9IDE - Web-based editor for javascript and Nodejs.


Chrome extensions

WhatFont - Hover over text and see what font it is.
EyeDropper - Hover over colors on a page to see the hex and rgb coloring.
SmoothGestures - I use the Back button a lot.  With this I can hold the right mouse button and drag left to jump back in history.
Springpad Extension - Read my comments about Springpad above.
Web Developer - If you only install one extension, this should be the one.
Speed Dial 2 - Gives thumbnail previews of your favorites.  For $1.99 one time you can sync your bookmarks across all your instances of Chrome.