Archive for November, 2009

Album of the year?

Sunday, November 29th, 2009

Is it just me, or were precious few good albums released in 2009? Even those fags at MetaCritic practically admit defeat with this lame-ass list. There’s not a lot on there you should bother with, trust me, I’ve heard enough of them to realise MC is grasping at straws.

So, dear reader, was there a good album released in 2009? To be honest, my favourite for the year so far is “The Boy Who Knew Too Much” by Mika. It pains me to call such mainstream sugar-pop the album of the year but where’s the alternative?

Rails 2.3.5 still broken on 1.9.1

Sunday, November 29th, 2009

So Rails 2.3.5 is out and while I don’t really know what’s new, I can tell you what isn’t – not running on the current stable version of the ruby programming language as reported by Ruby-Lang.

If you would like to start testing Rails on 1.9, you’ll need to apply the patches listed at this Lighthouse ticket, which fix the two most serious problems. A kind user down the bottom of that ticket has pulled the patches into monkeypatches suitable for just slipping into your config/initializers folder – I can confirm that they work for me.

I cannot fathom Rails Core’s bizarre refusal to pull these patches into 2.3.5. Yet another release goes out that is completely non-functional on the newest, fastest and best version of the language, forcing users to seek out and apply non-official monkeypatches – or just forsake 1.9 altogether, as the vast majority seem to be doing. In what way is this in the best interest of the framework or the community? What is holding them back? The patches might not be 100% perfect but they’re a lot better than what we have now, which is that out of the box Rails is broken on 1.9.1.

UPDATE: Happily, the 1.9.1-patched Rails does seem to be working on 1.9.2 as well. I’ve installed ruby 1.9.2dev (2009-09-07 trunk 24787) and my favourite “fussy site” is working there, too. That’s great news, because 1.9.2 is going to be the “serious” version of Ruby 1.9 and has further big performance increases.

i5 or i7

Friday, November 27th, 2009

So, I’m getting a desperately needed new iMac.

I’d been planning to get the i5, but after seeing this report I think I’ll plump for the i7. It’s on sale today for AUD$2,758.01 (don’t forget the 1c).

Killing a sentient being is murder, no matter what species

Saturday, November 21st, 2009

New research is confirming what interested parties have suspected for a long time - dogs are as intelligent as infant humans.

I am pretty dispassionate about which species is involved when it comes to ending life – the main factor is the intelligence and self-awareness of the victim. There are plenty of animals that are demonstrably self-aware, so in my view, it’s ethically worse to kill, say, an adult dog, than to perform an abortion. Or a dolphin, for that matter – or a pig, or an ape, or any other animal with clear signs of intelligence.

Glad to see more expert support that we should be taking the rights of animals more seriously. We are animals, after all, no matter what superstitionists insist, and by killing any other animal, we’re killing our own. Better have good reason.

Maglev hype train derailed at last

Saturday, November 21st, 2009

Ah, Maglev. It seems that only yesterday I was ripped to shreds by a furious mob for daring to question your public claims of drastic speed gains over every other Ruby interpreter.

And so imagine my delight today, when the first alpha release is finally available! Sure, it’s only an alpha release, so it’s not going to be quite as good as the final product – but surely it will live up to the claims in the RailsConf 2008 demonstration? I mean, it was that good even then! Surely it must be even better now!

Anyway, I obviously had to install it and put these claims to the test.

I followed the instructions on – with a couple of changes, such as that I use .profile instead of .bashrc.

I decided to test with as close to a real world library as I could. That post mentions that Maglev supports Sinatra, so I thought that would be an ideal test. Then again, it also says it supports RubyGems but I couldn’t get that to work at all. C extensions are out too, so we’ll be using Webrick.

To get around the RubyGems problem, I manually downloaded Rack and Sinatra and placed the contents of their lib directories into a single folder. I then created a trivial sinatra app as shown:

require File.expand_path(::File.dirname(__FILE__)) + '/sinatra.rb'
require File.expand_path(::File.dirname(__FILE__)) + '/rack.rb'
 
get '/' do
  "Maglev Rocks!"
end

I decided to run good old ab against it. My exact command was:

ab -kc 10 -t 10 http://127.0.0.1:4567/

Because I have other servers installed in Ruby 1.8.7 and 1.9.1, I forced sinatra to serve using webrick as follows:

ruby1.9 testmaglev.rb -s webrick

Considering the past hype of order-of-magnitude speed hikes, I was ready to be blown away. Are you ready to be blown away? Oh yeah baby, 50 times faster comin’ right up!

RESULTS

Ruby 1.8.7: Requests per second: 131.38 [#/sec] (mean)
Ruby 1.9.1: Requests per second: 144.77 [#/sec] (mean)
Maglev: Requests per second: 97.78 [#/sec] (mean)

Well golly gosh. Look at that. It’s not faster after all – in fact it’s climbing the same steep performance hill ever other interpreter has had to climb, Smalltalk magic notwithstanding. How about that.

I said the benchmarks shown at RailsConf were bullshit and would not even remotely reflect the real world performance of the final product. I said that until the interpreter implemented all of ruby, it was not ruby, and it was worthless to measure its performance. I poured skepticism upon the notion that a new interpreter, just by dint of some doubtfully superior Smalltalk heritage, could leapfrog all previous contenders with a two-order-of-magnitude performance boost.

Looks like I was right. But don’t take my word for it, run the tests yourself. I will be accepting contrite apologies in comments … ;)

Rails Metal MongoDB GridFS access

Friday, November 20th, 2009

I’m kind of new to Metal (and all things Rack) but this works. Just for reference.

# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
class ImageShow
 
  def self.call(env) 
    request = Rack::Request.new(env)
    if request.path_info =~ /^\/show_image\/(.+)$/
      if GridFS::GridStore.exist?(Media.database, $1)
        GridFS::GridStore.open(Media.database, $1, 'r') do |file|
          [200, {'Content-Type' => file.content_type}, [file.read]]
        end
      else
        [404, {'Content-Type' => 'text/plain'}, ['File not found.']]
      end
    else
      [404, {"Content-Type" => "text/html"}, ["Not Found"]]
    end
 end
 
end

That will need to be saved as image_show.rb. Slow .. but not as slow as doing it through a Rails controller.

Girl of the week

Friday, November 20th, 2009

Shiori Kanzaki

Rails kinda running on 1.9.1

Wednesday, November 18th, 2009

So I’ve been ranting recently about Rails’ lack of Ruby 1.9 support. And rightfully so! It’s inexcusable for the #1 Ruby framework to not support the fastest, most efficient version of Ruby, especially not after it’s been available for almost 2 years.

But for some people, it is kind-of possible to use Rails on 1.9 today. Rails’ 2.3-stable (“stable” in quotes) branch has fixed a few of the worst problems in 2.3.4, such as the string comparison bug which made authentication systems unworkable. The showstoppers for me, like the utf8 in templates issue, remain. I wouldn’t rely on it in production – not for a site which your job depends on, anyway – but as long as you don’t use certain features of rails (like i18n), and don’t need to deal with utf8 much if at all, you may be able to use it.

You should want to switch to 1.9.1 as soon as possible. I see between 20% and 50% higher performance than 1.8.7, with memory usage significantly lower and even better, less prone to leakage. In one of my projects there is a daemon which does nothing except wait for items to hit a queue and then launches a script to deal with them. In 1.8.7, it would gain a meg or so every few hours and I’d be nervous about leaving it running unmonitored. In 1.9.1 it sits rock solid on 6.5M for weeks on end. I am totally sold on 1.9.1.

Anyway, here’s the instructions to get Rails up to the latest possible usable state. Open a terminal at the root of your Rails app:

# head into /vendor
$ cd vendor
 
# clear out any other rails
$ rm -rf rails/
 
# clone rails into here
$ git clone git://github.com/rails/rails.git rails
Initialized empty Git repository in /Users/sho/projects/myproject.com/vendor/rails/.git/
remote: Counting objects: 123204, done.
remote: Compressing objects: 100% (27700/27700), done.
remote: Total 123204 (delta 95101), reused 121853 (delta 94037)
Receiving objects: 100% (123204/123204), 21.72 MiB | 380 KiB/s, done.
Resolving deltas: 100% (95101/95101), done.
 
# enter that directory
$ cd rails/
 
# checkout 2.3 stable, ignore the errors
$ git checkout -b  2-3-stable remotes/origin/2-3-stable
warning: unable to unlink arel: Operation not permitted
warning: unable to unlink rack-mount: Operation not permitted
Branch 2-3-stable set up to track remote branch 2-3-stable from origin.
Switched to a new branch '2-3-stable'
 
# go back to the root dir 
$ cd ../..
 
# run rails as 1.9.1. get ready to find out which gems you forgot!
$ ruby1.9 script/server
 
# hello, console
$ script/console --irb=irb1.9
Loading development environment (Rails 2.3.4)
>> RUBY_VERSION
=> "1.9.1"

And there you go. It’s working, kind of. Try it out.

I would probably recommend using thin as the web server, but if you need a working mongrel in 1.9, my gem is still available:

sudo gem1.9 install sho-mongrel

That will probably die when github pulls gems for real but who knows, maybe the official mongrel will have been fixed by then (HA!).

Meanwhile, I’m going to step up my harassment of the Rails Core team to get this crap fixed; it is a fucking JOKE that it still doesn’t work 100%.

update: moved the gem to gemcutter.

script/memcached

Wednesday, November 18th, 2009

The inimitable Wincent recently referred to a memcached script he had written that toggled whether memcached is running or not. I thought that was a great idea so wrote my own and in the spirit of comparing dicks I thought I’d post it here.

#!/usr/bin/env ruby
 
 = ::File.expand_path(::File.dirname(__FILE__)) + '/../tmp/memcached.pid'
 
def process_id
  File.exist?() ? File.read().to_i : false
end
 
def running?
  if process_id 
    Process.kill(0, process_id) == 1 rescue false
  else
    false
  end
end
 
def start!
  print 'starting memcached ...'
  system "memcached -d -P #{} -l 127.0.0.1"
  sleep 0.5
  print "started with pid #{File.read}"
end
 
def stop!
  print 'stopping memcached ...'
  Process.kill('INT', process_id)
  sleep 0.5
  File.delete() if File.exist?()
  print 'done.'
  puts
end
 
def ensure_running
  running? ? puts('already running.') : start!
end
 
def ensure_stopped
  !running? ? puts('not running.') : stop!
end
 
def toggle
  running? ? stop! : start!
end
 
case ARGV.first
when 'start'
  ensure_running
when 'stop'
  ensure_stopped
when 'toggle'
  toggle
when 'status'
  running? ? puts('running') : puts('not running')
else
  toggle
end

I love writing these kinds of scripts in Ruby. It’s perfect for it.

Cool Ruby one-liner

Tuesday, November 17th, 2009

Is the process 1234 running?

Process.kill(0, 1234) == 1 rescue false

Adrift on Rails

Monday, November 16th, 2009

Ruby on Rails development seems to be stalled, with no substantial release since March and many show-stopper bugs in the current version, 2.3.4, such as the claimed Ruby 1.9 support which does not work, meaning that the official Rails gem doesn’t run on the only version of Ruby that doesn’t leak memory like a firehose leaks water. Freezing to edge is broken and has been broken for months. Needless to say, that’s because master is now Rails 3.0pre which is in a totally unrunnable state.

The seems oddly low-volume, considering 2.3.5 is over a month late and 2.3.4 has so many problems. Working patches to fix critical errors languish unmerged for months, constantly needing to be regenerated to keep up – not that there’s anything official to keep up with, since there’s no real 2.3.5 target and master is fixated on the faraway 3.0. The Lighthouse tracking system is itself a hopeless, unnavigable mess.

So what’s going on with Rails? With the historical core developers absent or tied up with reinvent-the-world expeditions like the 3.0 rewrite, the projects appears directionless and adrift. Meanwhile the shine fades and word leaks out that all is not well in Railsville.

What happened? Two causes.

Firstly, the shoot-for-the moon total rewrite for Rails 3, leaving Rails 2 languishing for the better part of a year now and Rails 3 in a totally unrunnable state.

Secondly – well, this is just my opinion. But one of the reasons Rails was great at first because it was written by people who actually wanted to use it to make actual proper web apps. And then it became famous, and then it attracted another type of developer – people who just wanted their membership in Rails Core on their resumé – but they weren’t going to actually use it for real-world projects. So instead of getting fixes to seven-month-old show-stopper bugs, we get Engines and RESTful Resource Mapping and Application Templates, none of which are much used or useful in the real world.

Rails is looking sicker by the month. And since Core has bet the company on Rails 3.0, it better blow the world away and fix everything, or I have a feeling a lot of Rails devs are going to start looking for the next big thing. Or actually, the next small thing. I would trade off 90% of Rails’ useless bloat and feature creep for a minimalist framework that actually works, has developers who care, has a readable codebase and a clear direction. Basically, what Rails used to be before DHH got tired of saying “NO!” and it turned into a stumbling monster.

Sinatra? It’s possible but it’s a little too unstructured; great for a tiny API but I’d be worried about trying to use it for a large project. Django is by all appearances in even worse shape than Rails. Ramaze? Looks very interesting.

Update to turn down the tone a bit and correct some links. I will follow up with some constructive posts later; it is actually possible to run Rails on 1.9, just takes some work.

Upgrading ImageMagick on RHEL5

Friday, November 13th, 2009

Somehow my prior install of ImageMagick, a custom build from source, lacked proper PNG support. A bit of searching uncovered this excellent guide to installing it from a source RPM – but I had a few modifications to those instructions so decided to record my own.

Firstly, I had to uninstall my custom build, so off to the build directory (you should always keep these) to run sudo make uninstall. Or check for any old rpm installs: rpm -qa | grep ImageMagick.

Next, upgrading packages. Some I had, some I didn’t. You’ll need EPEL.

yum --enablerepo=epel --enablerepo=rpmforge install djvulibre-devel libwmf-devel jasper-devel libtool-ltdl-devel librsvg2-devel openexr-devel gcc gcc-c++ ghostscript freetype-devel libjpeg-devel libpng-devel giflib-devel libwmf-devel libexif-devel libtiff-devel

Note there are several deletions from the post I referenced above; I had some dependency conflicts and I removed them.

Next, download this source RPM:

wget http://red.penguin.ro/SRPMS/ImageMagick-6.5.7-0.src.rpm

And build it:

rpmbuild --rebuild ImageMagick-6.5.7-0.src.rpm

Hopefully it will work first time; did for me.

Now you should be able to run:

$ rpm -Uvh /usr/src/redhat/RPMS/x86_64/ImageMagick-6.5.7-0.x86_64.rpm
$ rpm -Uvh /usr/src/redhat/RPMS/x86_64/ImageMagick-devel-6.5.7-0.x86_64.rpm

I skipped all the others since I don’t need them but you might want to add the C++, perl, etc libraries too.

Confirm you’ve got the correct version in your path:

$ convert --version
Version: ImageMagick 6.5.7-0 2009-11-12 Q16 OpenMP http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC

And now reinstall rmagick:

sudo gem install rmagick

And we’re done!

Image watermarking with CarrierWave

Friday, November 13th, 2009

I love the library for handling image uploads. But how can we do watermarks?

Easy. Prepare a transparent PNG with your logo, then add the following rule to your Uploader class:

  def watermark(path_to_file)
    manipulate! do |img|
      logo = Magick::Image.read(path_to_file).first
      img = img.composite(logo, Magick::SouthEastGravity, Magick::OverCompositeOp)
    end
  end

Now call it like this:

  version :medium do
    process :resize_to_fill => [500,500]
    process :watermark => ["#{RAILS_ROOT}/public/images/logo.png"]
  end

Bingo, a beautiful watermark.

Coding well is embarrassing

Wednesday, November 11th, 2009

I don’t use SciPy (a science/maths programming library), or indeed Python much at all, but I can closely relate to the experience of the author of this post.

Especially these quotes:

The bottleneck in writing code isn’t in the writing of the code, it’s in understanding and conceptualising what needs to be done. Once you’ve done that, i.e. come up with mathematical objects and equations that describe your algorithm, you simply express these in a few lines [..] and hit go.

[..] you spend months developing your complex algorithms and when you’re done you show somebody the result of all your efforts — a page or two of code. It looks like something that somebody could have written in an afternoon. Even worse, you start to suspect that if you had really known [..] and spent a few days carefully thinking about the problem to start with, then you probably could have coded it in an afternoon.

He’s talking about generating financial algorithms but I feel the same way about many things I’ve done. So many times I’ve written reams of code, fleshing out the way I think something should be, only to later realise I could generalise much of it and in the process shedding hundreds of lines and days/weeks of work. I just don’t know what I want until I see it expressed in front of me, and until I know what the product should be, I can’t generalise. It’s as if the process of coding is analogous to analysing a number series – it’s only when one has enough points of data, say 5 or 10, that one can guess at the equation that generated it. In my case, it’s only when I’ve implemented the same damn thing with minor changes 5 times in a row that I finally see the pattern and am able to collapse it down into an abstracted general form.

And then days, weeks, months, later, when one inspects the results of one’s labours, one’s greeted with a couple thousand lines of code, if that, with probably only a few hundred lines of actual “live” structural code. It does indeed look like something you could have bashed out in an afternoon if you’d really tried. And no doubt I could type it all again in a few hours. The trick is knowing what to type ..

New minimum culture

Friday, November 6th, 2009

It’s come to my attention that certain visitors to this web log are criminally bereft of exposure to the essential media of the ages.

I live to serve and I correct this lacking with this comprehensive study course:

Pump Up the Volume
Heathers

More as I think of them.