Running Resque Web

Today, I finally dove into Rack. Whenever I hear about a new technology, I know what it is, but don’t have anything specific in mind, so I never end up playing with it. Until I have to–which is both good and bad. Good in that I never end up wasting time with dead-end technologies. Bad in that I could have solved other problems with this tool had I known about it more in depth.

I wanted to run resque‘s resque-web along-side of the Noteleaf app. It’s basically a simple sinatra app that tells you the current state of the background workers. After reading about it for a while, I found different solutions. , and .

The one on stackoverflow suggested that you write write your own config.ru. Btw, you put config.ru in your Rails root directory. No one seems to mention that. After a lot of reading I came up with the rather vanilla:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require "config/environment"

# HTTP Auth For Resque Console
AUTH_PASSWORD = "password" || ENV['AUTH']
if AUTH_PASSWORD
  Resque::Server.use Rack::Auth::Basic do |username, password|
    password == AUTH_PASSWORD
  end
end

use Rails::Rack::Static
run Rack::URLMap.new(
  "/" => ActionController::Dispatcher.new,
  "/resque" => Resque::Server.new
)

However, when deploying it to the server, rackup didn’t seem to be able to find my rails gem. So looking around some more, I did find another hint on engineyard, which lead me to find this:

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
require 'sinatra/base'

class ResqueWeb < Sinatra::Base
  require 'resque/server'
  use Rack::ShowExceptions
  
  # Set the AUTH env variable to your basic auth password to protect Resque.
  AUTH_PASSWORD = ENV['AUTH']
  if AUTH_PASSWORD
    Resque::Server.use Rack::Auth::Basic do |username, password|
      password == AUTH_PASSWORD
    end
  end

  def call(env)
    if env["PATH_INFO"] =~ /^/resque/
      env["PATH_INFO"].sub!(/^/resque/, '')
      env['SCRIPT_NAME'] = '/resque'
      app = Resque::Server.new
      app.call(env)
    else
      super
    end
  end

end

You edit your config/environments.rb file like so, and resque_web.rb can either be required in your environments file or put in the initializers, where it’s loaded before environments is.

In hindsight, that works well, because it’s a small demo of how to insert your own middleware into rails. And it’s not that hard either. Just have to look up the rack calls, and environment variables, so you’ll know what’s available. 

Finally having something concrete for using Rack is great. I had been thinking about a host of other stuff that’s considered the underbelly of a web app that can be better implemented as Rack middleware apps, rather than contaminating your web app itself. Neato.
Advertisements

2 thoughts on “Running Resque Web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s