Change ActiveRecord Pool Size on the Fly

I have a rake task that runs using the application’s production environment. Unlike the production environment though, it is threaded. In order to make any real use out of that, I need to increase the ActiveRecord connection pool size. Normally you’d do this in your database.yml, but in this case I’d rather not modify the production environment’s settings for the sake of a single rake task.

Here’s how you can make the change, on the fly, inside of your rake task:

task :swimming_pool => :environment do
  ActiveRecord::Base.connection_pool.instance_variable_set('@size', 15)
  ActiveRecord::Base.connection_pool.instance_variable_set('@timeout', 10)
  # Do awesome threaded stuff here
end

The first line of the rake task increases your pool size and the second line changes your timeout when waiting for a connection from the pool. You can adjust those values to whatever you need. That just happened to be what I need for this particular task.

Posted in Code, ruby at June 16th, 2010. No Comments.

Earlier today, Elad Meidar asked on Twitter about how to bypass checking the Authenticity Token in Rails for an action, sometimes. The example he mentioned was for a write API but this could theoretically be used for other situations where you only want to skip the authenticity token check of an action under specific circumstances. We went back and fort

First off, you need to do some before filter work:

skip_before_filter :verify_authenticity_token, :only => [:your_action]
before_filter :semi_verify_authenticity_token, :only => [:your_action]

Then you need a function to define when to check for the token authenticity:

def semi_verify_authenticity_token
  verify_authenticity_token unless request.xhr? # Or whatever other criteria you would use
end

All you really have to do then is make sure that verify_authenticity_token gets called based on the params or request and you should be set. This can be useful for APIs or AJAX calls calls to a given action where you don’t mind skipping the token check, but you still want to enforce it for the regular HTML browser view.

Posted in Uncategorized at April 7th, 2010. No Comments.

Code Highlighting

Just a small thing. I was trying to find a good syntax highlighter for code on this blog and another one I’m about to start. After trying a few, I settled on highlight.js from http://softwaremaniacs.org/soft/highlight/en

You basically need to simply wrap your code in <pre><code></code></pre> and it handles the rest. Here’s an example:


class ThisIsRuby

  def fun
    puts "This is fun!"
  end

end

test = ThisIsRuby.new
puts test.fun

On a semi-related note, I’m still chasing down some nginx config related gremlins. If you see anything weird, let me know in the comments.

Posted in Uncategorized at December 9th, 2009. 2 Comments.

WebbyNode, Nginx, and Percona MySQL

I’ve been having some issues with the slice that the blog and some other applications were on the box.  Over the past few weeks, MySQL has been crashing or perhaps SliceHost was rebooting my server and MySQL was not coming back up.  It was all very mysterious to me, but I was unhappy with the overall setup anyway.

WebbyNode is a newer player in the VPS game and they offer basically the same setup as SliceHost at a lower price.  I also like their ReadyStacks and github friendliness.  I registered and built out a box with Nginx and Percona MySQL.  Everything feels fairly snappy when I’m on the box despite being a low memory VPS.

Going forward I’m going to be moving Twexaminer.com over to this same VPS and run it through Nginx with Unicorn.

If anyone has any questions about the setup, let me know.  If I get a chance later, I’ll try to come up with a short guide on how I got everything working.

Posted in Uncategorized at December 7th, 2009. 2 Comments.

libxml-ruby vs nokogiri vs hpricot

Update: Aaron told me that he is going to be re-running the benchmarks this weekend so we’ll get a more complete set of data from the machine that originally ran the tests.

If you’re into parsing XML or HTML with ruby then chances are you’re familiar with the various gems out there for getting the job done.  Lately, there have been a lot of things flying around about which is the fastest and to settle it, Aaron Patterson (author of Nokogiri and Mechanize) wrote a test suite.

After it’s release, RubyInside posted about how the tests showed how fast Nokogiri was compared to Hpricot in this article here: Ruby XML Performance Shootout: Nokogiri vs LibXML vs Hpricot vs REXML.  Later in the day, I saw Why’s posting about the release of Hpricot here: hpricot 0.7 and decided to modify Aaron’s tests to use Hpricot.XML and here are the results:
Read More…

Posted in Comedy, New Stuff, Software Development, ruby at March 18th, 2009. 2 Comments.

you need to write me [updated]

About 20 minutes ago I entered a situation where every time I ran a script on my machine, the only output would be “you need to write me”

Naturally I was a little freaked out.

After retracing my steps over the past hour I remembered I had updated my ruby gems with a good ol “sudo gem update.”  I do it all the time so I didn’t see the cause for concern.  I went and looked at the newly installed gems and saw that there was libxml-ruby-1.0.0.  I browsed inside the gem and saw that it had a bin directory that had a ruby executable in it.  Cute.  Whoever the person is who released that needs to pay super close attention to what they are doing in the future.

Anyhow, I uninstalled the gem and when it asked if I wanted to remove the ruby executable I said yes.  This of course trashed the ruby executable in my /usr/bin.  Luckily I was able to retrieve it from Jay Amster and all was well.  If I was to do things over I’d say not to trash the executable and just delete the gem and all of its files.

Having that broken ruby executable in my path devastated my system though.  Half of my Textmate scripts no longer worked, none of my rails apps would execute, etc.  It was awful.  Thankfully I was able to figure it out quickly and hopefully if you run a search for “you need to write me” then you’ll stumble upon this post and know what to do to fix your machine.

UPDATE:

It would appear as though this problem is now resolved.  Maybe I got a bad install of the gem?  Maybe it was just a fluke?  Who knows?  It appears safe to install the latest libxml-ruby now though.

Posted in ruby at March 6th, 2009. No Comments.

Ruby String#Nameize Revised

This morning, Kevin Glowacz (@kevinglowacz) replied to me a few times on Twitter about Ruby String#Nameize class extension I had posted a while back.  I had done some work to it after posting it here.  Kevin also asked me a few questions about oddities that were in it that have since been resolved.  So thanks to his prodding, you get a slightly updated version…

The only real “feature” is that it will now handle full names just fine. Otherwise, the rest of the stuff was mostly performance related.  Here it is:

class String
  # Extension of the string class to properly handle camel names
  def nameize
    if self.match(/ /)
      # If the name has a space in it, we gotta run the parts through the nameizer.
      name = self.split(' ').each { |part| part.nameize! }.join(' ')
      return name
    elsif self.match(/^[A-Z]/)
      # If they took the time to capitalize their name then let's just jump out.
      return self
    else
      # If there are no spaces and there is no prior
      # capitalization then let's downcase the whole thing.
      name = self.downcase
    end
    # Let's now assume that they were lazy...
    return case
    when name.match(/^mac/)
      name.gsub(/^mac/, "").capitalize.insert(0, "Mac")
    when name.match(/^mc/)
      name.gsub(/^mc/, "").capitalize.insert(0, "Mc")
    when name.match(/^o\'/)
      name.split("'").each{ |piece| piece.capitalize! }.join("'")
    else
      name.capitalize # name is a first name or it's not Irish then capitalize it.
    end
  end

  def nameize!
    replace nameize # BANG!
  end

end

As always – question, comments, suggestions – shoot me an email, leave a comment, or hit me on Twitter (@PatrickTulskie).

Posted in Code, Software Development, ruby at December 30th, 2008. 5 Comments.

Every Friday we have our code reviews at BeenVerified and it is definitely a non-trivial event.  Our development team looks through the code all together and offers suggestions and ways to improve what the creator deems near-complete code.  Code reviews have become my favorite part of team based development because they offer me such a badass opportunity to learn more.  Everyone looks a problem differently and so getting insight from other people is huge because you might not consider all of your options when you’re knee deep in 1000 lines of ruby, CSS, and js all at once.

Yeah it’s great, except this Friday it didn’t happen.  Thanksgiving weekend happened instead so we pushed it to Monday.  Being the silly goose I am, I decided to get a new Macbook on Black Friday.  I restored my stuff from Time Machine, installed my Ruby Gems, and thought all was well.  Monday morning, my turn to present code came up and there was a problem with screen sharing.  Crap.  All of my code is in a git branch that is not pushed to a server yet and the time it would have taken to get to a state where we could present it from another machine would have been too much so we postponed my review until my screen sharing would work.  This was most displeasing to me. Read More…

Posted in Code, Software Development, Uncategorized, ruby at December 3rd, 2008. No Comments.

Leopard and MySQL Gem

Those of you doing rails development work on Leopard with MySQL have probably seen this error message when starting your app:

WARNING: You’re using the Ruby-based MySQL library that ships with Rails. This library is not suited for production. Please install the C-based MySQL library instead (gem install mysql).

Normally I don’t care, but I figured since I was doing some cleanup today and getting things ready to move on to a longer term it might be good to have a properly working MySQL gem.  I like to run with a system that is close to what we run production.  The closer you get, the less surprises you have when you push it live.

Read More…

Posted in Code, Software Development, Uncategorized, ruby at November 6th, 2008. 2 Comments.

The Tie Breaker [Updated]

I have this disturbing lack of confidence in both presidential candidates.  I don’t really want either of them.  Today I was trying to decide who to vote for because, well, it is election day.  I was walking into the office from lunch when it dawned on me: the best way to pick a president is based on their website.

Presidential Candidate Websites.

Shock and horror came over me as I scanned their websites.  There was stuff all over the place, explosive gradients and backgrounds that simply don’t jive.  Youtube videos are injected in every nook and cranny.  Since both websites are full of crap I don’t fell like reading or watching, I thought that I’d go a step further and do a good ol’ W3C Markup Validation on them.  The results were astounding to say the least.

John McCain: 171 Errors, 46 Warnings (217 problems)
Source: W3C

Barack Obama: 220 Errors, 37 Warnings (257 problems)
Source: W3C

You would think that with the Democratic party doing anything and everything to get Barack all over the media, news, magazines, to the point where I check under my bed at night to make sure he’s not there, they would have made sure his website was W3C compliant.  If he wins, what does that say about their concern for web standards.  It makes his “net neutrality” talks sound like complete crap.  McCain’s site is not much better than Obama’s but the fact is that McCain’s site is better.  Winning is winning.

You’d think that my selection process would be over, but you would be wrong.

VP Candidates.

I went to JoeBiden.com and SarahPalin.com and both were serious let downs.  Biden freeloads off of Obama’s website.  Palin’s website says “This space intentionally left blank.”  Serious disappointment.  Not even a picture of Palin for me to hang up on the wall of my gun locker.  Biden too… dude what’s up with that?  You gotta have Obama carry you through the whole damn election?  Complete rubbish from both the elephant and the ass.

For giggles, I ran ‘em through anyway…

Sarah Palin: 5 Errors, 0 Warnings

Joe Biden: 72 Errors, 29 Warnings

…and let me tell you, I did giggle quite a bit.

Because of how atrocious both VP candidates’ sites are, I won’t be including them in my decision.  Their awfulness cancels each other out.  If you wanted to play a numbers game though, Biden’s piggybacking on Obama’s site does more hurt than good.

The Tie Breaker.

Don’t listen to my drivel.  This blog post is balderdash.  You shouldn’t judge your presidential candidate based on how compliant their website is with W3C standards, even if I am.  When you don’t like either candidate, you need to pick one small issue that you can judge the candidates on go from there.  That’s the tie breaker.  Could be something as lofty as religious beliefs or something as trivial as who has a better choice of neck ties.

Fact of the matter is, you need to go out and vote, no matter how irrational and illogical your selection process might be.

Update

Just tested Change.gov and I’m slightly more impressed.  Seems like since Obama is now president-elect he is able to rocket past anything McCain could ever compose with a WYSIWYG with a cup of coffee.  He has created quite a website that is pretty damn compliant.

Change.gov: 20 Errors, 3 Warning

This makes me feel much better, because the one thing that keeps me up at night is web standards.  Seems like we’re getting much closer to changing.

Posted in Opinion at November 4th, 2008. No Comments.