Archive for the ‘All’ Category

How to save 100m of RAM per mongrel

UPDATE: See Part 2 of this for a better solution
Note: This monkey-patch only works on Rails 2.2
We recently noticed our mongrels, upon startup, were 244M. Eek.

PID USER PR NI VIRT RES SHR S %CPU %MEM [...]

MRI Ruby + MySQL + Threads == Stop the world… JRuby doesn’t

As we have been internally discussing how to scale our databases from 10’s of millions of rows to 100’s of millions, database sharding came up.
Depending on your data model and your application, sharding data into tables by some natural key is great if any given request uses only one shard. FiveRun’s DataFabric seems [...]

Using helpers in a controller: with_helpers

Although there is supposed to be a clear separation between views and controllers, often when it comes to helper functions, there is a small bit of overlap and there are situations where it’d be nice to simply use a few helpers from inside an action.

ActionController::Base.class_eval do
def with_helpers(&block)
template = ActionView::Base.new([],{},self)
[...]

Breaking the Rails static asset timestamp cache in development mode

Rails automatically adds the File.mtime to static assets when using stylesheet_link_tag and javascript_include_tag. The file’s mtime is cached to prevent excessive file system access… even in development mode. This is problematic in a Facebook canvas during development mode because often you won’t immediately see the changes you make to your stylesheets and javascripts.
Here [...]

Cleaning up old releases

Instead of relaying on running cleanup of old releases via capistrano, we have a cron job to only keep releases for last two days (but at least three latest).

#!/usr/bin/env ruby

require ‘fileutils’

KEEP_RELEASES = 3
KEEP_DAYS = 2
EXCLUDE_APPS = %W(uploadr)

cut_time = (Time.now.utc – KEEP_DAYS*24*60*60).strftime("%Y%m%d%H%M%S").to_i

Dir['/u/apps/*'].each do |app|
next if EXCLUDE_APPS.include?(File.basename(app))
dirs = Dir["#{ app }/releases/*"]
fresh [...]

Rake task for syntax checking a Ruby on Rails project

Here’s a quick rake file that will crawl through your Rails project and syntax check ruby, erb, and yml files. You should always run this before doing a “cap deploy” and it even doesn’t hurt to run it before a “svn commit”.

require ‘erb’
require ‘open3′
require ‘yaml’

task :check_syntax => [:check_ruby, :check_erb, :check_yaml]

task :check_erb do
(Dir["**/*.erb"] [...]