Resque Admin in Rails 3 Routes With CanCan

Resque is a background jobs queue that’s highly recommended over Delayed::Job if you are processing a lot of jobs. It uses Redis as the backend which doesn’t suffer from db related bottlenecks under high load.

Resque comes with a built-in admin interface that’s Rack compatible. In Rails 3, you can mount the Resque server admin directly in your routes.rb file.

1
mount Resque::Server, at: '/resque'

But you’ll definitely want to add password protection. Ryan Bates in his Resque RailsCast covers the basics of using Devise and HTTP auth. However, you’ll probably want to hook into your existing ACL system. In my case, I’m using CanCan.

CanCan is not available in the routes.rb by default, but it’s pretty easy to manually load the user and check permissions.

1
2
3
4
5
6
# routes.rb
namespace :admin do
  constraints CanAccessResque do
    mount Resque::Server, at: 'resque'
  end
end
1
2
3
4
5
6
7
8
# config/initializers/admin.rb
class CanAccessResque
  def self.matches?(request)
    current_user = request.env['warden'].user
    return false if current_user.blank?
    Ability.new(current_user).can? :manage, Resque
  end
end
1
2
3
4
5
6
7
8
9
10
# ability.rb
class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new
    if user.is_admin?
      can :manage, Resque
    end
  end
end

You’ll need User.is_admin? method or change the logic in Ability to suit your project.

Now an authenticated user with is_admin? == true will be able to access Resque admin. Other users will get a 404 since no route matches.

Thanks to Arcath’s blog post for initially pointing me in the right direction.