-
Notifications
You must be signed in to change notification settings - Fork 0
Monitoring
Sidekiq comes with a Sinatra application that can display the current state of a Sidekiq installation.
Add slim
and sinatra
(and sprockets
if you are on Rails 3.0) to your Gemfile
gem 'slim'
# if you require 'sinatra' you get the DSL extended to Object
gem 'sinatra', '>= 1.3.0', :require => nil
Add the following to your config/routes.rb
:
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
In a production application you'll likely want to protect access to this information. You can use the constraints feature of routing to accomplish this:
Checks a User
model instance that responds to admin?
constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? }
constraints constraint do
mount Sidekiq::Web => '/sidekiq'
end
Allow any authenticated User
constraint = lambda { |request| request.env['warden'].authenticate!({ scope: :user }) }
constraints constraint do
mount Sidekiq::Web => '/sidekiq'
end
Short version
authenticate :user do
mount Sidekiq::Web => '/sidekiq'
end
# lib/admin_constraint.rb
class AdminConstraint
def matches?(request)
return false unless request.cookies['user_credentials'].present?
user = User.find_by_persistence_token(request.cookies['user_credentials'].split(':')[0])
user && user.admin?
end
end
# config/routes.rb
require "admin_constraint"
mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new
Checks a User
model instance that responds to admin?
# lib/admin_constraint.rb
class AdminConstraint
def matches?(request)
return false unless request.session[:user_id]
user = User.find request.session[:user_id]
user && user.admin?
end
end
# config/routes.rb
require "admin_constraint"
mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new
class AuthConstraint
def self.admin?(request)
return false unless (cookie = request.cookies['auth'])
Rails.cache.fetch(cookie['user'], :expires_in => 1.minute) do
auth_data = JSON.parse(Base64.decode64(cookie['data']))
response = HTTParty.post(Auth.validate_url, :query => auth_data)
response.code == 200 && JSON.parse(response.body)['roles'].to_a.include?('Admin')
end
end
end
# config/routes.rb
constraints lambda {|request| AuthConstraint.admin?(request) } do
mount Sidekiq::Web => '/admin/sidekiq'
end
Here's an example config.ru
for booting Sidekiq::Web in your choice of Rack server:
require 'sidekiq'
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }
end
require 'sidekiq/web'
run Sidekiq::Web
If you do everything right, you should see this in your browser:
require 'sidekiq'
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }
end
require 'sidekiq/web'
Sidekiq::Web.use Rack::Auth::Basic do |username, password|
username == 'some_user' && password == 'some_pass'
end
run Sidekiq::Web
Below is a collection of nagios checks that includes check_sidekiq_queue script, which validates that a given queue depth is within a particular range. It's a simple shell script that uses redis-cli command line tool, and does not have any dependency on ruby.