Brock's Blog

RhoConnect and Redis to Go on Heroku

| Comments

Let’s say you want to deploy RhoConnect on Heroku but don’t want to use RhoMobile’s official RhoConnect addon (docs here and here). This would mean that you’d have to use the Redis To Go addon or get your own redis instance up and running on AWS.

The easiest way to go about this is to use Redis To Go (RTG), but there is one tweak you have to make to get it talking with RhoConnect.

Add the following lines to the Application.initializer method in application.rb, before the super call:

1
2
3
4
if ENV['REDISTOGO_URL']
  uri = URI.parse(ENV["REDISTOGO_URL"])
  Store.db = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end

So, if you applied this tweak to the default application.rb, it would look like this:

application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Application < Rhoconnect::Base
  class << self
    def authenticate(username,password,session)
      true # do some interesting authentication here...
    end

    # Add hooks for application startup here
    # Don't forget to call super at the end!
    def initializer(path)
      if ENV['REDISTOGO_URL']
        uri = URI.parse(ENV["REDISTOGO_URL"])
        Store.db = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
      end
      super
    end

    # Calling super here returns rack tempfile path:
    # i.e. /var/folders/J4/J4wGJ-r6H7S313GEZ-Xx5E+++TI
    # Note: This tempfile is removed when server stops or crashes...
    # See http://rack.rubyforge.org/doc/Multipart.html for more info
    # 
    # Override this by creating a copy of the file somewhere
    # and returning the path to that file (then don't call super!):
    # i.e. /mnt/myimages/soccer.png
    def store_blob(object,field_name,blob)
      super #=> returns blob[:tempfile]
    end
  end
end

Application.initializer(ROOT_PATH)

# Support passenger smart spawning/fork mode:
if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      # We're in smart spawning mode.
      Store.db.client.reconnect
    else
      # We're in conservative spawning mode. We don't need to do anything.
    end
  end
end

To explain this a bit, if you install the RTG addon, it adds an REDISTOGO_URL environment variable to your heroku instance, with a value like this:

redis://redistogo:f8c3a80cb76610cb38cb0fe56110b08f@pike.redistogo.com:9436

RhoConnect won’t read this environment variable out of the box, so we’ve got to add the aforementioned bit of code to glue them together.

Tip: If you’re curious, you can run heroku config in your app to see all of the environment variables available to your heroku app (including REDISTOGO_URL).

Hope that helps!

Deploying RhoConnect to Multiple Environments - Part 1

| Comments

It’s relatively straightforward to configure RhoConnect for a single environment (i.e. production), but things get more complicated if you want to have a robust way to deploy to several environments (i.e. development, testing, production).

In this post I’ll show you how to do the following:

  1. Set up global and environment-specific configurations
  2. Configure RhoConnect to use a given environment’s settings