mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
Minor cleanups and improve extension compatibility
This commit is contained in:
parent
20e1035bea
commit
91fd77d44e
5 changed files with 107 additions and 105 deletions
|
@ -1,30 +1,28 @@
|
|||
# frozen_string_literal: true
|
||||
require 'erb'
|
||||
require 'yaml'
|
||||
|
||||
require 'sidekiq'
|
||||
require 'sidekiq/api'
|
||||
require 'sidekiq/paginator'
|
||||
require 'sidekiq/web/helpers'
|
||||
|
||||
require 'sidekiq/web/router'
|
||||
require 'sidekiq/web/action'
|
||||
require 'sidekiq/web/application'
|
||||
|
||||
require 'rack/protection'
|
||||
|
||||
require 'rack/builder'
|
||||
require 'rack/static'
|
||||
require 'rack/file'
|
||||
require 'rack/session/cookie'
|
||||
|
||||
module Sidekiq
|
||||
class Web
|
||||
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
|
||||
PATH_INFO = 'PATH_INFO'.freeze
|
||||
|
||||
ROOT = File.expand_path(File.dirname(__FILE__) + "/../../web")
|
||||
VIEWS = "#{ROOT}/views"
|
||||
LOCALES = ["#{ROOT}/locales"]
|
||||
LAYOUT = "#{VIEWS}/layout.erb"
|
||||
ASSETS = "#{ROOT}/assets"
|
||||
ROOT = File.expand_path("#{File.dirname(__FILE__)}/../../web")
|
||||
VIEWS = "#{ROOT}/views".freeze
|
||||
LOCALES = ["#{ROOT}/locales".freeze]
|
||||
LAYOUT = "#{VIEWS}/layout.erb".freeze
|
||||
ASSETS = "#{ROOT}/assets".freeze
|
||||
|
||||
DEFAULT_TABS = {
|
||||
"Dashboard" => '',
|
||||
|
@ -58,9 +56,7 @@ module Sidekiq
|
|||
end
|
||||
|
||||
def initialize
|
||||
secret = Web.session_secret
|
||||
|
||||
if secret.nil?
|
||||
unless secret = Web.session_secret
|
||||
require 'securerandom'
|
||||
secret = SecureRandom.hex(64)
|
||||
end
|
||||
|
@ -91,9 +87,12 @@ module Sidekiq
|
|||
def self.register(extension)
|
||||
extension.registered(WebApplication)
|
||||
end
|
||||
|
||||
ERB.new(File.read LAYOUT).def_method(WebAction, '_render')
|
||||
end
|
||||
|
||||
Sidekiq::WebApplication.helpers WebHelpers
|
||||
Sidekiq::WebApplication.helpers Sidekiq::Paginator
|
||||
|
||||
ERB.new(File.read Web::LAYOUT).def_method(WebAction, '_render')
|
||||
end
|
||||
|
||||
if defined?(::ActionDispatch::Request::Session) &&
|
||||
|
|
63
lib/sidekiq/web/action.rb
Normal file
63
lib/sidekiq/web/action.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Sidekiq
|
||||
class WebAction
|
||||
RACK_SESSION = 'rack.session'.freeze
|
||||
|
||||
LOCATION = "Location".freeze
|
||||
|
||||
TEXT_HTML = { "Content-Type".freeze => "text/html".freeze }
|
||||
APPLICATION_JSON = { "Content-Type".freeze => "application/json".freeze }
|
||||
|
||||
attr_accessor :env, :app
|
||||
|
||||
def request
|
||||
@request ||= Rack::Request.new(env)
|
||||
end
|
||||
|
||||
def params
|
||||
request.params
|
||||
end
|
||||
|
||||
def route_params
|
||||
env[WebRouter::ROUTE_PARAMS]
|
||||
end
|
||||
|
||||
def session
|
||||
env[RACK_SESSION]
|
||||
end
|
||||
|
||||
def erb(content, options = {})
|
||||
b = binding
|
||||
|
||||
if locals = options[:locals]
|
||||
locals.each {|k, v| b.local_variable_set(k, v) }
|
||||
end
|
||||
|
||||
_render { ERB.new(content).result(b) }
|
||||
end
|
||||
|
||||
def partial(file, locals = {})
|
||||
ERB.new(File.read "#{Web::VIEWS}/_#{file}.erb").result(binding)
|
||||
end
|
||||
|
||||
def redirect(location)
|
||||
[302, { LOCATION => "#{request.base_url}#{root_path}#{location}" }, []]
|
||||
end
|
||||
|
||||
def render(file, locals = {})
|
||||
output = erb(File.read "#{Web::VIEWS}/#{file}.erb", locals: locals)
|
||||
|
||||
[200, TEXT_HTML, [output]]
|
||||
end
|
||||
|
||||
def json(payload)
|
||||
[200, APPLICATION_JSON, [Sidekiq.dump_json(payload)]]
|
||||
end
|
||||
|
||||
def initialize(env, app)
|
||||
@env = env
|
||||
@app = app
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,9 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Sidekiq
|
||||
class WebApplication
|
||||
extend WebRouter
|
||||
|
||||
REDIS_KEYS = %w(redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human)
|
||||
NOPE = [404, {}, []]
|
||||
|
||||
get "/" do
|
||||
@redis_info = redis_info.select{ |k, v| REDIS_KEYS.include? k }
|
||||
|
@ -232,8 +234,6 @@ module Sidekiq
|
|||
json Sidekiq::Stats::Queues.new.lengths
|
||||
end
|
||||
|
||||
NOPE = [404, {}, []]
|
||||
|
||||
def call(env)
|
||||
action = self.class.match(env)
|
||||
return NOPE unless action
|
||||
|
@ -241,7 +241,15 @@ module Sidekiq
|
|||
self.class.run_befores(env)
|
||||
resp = action.instance_exec env, &action.app
|
||||
self.class.run_afters(env)
|
||||
|
||||
case resp
|
||||
when Array
|
||||
resp
|
||||
when Integer
|
||||
[resp, {}, []]
|
||||
else
|
||||
[200, WebAction::TEXT_HTML, [resp]]
|
||||
end
|
||||
end
|
||||
|
||||
def self.helpers(mod)
|
||||
|
@ -275,6 +283,5 @@ module Sidekiq
|
|||
def self.afters
|
||||
@afters ||= []
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
require 'uri'
|
||||
require 'yaml'
|
||||
|
||||
module Sidekiq
|
||||
# This is not a public API
|
||||
|
@ -251,5 +252,23 @@ module Sidekiq
|
|||
"#{redis_connection}#{namespace_suffix}"
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
require 'sidekiq/web/helpers'
|
||||
|
||||
module Sidekiq
|
||||
module WebRouter
|
||||
|
@ -38,78 +37,6 @@ module Sidekiq
|
|||
end
|
||||
end
|
||||
|
||||
class WebAction
|
||||
include WebHelpers
|
||||
include Sidekiq::Paginator
|
||||
|
||||
RACK_SESSION = 'rack.session'.freeze
|
||||
|
||||
CONTENT_TYPE = "Content-Type".freeze
|
||||
LOCATION = "Location".freeze
|
||||
|
||||
TEXT_HTML = "text/html".freeze
|
||||
APPLICATION_JSON = "application/json".freeze
|
||||
|
||||
attr_accessor :env, :app
|
||||
|
||||
def request
|
||||
@request ||= Rack::Request.new(env)
|
||||
end
|
||||
|
||||
def params
|
||||
request.params
|
||||
end
|
||||
|
||||
def route_params
|
||||
env[WebRouter::ROUTE_PARAMS]
|
||||
end
|
||||
|
||||
def session
|
||||
env[RACK_SESSION]
|
||||
end
|
||||
|
||||
def render(file)
|
||||
output = _render { ERB.new(File.read "#{Web::VIEWS}/#{file}.erb").result(binding) }
|
||||
|
||||
[200, { CONTENT_TYPE => TEXT_HTML }, [output]]
|
||||
end
|
||||
|
||||
def redirect(location)
|
||||
[302, { LOCATION => "#{request.base_url}#{root_path}#{location}" }, []]
|
||||
end
|
||||
|
||||
def json(payload)
|
||||
[200, { CONTENT_TYPE => APPLICATION_JSON }, [Sidekiq.dump_json(payload)]]
|
||||
end
|
||||
|
||||
def partial(file, locals = {})
|
||||
ERB.new(File.read "#{Web::VIEWS}/_#{file}.erb").result(binding)
|
||||
end
|
||||
|
||||
def initialize(env, app)
|
||||
@env = env
|
||||
@app = app
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
class WebRoute
|
||||
attr_accessor :request_method, :pattern, :app, :constraints, :name
|
||||
|
||||
|
@ -156,18 +83,5 @@ module Sidekiq
|
|||
|
||||
true
|
||||
end
|
||||
|
||||
def eql?(o)
|
||||
o.is_a?(self.class) &&
|
||||
o.request_method == request_method &&
|
||||
o.pattern == pattern &&
|
||||
o.app == app &&
|
||||
o.constraints == constraints
|
||||
end
|
||||
alias == eql?
|
||||
|
||||
def hash
|
||||
request_method.hash ^ pattern.hash ^ app.hash ^ constraints.hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue