Did you know you can use Puma in Cloud Foundry?

When Cloud Foundry was first released in April 2011, it supported both Ruby 1.8 and Ruby 1.9 but it only supported the thin server. This was the same server that was originally only supported by Heroku. Mike Perham wrote a great article on the Puma server last Christmas and it turns out it is really easy to use Puma within Cloud Foundry.

Summary of steps:

  • turn on threadsafe mode
  • add puma gem to Gemfile & install gems
  • update script/rails to change default to Puma
  • push to Cloud Foundry
  • check logs to confirm that it is using Puma!

Preparation

First, let’s turn on threadsafe mode so we get the humming threaded goodness (which is far better with runtime ruby19)

# Enable threaded mode
config.threadsafe!

Add the Puma gem to your Gemfile:

gem "puma"

NOTE, also remove gem "thin" if you had that.

Update your Gemfile.lock:

bundle install

Changing the default server

By default, Rails uses the all-ruby web server Webrick that comes as a standard library with each Ruby installation. We can change that default and we do that by editing the script/rails command.

Add the following two lines into your script/rails command:

require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)

The script/rails command will now look like:

#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
require 'rails/commands'

Running under Puma

You can now test that "rails server" defaults to Puma:

$ rails s
=> Booting Puma
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Puma 1.6.3 starting...
* Min threads: 0, max threads: 16
...

Hooray, we booted Puma by default.

Now we can push this up to Cloud Foundry and it too will use Puma by default!

$ vmc update # for vmc v0.3.X
$ vmc push   # for vmc v0.4+

Each Puma process defaults to maximum 16 threads. You can also scale out the number of Puma processes using the vmc instances command.

vmc instances NAME 5  # changes to 5 instances
vmc instances NAME +5  # adds 5 more instances
vmc instances NAME -4  # removes 4 instances

Remember, please stop using ruby 1.8. It is EOL soon and there will be no more security patches.

Today is a great day to delete and recreate all your Cloud Foundry ruby apps with the ruby19 runtime.

Spread the word

twitter icon facebook icon linkedin icon