Trying a month of 8hrs sleep a night

For more than the last 12 months, I've got into a habit of rarely getting more than 6 hours sleep a night, except on the occasional weekend.

I've done this to try to maximise the amount of waking hours available to me to as get things done as I want, but when I look at the last year, I just don't see the results of this approach at all, and I only end up being frustrated with how little I'm getting done compared to what I want to do.

I'm gonna try a couple of weeks of being adamant about sleeping 8 hrs a night, even if this means cutting social engagements short, to see if I end up being more productive during the time I have awake.

I might write about the differences in my days if I notice any.

  • Share/Bookmark
Posted in Journal | Leave a comment

Virtual servers and false alarms

When you start looking after virtual machines instead of buying shared hosting, you very quickly realise how important RAM is to making a server run properly.

I’m using a machine with a fairly heft chunk of free memory, but recently when shelling into the box to check memory usage I’ve have some pretty scary stats that look like I’ve hardly got any memory left at all:

stemcaa1- ~ — ssh — bash — top

No cause for concern

I was understandably a bit confused when I first saw this, and contacted support at Memset, and I’m pretty satisfied with their answer.

This is actually the way Linux handles memory management – it’s rather complicated. As long as your swap isn’t very high, you’re not actually using that much memory. This link sum’s it quite well:
http://www.linuxvox.com/linux-articles/memory-and-swap/2-how-much-free-memory-does-my-linux-server-really-have

By default the Linux Kernel is designed to use as much free ram as possible when it can, and then release as and when it’s needed by other apps.

Panic over.

  • Share/Bookmark
Posted in Journal | Leave a comment

Setting environments with Rails: how Rake and Capistrano differ

I keep forgetting this when I’m developing with Rails, and while it’s not massively hard to find, it IS annoying to keep looking up, so scribbling it here should commit it slightly more fully to memory

If you want to make sure stuff happens in a certain environment with Rake, you need to make sure you set the environment before you call the command. You’ll do this when setting up a database for testing.

# Drop the test database
RAILS_ENV=test rake db:drop
# Clear database and reset schema to restart
RAILS_ENV=test rake db:reset
# Recreate the staging database, but only migrate it to version 3
RAILS_ENV=staging rake db:reset VERSION=3

However, If you’re using Capistrano, and multistage environments (and you really should, it makes live, way, way better) you’ll need to set it after calling the command initial, but before you pass the argument like so:

# deploy an app the beta environment
cap beta deploy
# setup the staging environment before deployment
cap staging deploy:setup

Rake comes first, and cap comes second.

  • Share/Bookmark
Posted in Coding | 1 Comment

Setting up wordpress to update itself

Wordpress’s auto update feature is incredibly handy.

However, not every webhost automatically has accounts setup to allow for this, and if click the promising looking auto-upgrade icon, you’ll often be face with this screen here:

Wordpress - auto-upload fail

You get this screen when the folder containing wordpress belongs to the user, instead of the apache daemon; this is normally either www-data if you’re using Ubuntu, or nobody if you’re using Redhat Linux instead.

The simplest way to do this is to change the folders to be owned by the apache daemon instead, by using chmod like so:


chown -R nobody wordpress

Adding the -R flag makes the changing of ownership recursive through all the folders inside wordpress.

Job done.

This is outlined in more detail by Rob Spencer on his own blog – really quite handy.

  • Share/Bookmark
Posted in Coding | Tagged , | 2 Comments

Fixing old gits

I've been working a project that used a very old git repo, and when I tried to run commit, I was greeted with this particular gem, over and over:


Git: You have some suspicious patch lines…
(endless whining about whitespace and tabs and other petty crap

This gets pretty tiresome, but fortunately, it's quite easy to fix. You can either run git commit with the --noverify flag, or for a more permanent fix, take by taking out the executable bit on the pre-commit hook like so:


cd .git/hooks/ # go to the directory where the git hooks are
chmod -x pre-commit #metaphorically speaking, take a crow bar its kneecaps

Mercifully, this little feat of silicon skullduggery is already done for you in more recent git versions, so you needn't dirty your hands.

Thanks Rob Wilkerson's, and Dan Klassen for this particular gem.

  • Share/Bookmark
Posted in Coding | Leave a comment

Money is fertilizer, companies are soil

I spent a fair chunk of yesterday working on a redesign of the new ORG website, with Felix, Chris, Mike, Harry, and after we had finished for the talking with Felix about the recession and worthwhile work. We both have friends who work in the financial sector, we both get nerdily angry about bad design, and we have watched with dismay how ludicrous amounts of public funds have gone to propping up inherently rotten organisations that don’t deliver tangible value, but justify themselves by being ‘too big too fail’.

When we have such epic challenges ahead of us like dealing with climate change, the idea of allocating such epic-scale resources to keep people employed doing what they’ve been doing before, just because that’s how things have always been makes no sense.

It’s easy to become totally cynical in the face of this, and just fall into line with everyone else who chooses to turn a blind eye to the undeniable human costs associated with a lifestyle of cheap flights, new iPods every 6 months, and cheaply made stuff we don’t need from Primark, and make for the most of the next ten years before the shit really does hit the fan.

But thankfully pieces like Umair Haque’s generation-M manifesto, or Woody Tasch’s ideas of slow money, make it feel like there’s a point in not falling into line just yet.

  • Share/Bookmark
Posted in Journal | Tagged | 1 Comment

Looking for a green host – concluding the search

I think I can finally stop searching for a decent green host who does virtual servers,now that I've found Memset.

I was after a host based in the EU, who specialised in virtual servers, had experience with open source software, were environmentally sound, and affordable - I've gone the why's of this in more detail in previous posts.

This post will outline how why I ended up choosing Memset, and list a few other honourable mentions from other companies who might also fit the bill if you're looking for similar things to me in hosting.

They're based in the UK

Having servers in the UK generally takes an element of uncertainty out of hosting - and while I'm hardly going to drive all the way down to Reading to look at a couple of boxes, seeing an accolade from a UK publication about their service carries much more weight with me than one from a US mag I've never heard of.

They do virtual machines and they've been doing it for long time:

Memset have been doing VM's since before it became cool, and their choice of Xen, an open source platform for the virtualisation, ticks the opensource geek tickbox for me.

They're affordable 

Of the companies, in the UK, Memset seemed to offer the best combination of price and service I could find. In fact their new pricing even looks aggresive when you compare them to US based operators like Linode and Slicehost. £9.99 for a VPS with 512mb of ram (the main resource I'm interested in) is extremely respectable, and when I did need to upgrade ram on a VPS recently they did it in about 15 minutes after me asking them.

They're about as green as I could find in the UK

They don't use renewable energy to power their data centres, but apparently this is down to there not actually being enough in the UK to meet demand at all - I put out a few emails asking around, and was contacted directly by the Kate Craig-Wood, the Memset MD, who provided me with detailed answers to the questions I had. She explained her reasons for choosing conventional energy, telling me that hosting providers in the UK that claim they use renewable will only end up sourcing a fraction of their power from these sources.

This seems in keeping with what I could find myself - the only place in the UK that I could find that runs on renewable power offers virtual private servers is Smartbunker, who've stopped offering VPS's to concentrate on bigger clients instead.

So if you can't run on renewable energy, you can at least go for efficiency, and that's what they've been done. At the moment, a widely accepted figure for measuring datacentre efficiency is PUE - Power Usage Effectiveness, and is measured by comparing the power being going into a datacentre to how much is used by the computers themselves. The closer to one this figure gets the better.

If you have a PUE of 2 for example for every watt used by the computers, another watt is spent on keeping the data centre cool and power going to the servers themselves. Google's own pages cite a PUE of 1.9 as the direction that most datacentres are heading towards for 2011, although they're already way ahead of this with figures of 1.1-1.4 as their average. 

Memset tell me they hit a PUE of around 1.3, and they offset any emissions left over through the carbon neutral company, one of largest, most well known (and also one of the most expensive) companies selling offsets.

Other options 

Two other UK based hosts who look interesting if you're after UK virtual private servers are Bytemark and Brightbox. Both are more expensive; with Brightbox you're partly paying for their staff's expertise with Rails, and the fact that they integrate tightly with FiveRuns, a monitoring tool to help you see where the performance bottlenecks are in your apps. They also offset the their companies carbon with ClimateCare, another UK company.

Bytemark is the host I used before Memset. They've been extremely attentive to any requests I've had, and were able to answer all the questions I had about their datacentres, other than bizarrely their PUE. They're also well known for being friendly to open source development. I'd happily recommend them as an alternative to Memset if you're not crazy about storing all your eggs in one basket.

  • Share/Bookmark
Posted in Journal | 12 Comments

On the new Star Trek movie

I chuffing loved it.

Clever, funny, well paced, and brilliantly shot. If you take the film as a typical summer blockbuster it delivers; it's immensely entertaining, the cast unformily photogenic, and the special effects are thrilling.

If you have any knowledge of the previous series though, there's a whole extra layer of in-jokes and references to the TV series that it's based on; in particular, the scene justifying breaking from canonical TV series is a masterstroke of tongue in cheek dialogue, but amazingly doesn't feel too laboured like it so easily could have.

It's really, really good fun. Go see it.

  • Share/Bookmark
Posted in Journal | 2 Comments

Frustrating and Cryptic Ruby Idioms (#1 of a series)

I keep coming across these FACRI's (Frustrating and Cryptic Ruby Idioms) in my work, so I'm jotting them here in the hope that I'll remember them better in future.

Ruby idioms

Ruby is a wonderful, if somewhat slow and memory hungry language, with an incredibly flexible and expressive syntax. However this flexibility leads to the creation of idioms that initially look totally opaque, if you don't know what to look for.

Case in point: the object{:&method} idiom

If you want to take an array called names , want to create a new array by running a text manipulation on every member of that array, a terse, but readable way to do this would be:

result = names.map { |name| name.upcase }

The intent is pretty clear here, and what happens programatically is also very readable. Another to do this though is write it like:

result = names.map {&:upcase}

Something called type coercion is occurring here; you normally pass the map method a Proc object to execute, with a placeholder name for each iteration, and the code to run and return. However because you're not passing a Proc object here, Ruby tries to convert it on the fly into a Proc object using a method called to_proc:

def to_proc
  proc { |obj, *args| obj.send(self, *args) }
end

So in this case, it's passing in names, and the method in the *args is upcase. I wasn't familiar with the send method here either, so the documentation for it from ruby core may help here:

class Klass
     def hello(*args)
       "Hello " + args.join(' ')
     end
   end
k = Klass.new
k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"
An expensive idiom, by rockstars, for rockstars.

Th end result of all these examples is a saving of about 12 characters, at the expense of readability, and a huge performance hit as each member in names of passed around and type coerced like there's no tomorrow.

If you're a coding savant, the elegance of this will probably make you weep tears of syntactic joy, and the clever brevity of this isn't lost on me.

However, coming to this, without too much knowledge of the Ruby extensions project, or someone to talk you through what's happening is likely to be a frustrating experience.

Hope fully this will save time for someone else in future.

  • Share/Bookmark
Posted in Journal | Tagged , , , | 1 Comment

How to set up a debugger with mod_rails/Passenger

Anyone who's worked on the web will know easy it is to end up constantly refreshing pages to see if the content delivered from say, a database driven site is the indeed content you want to show.

This is one approach, and while simple to understand, there are often other approaches available to this.

One example is using the ruby debugger, and break points in your code to inspect and control what is happening at each step, to see what variables are available.

This, combined with the more traditional tools for debugging a view using @object.inspect or debug(@object) methods make fixing bugs less of a pain.

As many of us transition from using Mongrel as our server to Passenger, we find ourselves missing this useful tool. This post outlines one way to setup a debugger for Passenger.

Putting a debugger back in Passenger

Before people started using Passenger for handling rails requests, using the ruby debugger was simply a case of adding debugger statements in the code, and then starting the Mongrel or Webrick with script/server --debugger.

When the code hit a debugger statement, the console would drop into an interactive debugger, and all would be well with the world.

Passenger currently doesn't allow an easy way to use the debugger straight out of the box, so we'll have to make a few changes to the code in our own environments, and use a gem called ruby-debug if we want this behaviour again.

Add ruby-debug

We're actually using a gem called ruby debug - this gives us a powerful remote debugger, to inspect code - it does load of other interesting things, but in this case we're only interested in a subset of the features here.

sudo gem install ruby-debug

Add code to use ruby debug when you hit a debugger statement

Just like how we restart Passenger using the touch tmp/restart.txt file, the nicest way I've found to switch on the debugger comes from the ominously titled duck_punching weblog. It involves adding a rake task to create a debug.txt text file, and adding a snippet of code to your development.rb config file:

if File.exists?(File.join(RAILS_ROOT,'tmp', 'debug.txt'))
  require 'ruby-debug'
  Debugger.wait_connection = true
  Debugger.start_remote
  File.delete(File.join(RAILS_ROOT,'tmp', 'debug.txt'))
end

This snippet checks for the existence of a debug.txt in the tmp directory of the main Rails directory, and if it's there, it opens a connection to connect to for debugging.

And here's there restart task for rake:

task :restart do
  system("touch tmp/restart.txt")
  system("touch tmp/debug.txt") if ENV["DEBUG"] == 'true'
end

To run in debug mode, you just pass the constant DEBUG in while calling the rake task like so:

rake restart DEBUG=true

Sprinkle in your debugger statements

We can now add the debugger statements where we want the breakpoints in our code for us to inspect.

Open a rdebug session

Now we can finally open our ruby debug session, using the -c switch to start our client connection.

rdebug -c

Because we're connecting to the same machine as we're running rails on, it defaults to localhost, but we can connect to other remote machines too.

Gotcha - Remember to reboot!

Once you've finished your command line debugging session, and exited the debugger, you won't be able to start a new sessions without calling the rake:restart command with the DEBUG=true values.

Now that you have this set up, take a look at this Railscast by Ryan Bates or this one by Brian Donovan to get an idea of just how useful the ruby debugger is.

Patrick Lenz has another really helpful article that goes in depth with the commands available to you during a debug session - I found it incredibly useful, and I can't recommend it highly enough.

  • Share/Bookmark
Posted in Journal | Leave a comment