Archive for May, 2012

Primary key ranges in Propel ORM

Wednesday, May 23rd, 2012 | Programming, Tech

If you’re using Propel ORM, you may want to select a range of primary keys. According to the documentation, you should be able to do this using code similar to the following.

BookQuery::create()->filterById(array('min' => 1, 'max' => 100));

However, what you find you get is that will return books with the ID of 1 and 100, but nothing else.

That is because Propel does not support ranges on IDs. This has been noted on the Propel GitHub issue tracker and will be resolved at some point in the future, but until then you have two possibilities.

Firstly, if you’re only looking to specify one value in the range, you can pass a criteria constant to filter by that.

BookQuery::create()->filterById(30, \Criteria::GREATER_THAN)->find();

You can also use LESS_THAN in the same way. Or, if you need a range with both ends specified, you can resort to the where() method.

BookQuery::create()->where('id BETWEEN 1 AND 100')->find();

Though that method requires you to use the database column names, rather than the PHP names used in Propel (yours may be the same, but I often rename mine for legacy reasons).

How we built Village Chief

Tuesday, May 22nd, 2012 | Limited, Programming, Tech

We recently launched Village Chief, a Facebook game in which you take on the role of a local village chief with the aim of growing your community into a thriving settlement.

The project was designed from the ground up to be highly scalable – Facebook cames can go viral very quickly so you need a system which can scale up very quickly if your game suddenly becomes very popular, or you risk losing out on huge amounts of market capitalisation.

First off, it’s written in Ruby. Ruby is a cool scripting language that is similar to Python and it’s popular web framework Ruby on Rails has received a lot of praise being the framework that Twitter and 37signal’s suite (Basecamp, Highrise, etc) are written in. It’s a very forgiving language, even compared to PHP, so if you like your static typing consider Scala instead, but for rapid web development, Ruby is a gem.

Despite the popularity of the Rails framework, I was looking for something a little more lightweight, so Village Chief is built using Sinatra. The documentation is a little underdeveloped but is never the less sufficient to get buy and lets you get a web application up and running nice and fast.

Ruby also includes a great module system called Ruby Gems (spot the earlier pun now 😉 ), a system similar to PHP’s PECL. The game makes heavy use of these – for example, using Koala to interact with Facebook and Dalli to handle our caching.

For a data source, it made sense to use a NoSQL solution. I originally settled on Apache Cassandra. It doesn’t have the flexibility or functionally of Mongo but it terms of raw speed it’s hard to beat and scales onto multiple servers with eventual consistency beautifully.

When it came to deployment, I decided to go with Amazon AWS’s DynamoDB instead. I’ll discuss more about why this was later but this meant rewriting the database interaction. This was easy to do thanks to our database abstraction layer and allowed us to continue using Cassandra for our testing systems while rolling out the production environment on DynamoDB.

Some data was also rolled out using SQLite. This was used for data such as the product catalogue which doesn’t contain user data – just things you can buy. Keeping it in an SQLite database allows us get quick access while still being able to query against the records – and importantly, allows us to version control our product catalogue.

To keep the database load down, some of the sorting was moved into the web server layer instead. On top of this we built a caching system using Memcache to save a lot of the data in memory, thus making things much faster.

When it came to deployment, I decided that Amazon’s EC2 cloud. You can start up a new server in under a minute, which is fantastic for scalability. Thanks to Amazon’s Elastic IPs, you can maintain a set of public IPs and map them to any one of your EC2 servers.

The servers are based on the latest Ubuntu release and to make sure that they are delivering content at top speed, I choose to go with the nginx web server rather than Apache, which works in conjunction with Passenger to provide the platform.

One limitation of EC2 is that you either have to allocate EBS (Elastic Block Storage) which adds additional cost, or use the ephemeral storage which you lose every time you turn the box off. The solution? Put everything into an external database so that we can switch boxes on and off without a problem – hence the decision to use DynamoDB rather than running Cassandra on the servers.

Code is then deployed using Capistrano which goes onto each of the boxes and performs a structed deployment that you can easily roll back, pulling the code direct from our Git repository.

That is a whistle-stop tour of the technologies used in Village Chief. If you have any questions about any of them, feel free to post a comment.

Installing JPEG support on Debian

Monday, May 21st, 2012 | Life, Tech

If you have made the mistake I did of doing a fairly minimal install on Debian, you might have noticed it is missing a few fairly basic features – such as JPEG support! Luckily it’s fairly easy to install it.

wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
tar -zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure --enable-shared
make
make install

ODDTV

Monday, May 21st, 2012 | Life

Recently, I attended the launch of ODDTV, the new social video platform for Open Door Design, the design agency I worked at. It was great to see some old faces and although we couldn’t stay for long, drink as much free lemonade as we possibly could.

ODDTV is an interesting mix of videos that ODD has been involved with or generally likes the look of. Check it out.

Humanist Community May meeting

Sunday, May 20th, 2012 | Events, Humanism

This month’s Humanist Community of Leeds was looking very promising with the glorious sunshine that the weekend had experienced. Unfortunately, it didn’t quite last until Sunday evening, but it was an enjoyable evening none the less. With food now served until 10pm we were a little less under pressure to get started and enjoyed a long evening of discussions that was arguable even more political than normal.

Continuous integration

Saturday, May 19th, 2012 | Limited, Tech

I’m currently consulting at a small software house in Leeds city centre and we’re making continuous integration a big thing.

Initially, we started off using CruiseControl which integrates nicely with phpUnderControl. Unfortunately, phpUC is now seriously showing its age – many of the components simply didn’t work anymore and it still uses phpDocumentor, a project that was abandoned years ago to the point where it is now unusable with modern code (though I’m glad to see the project has recently been rescued and a 2.0 version is being developed).

As a result, we’ve now switched to Jenkins and it’s really working out well. it seamlessly integrates with Subversion and using the Ant build script we’re able to integrate PHPUnit, PHP CodeSniffer, PHP Copy & Paste Detector and PHP Mess Detector, all of which provide great feedback on the standard of code which is being written.

You can then varying the log level and what classes as a failed build, so if you want to ensure everything is absolutely perfect, you can have Jenkins email you every time someone doesn’t quite space their code out right, or adds a local variable that they never use. Then again, you can just have that stuff running in the background and check it when you have time.

One issue you might run into when first integrating such tools is that builds can take a long time to run – CodeSniffer and Mess Detector can take up to 10 minutes each even on a small code, when there are lots of issues to resolve. However, once you have resolved most of these issues you will find that they run quickly again and should be able to get your build time down to a couple of minutes on even a large codebase.

The metric system

Friday, May 18th, 2012 | Thoughts

It boggles the mind that people still use imperial measurements.

They just don’t make any sense in a decimal number system (which is the number system we all use – based around tens). I mean what is it? You have an inch, and there are 12 of those in a foot, and there are three of those in a yard and there are 1760 of those in a mile? Nobody can make an argument for that being a better system of measurement!

But if you ask people how tall they are, or how much they weigh, you often get an imperial measurement back. Or a slap if you’ve just asked a fat girl. But mostly an imperial measurement. Once you make the leap, you realise how silly it was, but we’re never going to win hearts and minds by lambasting people; we need a positive approach.

So here are five great reasons to make an effort to use the metric system…

1. Your penis is longer. Why settled for a six inch penis when you can have a 15 centimetre one?

2. Why have a pint of beer, when you can have a litre?

3. Using the metric system annoys the Bible Belt who believe it is a Communist plot.

4. You can drive at over 100 kmph legally, you can’t do that in mph without risking instant disqualification.

5. Even drug dealers have started to use the metric system now. How will you know if you’re getting a good price if you don’t know what 50 grams are?

Floating along the Jersey Shore

Thursday, May 17th, 2012 | Thoughts

I really enjoyed Louis Theroux’s recent two documentaries, Extreme Love, even if they were both heartbreaking.

The first, which looked at autism focused on a specialist school in New Jersey. What struck me first though was that I was somewhat thrown as to what I was watching. Was this a special school for autism, or a special school for fat kids?

It sounds like a joke, but I was genuinely shocked as to how many of the children at the school were significantly overweight. Has obesity in the United States become such an epidemic that it has now become so shocking to the rest of the world?

Probably not. A quick google around suggests that obesity is particularly prevelant in children with autism. They use data which is now eight years old and even back then, over 30% of children with autism were reported to be overweight.

This compares with 23% of children who do not suffer from autism – still a very high number though.

Body dissatisfaction

Wednesday, May 16th, 2012 | Religion & Politics, Science, Thoughts

I’ve started to pile on some pounds (we really need to come up with an updated term that reflects the metric system I know and love) recently, to the point where I’ve gone from the most perfect weight a human being has ever weighed to having only four kilograms of wiggle room before I’m no longer in my target BMI.

It’s very distressing because I lead, on the whole, a very healthily lifestyle and if Rob Lyons is to be believed you could probably even drop the “on the whole” qualification.

Still, after a long day of carrying my fat body around, I do enjoy sitting down and catching up on Stuart Ritchie’s Twitter feed, which provides a refreshing change from the normally interlectually void stream of inane nonsense that normally comes through (Alex, Lil and George while at Fab, though I enjoy that stuff as well).

Recently, he tweeted about a new report which suggests that female body dissatisfaction is primary caused by inter-peer competitiveness, and not the media.

Based on the results of the study, the report concludes that media exposure actually has minimal impact on how unhappy women are with their bodies, in comparison to the significant effect that inter-peer competitiveness has.

So why are we always being told that it’s the media that are ruining our teenage daughters?

This reminds me the video games cause violent crime argument. It was a fact that a lot of people spread, and then we looked at the actual evidence and it turned out that video games do not cause violent crime. Though even after that, people continue to toot that horn.

In both cases, you have to wonder who is spreading this? Presumedly, it isn’t the media trying to give themselves a bad name (of course it could be different sectors of the media attacking each other). Is it just genuinely honest but misinformed people running pressure groups? Do we just assume that it is the case because it seems to fit the puzzle?

Changing your timezone in Debian

Tuesday, May 15th, 2012 | Life, Tech

If you want to change your timezone on Debian, use the following command.

dpkg-reconfigure tzdata