From a739c4f308bbf0651effd95e73142a8849056855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Fri, 16 Sep 2011 02:40:57 +0200 Subject: [PATCH] Ability to mount resque jobs monitoring directly into Diaspora* routes As resque-web is considered as a debug tool, route is disabled for production by default, and can be enabled via the `mount_resque_web` key in application.yml. --- app/models/app_config.rb | 2 +- config/application.yml.example | 3 +++ config/initializers/resque.rb | 24 ++++++++++++++++++++++++ config/routes.rb | 5 +++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/models/app_config.rb b/app/models/app_config.rb index e31e4dbb7..cdef7dc52 100644 --- a/app/models/app_config.rb +++ b/app/models/app_config.rb @@ -117,7 +117,7 @@ HELP def self.[] (key) return self.pod_uri if key == :pod_uri - return ENV[key.to_s] if ENV[key.to_s].present? && ENV["HEROKU"] + return ENV[key.to_s] if ENV[key.to_s] && ENV["HEROKU"] super end diff --git a/config/application.yml.example b/config/application.yml.example index 9f8f213f7..e0bd14e2f 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -165,6 +165,8 @@ defaults: &defaults # Use paypal for recurring donations paypal_hosted_button_id: "" + # Mount resque-web into routes + mount_resque_web: true # # Use this section to override default settings in specific environments @@ -176,6 +178,7 @@ development: production: <<: *defaults + mount_resque_web: false # diff --git a/config/initializers/resque.rb b/config/initializers/resque.rb index eb2ca2856..06c9e0c5e 100644 --- a/config/initializers/resque.rb +++ b/config/initializers/resque.rb @@ -27,3 +27,27 @@ if AppConfig.single_process_mode? end end end + +if AppConfig.mount_resque_web + require 'resque/server' + + 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 + [307, {"Location" => '/'}, self] + end + end + + def each(&block) + end + end + + Resque::Server.use AdminRack +end diff --git a/config/routes.rb b/config/routes.rb index 217b17ef6..25707a1da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -157,6 +157,11 @@ Diaspora::Application.routes.draw do get 'mobile/toggle', :to => 'home#toggle_mobile', :as => 'toggle_mobile' + # Resque web + if AppConfig.mount_resque_web + mount Resque::Server.new, :at => '/resque-jobs' + end + # Startpage root :to => 'home#show'