Monday, July 23, 2012

Greed scoring solution for the Edgecase Ruby Koans.

Edgecase has some nice koans for learning Ruby.  Going through them is a good refresher for some of the Ruby features you don't use that often, or the parts of Ruby where the principle of least surprise has gotten into the weeds (multiple assignment i'm talking to you). I wrote mine as a class and did TDD so I have my own test suite, but when I plug it into the koans, all is well. My original solution had a bunch of conditional statements, but some refactoring got rid of most of them. Using modulo was the key. Have a look, feedback is welcome.

Tuesday, July 17, 2012

Scale worker processes in Heroku for Sinatra with Delayed Job

I've done this a few times for some of my personal projects and it works pretty well. Every single time I do it I always have the same problems so here is a checklist to keep you from going off the rails (HAHAHA!).
  • Make sure you've got the right gems: delayed_job, delayed_job_active_record, workless, heroku
  • Make sure you've configured delayed job to use your workless process scaler, and your scaler matches the heroku stack you are running
  • Create a Procfile with your worker definition (see gist above)
  • Set all required environment variables: APP_NAME, HEROKU_PASSWORD, HEROKU_USER
  • I usually use the heroku scheduler app add-on to run my rake task that adds stuff to my jobs table.
I've seen the following HTTP errors when running misconfigured apps:  
  • 404.  This is what you'll get if you are missing the APP_NAME because  if you look at the ps_scale method in the heroku gem, it is using that name to build a URL so without it your URL will be broken.
  • 401. This is what you'll get if you forget to set the username and password environment variables because the ps_scale method of the heroku api requires authentication.
  • 422 (Unprocessable Entity). This is what you'll get if you forget the procfile.  It is a shitty error message that is trying to tell you that heroku doesn't know how to create a worker process if you don't define it. Ick.
Allright.  Best of luck future me, and whoever else finds this useful.