public
Last active — forked from skippy/resque_web.rb

Mountable resque-web for rails 3+ apps

  • Download Gist
app/meta/resque_web.rb
Ruby
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
# https://gist.github.com/1214052
require 'sinatra/base'
 
class ResqueWeb < Sinatra::Base
require 'resque/server'
use Rack::ShowExceptions
 
if CFG[:user].present? and CFG[:password].present?
Resque::Server.use Rack::Auth::Basic do |user, password|
user == CFG[:user] && password == CFG[:password]
end
end
 
def call(env)
@server ||= Resque::Server.new
status, headers, body = @server.call(env)
 
# in production/staging with nginx, assets always hang endless <-> this fixes it
if body.is_a? Sinatra::Helpers::StaticFile
buffer = []
body.each{|x| buffer << x }
body = buffer
end
 
[status, headers, body]
end
end
config/routes.rb
Ruby
1 2 3
# ...
match "/resque" => ResqueWeb, :anchor => false
# ...

Here's what I do using devise for authentication:

# routes.rb
mount Resque::Server => "/resque", :as => 'resque'

# config/initializers/resque.rb
Resque::Server.use(AdminRack)

# app/rack/admin_rack.rb
class AdminRack  
  def initialize(app)
    @app = app
  end

  def call(env)
    user = env['warden'].authenticate(:scope => :user)
    if user && user.admin?
      @app.call(env) 
    else
      throw(:warden, :scope => :user, :message => "Unauthorized")
    end
  end
end

Simplest way to do it:

# routes.rb
mount Resque::Server.new, :at => "/resque"

If you're running authentication thru Devise (:admin scope):

# routes.rb
authenticate :admin do
  mount Resque::Server.new, :at => "/resque"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.