2013-10-13 18:56:17 -04:00
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
# This is not a public API
|
|
|
|
module WebHelpers
|
|
|
|
def strings
|
|
|
|
@@strings ||= begin
|
2013-10-21 15:07:18 -04:00
|
|
|
# Allow sidekiq-web extensions to add locale paths
|
|
|
|
# so extensions can be localized
|
|
|
|
settings.locales.each_with_object({}) do |path,global|
|
|
|
|
Dir["#{path}/*.yml"].each_with_object(global) do |file,hash|
|
|
|
|
strs = YAML.load(File.open(file))
|
|
|
|
hash.deep_merge!(strs)
|
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-10 15:39:14 -05:00
|
|
|
# This is a hook for a Sidekiq Pro feature. Please don't touch.
|
|
|
|
def filtering(*)
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
def locale
|
|
|
|
lang = (request.env["HTTP_ACCEPT_LANGUAGE"] || 'en')[0,2]
|
|
|
|
strings[lang] ? lang : 'en'
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_locale
|
|
|
|
strings[locale]
|
|
|
|
end
|
|
|
|
|
|
|
|
def t(msg, options={})
|
|
|
|
string = get_locale[msg] || msg
|
|
|
|
string % options
|
|
|
|
end
|
|
|
|
|
|
|
|
def workers_size
|
2014-03-08 17:33:25 -05:00
|
|
|
@workers_size ||= workers.size
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def workers
|
2014-03-02 19:36:00 -05:00
|
|
|
@workers ||= Sidekiq::Workers.new
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def stats
|
|
|
|
@stats ||= Sidekiq::Stats.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def retries_with_score(score)
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.zrangebyscore('retry', score, score)
|
|
|
|
end.map { |msg| Sidekiq.load_json(msg) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
|
|
|
Sidekiq.redis { |conn| conn.client.location }
|
|
|
|
end
|
2013-10-25 21:52:59 -04:00
|
|
|
|
2013-10-22 10:15:24 -04:00
|
|
|
def redis_connection
|
|
|
|
Sidekiq.redis { |conn| conn.client.id }
|
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
|
|
|
|
def namespace
|
|
|
|
@@ns ||= Sidekiq.redis {|conn| conn.respond_to?(:namespace) ? conn.namespace : nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def root_path
|
|
|
|
"#{env['SCRIPT_NAME']}/"
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_path
|
|
|
|
@current_path ||= request.path_info.gsub(/^\//,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_status
|
|
|
|
workers_size == 0 ? 'idle' : 'active'
|
|
|
|
end
|
|
|
|
|
|
|
|
def relative_time(time)
|
|
|
|
%{<time datetime="#{time.getutc.iso8601}">#{time}</time>}
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_params(job, score)
|
|
|
|
"#{score}-#{job['jid']}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_params(params)
|
|
|
|
score, jid = params.split("-")
|
|
|
|
[score.to_f, jid]
|
|
|
|
end
|
|
|
|
|
2013-10-25 21:52:59 -04:00
|
|
|
SAFE_QPARAMS = %w(page poll)
|
|
|
|
|
|
|
|
# Merge options with current params, filter safe params, and stringify to query string
|
|
|
|
def qparams(options)
|
|
|
|
options = options.stringify_keys
|
|
|
|
params.merge(options).map { |key, value|
|
|
|
|
SAFE_QPARAMS.include?(key) ? "#{key}=#{value}" : next
|
|
|
|
}.join("&")
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
def truncate(text, truncate_after_chars = 2000)
|
|
|
|
truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text
|
|
|
|
end
|
|
|
|
|
|
|
|
def display_args(args, truncate_after_chars = 2000)
|
|
|
|
args.map do |arg|
|
|
|
|
a = arg.inspect
|
2013-10-30 18:10:23 -04:00
|
|
|
h(truncate(a))
|
2013-10-13 18:56:17 -04:00
|
|
|
end.join(", ")
|
|
|
|
end
|
|
|
|
|
|
|
|
RETRY_JOB_KEYS = Set.new(%w(
|
|
|
|
queue class args retry_count retried_at failed_at
|
|
|
|
jid error_message error_class backtrace
|
|
|
|
error_backtrace enqueued_at retry
|
|
|
|
))
|
|
|
|
|
|
|
|
def retry_extra_items(retry_job)
|
|
|
|
@retry_extra_items ||= {}.tap do |extra|
|
|
|
|
retry_job.item.each do |key, value|
|
|
|
|
extra[key] = value unless RETRY_JOB_KEYS.include?(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def number_with_delimiter(number)
|
|
|
|
begin
|
|
|
|
Float(number)
|
|
|
|
rescue ArgumentError, TypeError
|
|
|
|
return number
|
|
|
|
end
|
|
|
|
|
|
|
|
options = {:delimiter => ',', :separator => '.'}
|
|
|
|
parts = number.to_s.to_str.split('.')
|
|
|
|
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
|
|
|
|
parts.join(options[:separator])
|
|
|
|
end
|
|
|
|
|
|
|
|
def h(text)
|
2013-11-03 13:04:41 -05:00
|
|
|
::Rack::Utils.escape_html(text)
|
2013-11-01 19:17:15 -04:00
|
|
|
rescue ArgumentError => e
|
|
|
|
raise unless e.message.eql?('invalid byte sequence in UTF-8')
|
|
|
|
text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode!('UTF-8', 'UTF-16')
|
|
|
|
retry
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Any paginated list that performs an action needs to redirect
|
|
|
|
# back to the proper page after performing that action.
|
|
|
|
def redirect_with_query(url)
|
|
|
|
r = request.referer
|
|
|
|
if r && r =~ /\?/
|
|
|
|
ref = URI(r)
|
|
|
|
redirect("#{url}?#{ref.query}")
|
|
|
|
else
|
|
|
|
redirect url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|