Skip to content
Permalink
Browse files

Migrate Sidekiq::Web to a pure Rack application

Migrate Sidekiq::Web a pure Rack application to avoid sinatra as
dependency. rack-protection is still needed.

The application is mounted on top of Rack::Builder, mantaining all of
the previous http interface.

Rack apps being used:

- Rack::File to serve assets
- Rack::Session::Cookie, the secret can be configured via
  Sidekiq::Web.session_secret
- Rack::Protection, same as before when using sinatra
- Sidekiq::WebApplication, described below.

Sidekiq::WebApplication is a very simple rack application composed of a
Sidekiq::WebRouter and a Sidekiq::WebAction dispatcher. This terminology
was adopted to be able to mantain Sidekiq::Web as a Rack app.

The Router is heavily inspired on Rack::Router[0] (and in many parts
identical), however not being retrocompatible.

The Action is a wrapper to provide convenience, DRY code and maintain
the old interface.

I tried to mantain most of the old application structures so that
customizations and monkey-patches are easily adjustable or even
further work be done to enforce retrocompatibility.

Testing welcome!

0: https://github.com/pjb3/rack-router
  • Loading branch information...
badosu committed Jul 26, 2016
1 parent c89ff2e commit 9ea167db16bf7f4cc9ee94967d0ea0b4dee6a7c5
@@ -1,26 +1,30 @@
# frozen_string_literal: true
require 'erb'
require 'yaml'
require 'sinatra/base'

require 'sidekiq'
require 'sidekiq/api'
require 'sidekiq/paginator'
require 'sidekiq/web_helpers'

module Sidekiq
class Web < Sinatra::Base
include Sidekiq::Paginator
require 'sidekiq/web/router'
require 'sidekiq/web/application'

require 'rack/protection'

enable :sessions
use ::Rack::Protection, :use => :authenticity_token unless ENV['RACK_ENV'] == 'test'
require 'rack/builder'
require 'rack/static'
require 'rack/session/cookie'

set :root, File.expand_path(File.dirname(__FILE__) + "/../../web")
set :public_folder, proc { "#{root}/assets" }
set :views, proc { "#{root}/views" }
set :locales, ["#{root}/locales"]
module Sidekiq
class Web
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
PATH_INFO = 'PATH_INFO'.freeze

helpers WebHelpers
ROOT = File.expand_path(File.dirname(__FILE__) + "/../../web")
VIEWS = "#{ROOT}/views"
LOCALES = ["#{ROOT}/locales"]
LAYOUT = "#{VIEWS}/layout.erb"
ASSETS = "#{ROOT}/assets"

DEFAULT_TABS = {
"Dashboard" => '',
@@ -41,226 +45,56 @@ def custom_tabs
end
alias_method :tabs, :custom_tabs

attr_accessor :app_url
end

get "/busy" do
erb :busy
end

post "/busy" do
if params['identity']
p = Sidekiq::Process.new('identity' => params['identity'])
p.quiet! if params[:quiet]
p.stop! if params[:stop]
else
processes.each do |pro|
pro.quiet! if params[:quiet]
pro.stop! if params[:stop]
end
def locales
@locales ||= LOCALES
end
redirect "#{root_path}busy"
end

get "/queues" do
@queues = Sidekiq::Queue.all
erb :queues
end

get "/queues/:name" do
halt 404 unless params[:name]
@count = (params[:count] || 25).to_i
@name = params[:name]
@queue = Sidekiq::Queue.new(@name)
(@current_page, @total_size, @messages) = page("queue:#{@name}", params[:page], @count)
@messages = @messages.map { |msg| Sidekiq::Job.new(msg, @name) }
erb :queue
end

post "/queues/:name" do
Sidekiq::Queue.new(params[:name]).clear
redirect "#{root_path}queues"
end

post "/queues/:name/delete" do
Sidekiq::Job.new(params[:key_val], params[:name]).delete
redirect_with_query("#{root_path}queues/#{params[:name]}")
end

get '/morgue' do
@count = (params[:count] || 25).to_i
(@current_page, @total_size, @dead) = page("dead", params[:page], @count, reverse: true)
@dead = @dead.map { |msg, score| Sidekiq::SortedEntry.new(nil, score, msg) }
erb :morgue
end

get "/morgue/:key" do
halt 404 unless params['key']
@dead = Sidekiq::DeadSet.new.fetch(*parse_params(params['key'])).first
redirect "#{root_path}morgue" if @dead.nil?
erb :dead
end

post '/morgue' do
redirect request.path unless params['key']

params['key'].each do |key|
job = Sidekiq::DeadSet.new.fetch(*parse_params(key)).first
retry_or_delete_or_kill job, params if job
def session_secret=(secret)
@secret = secret
end
redirect_with_query("#{root_path}morgue")
end

post "/morgue/all/delete" do
Sidekiq::DeadSet.new.clear
redirect "#{root_path}morgue"
end

post "/morgue/all/retry" do
Sidekiq::DeadSet.new.retry_all
redirect "#{root_path}morgue"
end

post "/morgue/:key" do
halt 404 unless params['key']
job = Sidekiq::DeadSet.new.fetch(*parse_params(params['key'])).first
retry_or_delete_or_kill job, params if job
redirect_with_query("#{root_path}morgue")
attr_accessor :app_url, :session_secret
attr_writer :locales
end

def initialize
secret = Web.session_secret

get '/retries' do
@count = (params[:count] || 25).to_i
(@current_page, @total_size, @retries) = page("retry", params[:page], @count)
@retries = @retries.map { |msg, score| Sidekiq::SortedEntry.new(nil, score, msg) }
erb :retries
end

get "/retries/:key" do
@retry = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
redirect "#{root_path}retries" if @retry.nil?
erb :retry
end

post '/retries' do
redirect request.path unless params['key']

params['key'].each do |key|
job = Sidekiq::RetrySet.new.fetch(*parse_params(key)).first
retry_or_delete_or_kill job, params if job
if secret.nil?
# explicitly generating a session secret eagerly to play nice with preforking
begin
require 'securerandom'
secret = SecureRandom.hex(64)
rescue LoadError, NotImplementedError
# SecureRandom raises a NotImplementedError if no random device is available
secret = "%064x" % Kernel.rand(2**256-1)
end
end
redirect_with_query("#{root_path}retries")
end

post "/retries/all/delete" do
Sidekiq::RetrySet.new.clear
redirect "#{root_path}retries"
end

post "/retries/all/retry" do
Sidekiq::RetrySet.new.retry_all
redirect "#{root_path}retries"
end

post "/retries/:key" do
job = Sidekiq::RetrySet.new.fetch(*parse_params(params['key'])).first
retry_or_delete_or_kill job, params if job
redirect_with_query("#{root_path}retries")
end

get '/scheduled' do
@count = (params[:count] || 25).to_i
(@current_page, @total_size, @scheduled) = page("schedule", params[:page], @count)
@scheduled = @scheduled.map { |msg, score| Sidekiq::SortedEntry.new(nil, score, msg) }
erb :scheduled
end

get "/scheduled/:key" do
@job = Sidekiq::ScheduledSet.new.fetch(*parse_params(params['key'])).first
redirect "#{root_path}scheduled" if @job.nil?
erb :scheduled_job_info
end
@app = Rack::Builder.new do
%w(stylesheets javascripts images).each do |asset_dir|
map "/#{asset_dir}" do
run Rack::File.new("#{ASSETS}/#{asset_dir}")
end
end

post '/scheduled' do
redirect request.path unless params['key']
use Rack::Session::Cookie, secret: secret
use ::Rack::Protection, use: :authenticity_token unless ENV['RACK_ENV'] == 'test'

params['key'].each do |key|
job = Sidekiq::ScheduledSet.new.fetch(*parse_params(key)).first
delete_or_add_queue job, params if job
run WebApplication.new
end
redirect_with_query("#{root_path}scheduled")
end

post "/scheduled/:key" do
halt 404 unless params['key']
job = Sidekiq::ScheduledSet.new.fetch(*parse_params(params['key'])).first
delete_or_add_queue job, params if job
redirect_with_query("#{root_path}scheduled")
def call(env)
@app.call(env)
end

get '/' do
@redis_info = redis_info.select{ |k, v| REDIS_KEYS.include? k }
stats_history = Sidekiq::Stats::History.new((params[:days] || 30).to_i)
@processed_history = stats_history.processed
@failed_history = stats_history.failed
erb :dashboard
def self.call(env)
@app ||= new
@app.call(env)
end

REDIS_KEYS = %w(redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human)

get '/dashboard/stats' do
redirect "#{root_path}stats"
end

get '/stats' do
sidekiq_stats = Sidekiq::Stats.new
redis_stats = redis_info.select { |k, v| REDIS_KEYS.include? k }

content_type :json
Sidekiq.dump_json(
sidekiq: {
processed: sidekiq_stats.processed,
failed: sidekiq_stats.failed,
busy: sidekiq_stats.workers_size,
processes: sidekiq_stats.processes_size,
enqueued: sidekiq_stats.enqueued,
scheduled: sidekiq_stats.scheduled_size,
retries: sidekiq_stats.retry_size,
dead: sidekiq_stats.dead_size,
default_latency: sidekiq_stats.default_queue_latency
},
redis: redis_stats
)
end

get '/stats/queues' do
queue_stats = Sidekiq::Stats::Queues.new

content_type :json
Sidekiq.dump_json(
queue_stats.lengths
)
end

private

def retry_or_delete_or_kill job, params
if params['retry']
job.retry
elsif params['delete']
job.delete
elsif params['kill']
job.kill
end
end

def delete_or_add_queue job, params
if params['delete']
job.delete
elsif params['add_to_queue']
job.add_to_queue
end
end
ERB.new(File.read LAYOUT).def_method(WebAction, '_render')
end
end

1 comment on commit 9ea167d

@natebird

This comment has been minimized.

Copy link

commented on 9ea167d Jul 28, 2016

Wow. I expected much more changes than this for a port to Rack. This is very nice. It fits right in with Sidekiq's reduce dependencies motto.

Please sign in to comment.
You can’t perform that action at this time.