2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2013-10-13 18:56:17 -04:00
|
|
|
require 'uri'
|
2016-11-12 17:39:08 -05:00
|
|
|
require 'set'
|
2016-07-27 15:18:52 -04:00
|
|
|
require 'yaml'
|
2013-10-13 18:56:17 -04:00
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
# This is not a public API
|
|
|
|
module WebHelpers
|
2015-04-09 17:26:03 -04:00
|
|
|
def strings(lang)
|
|
|
|
@@strings ||= {}
|
|
|
|
@@strings[lang] ||= begin
|
2013-10-21 15:07:18 -04:00
|
|
|
# Allow sidekiq-web extensions to add locale paths
|
|
|
|
# so extensions can be localized
|
2016-07-28 22:36:21 -04:00
|
|
|
settings.locales.each_with_object({}) do |path, global|
|
2015-04-09 17:26:03 -04:00
|
|
|
find_locale_files(lang).each do |file|
|
2013-10-21 15:07:18 -04:00
|
|
|
strs = YAML.load(File.open(file))
|
2015-04-09 17:26:03 -04:00
|
|
|
global.deep_merge!(strs[lang])
|
2013-10-21 15:07:18 -04:00
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-06 13:29:41 -05:00
|
|
|
def clear_caches
|
|
|
|
@@strings = nil
|
|
|
|
@@locale_files = nil
|
|
|
|
end
|
|
|
|
|
2015-04-09 17:26:03 -04:00
|
|
|
def locale_files
|
2016-07-28 22:36:21 -04:00
|
|
|
@@locale_files ||= settings.locales.flat_map do |path|
|
2015-04-09 17:26:03 -04:00
|
|
|
Dir["#{path}/*.yml"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_locale_files(lang)
|
|
|
|
locale_files.select { |file| file =~ /\/#{lang}\.yml$/ }
|
|
|
|
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
|
|
|
|
|
2015-03-11 05:38:52 -04:00
|
|
|
# This view helper provide ability display you html code in
|
|
|
|
# to head of page. Example:
|
|
|
|
#
|
|
|
|
# <% add_to_head do %>
|
|
|
|
# <link rel="stylesheet" .../>
|
|
|
|
# <meta .../>
|
|
|
|
# <% end %>
|
|
|
|
#
|
2016-07-28 22:36:21 -04:00
|
|
|
def add_to_head
|
2015-03-11 05:38:52 -04:00
|
|
|
@head_html ||= []
|
2016-07-28 22:36:21 -04:00
|
|
|
@head_html << yield.dup if block_given?
|
2015-03-11 05:38:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def display_custom_head
|
2016-08-30 16:29:03 -04:00
|
|
|
@head_html.join if defined?(@head_html)
|
2015-03-11 05:38:52 -04:00
|
|
|
end
|
|
|
|
|
2016-09-12 17:30:55 -04:00
|
|
|
def poll_path
|
|
|
|
if current_path != '' && params['poll']
|
|
|
|
root_path + current_path
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-09 14:14:05 -04:00
|
|
|
# Given a browser request Accept-Language header like
|
|
|
|
# "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2", this function
|
|
|
|
# will return "fr" since that's the first code with a matching
|
|
|
|
# locale in web/locales
|
2013-10-13 18:56:17 -04:00
|
|
|
def locale
|
2015-04-09 14:14:05 -04:00
|
|
|
@locale ||= begin
|
|
|
|
locale = 'en'.freeze
|
2016-07-26 10:03:17 -04:00
|
|
|
languages = env['HTTP_ACCEPT_LANGUAGE'.freeze] || 'en'.freeze
|
2015-04-09 14:14:05 -04:00
|
|
|
languages.downcase.split(','.freeze).each do |lang|
|
2015-05-15 15:11:01 -04:00
|
|
|
next if lang == '*'.freeze
|
2015-04-09 14:14:05 -04:00
|
|
|
lang = lang.split(';'.freeze)[0]
|
2015-04-09 17:26:03 -04:00
|
|
|
break locale = lang if find_locale_files(lang).any?
|
2015-04-09 14:14:05 -04:00
|
|
|
end
|
|
|
|
locale
|
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
2016-11-19 16:24:13 -05:00
|
|
|
# mperham/sidekiq#3243
|
|
|
|
def unfiltered?
|
|
|
|
yield unless env['PATH_INFO'].start_with?("/filter/")
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
def get_locale
|
2015-04-09 17:26:03 -04:00
|
|
|
strings(locale)
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def t(msg, options={})
|
|
|
|
string = get_locale[msg] || msg
|
2014-03-19 20:11:12 -04:00
|
|
|
if options.empty?
|
|
|
|
string
|
|
|
|
else
|
|
|
|
string % options
|
|
|
|
end
|
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
|
|
|
|
|
2015-01-16 13:59:41 -05:00
|
|
|
def processes
|
|
|
|
@processes ||= Sidekiq::ProcessSet.new
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
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
|
|
|
|
|
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
|
2014-12-17 15:09:20 -05:00
|
|
|
@@ns ||= Sidekiq.redis { |conn| conn.respond_to?(:namespace) ? conn.namespace : nil }
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
2014-07-27 20:36:12 -04:00
|
|
|
def redis_info
|
2016-02-25 17:29:41 -05:00
|
|
|
Sidekiq.redis_info
|
2014-07-27 20:36:12 -04:00
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
def root_path
|
|
|
|
"#{env['SCRIPT_NAME']}/"
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_path
|
|
|
|
@current_path ||= request.path_info.gsub(/^\//,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_status
|
2015-01-16 17:03:19 -05:00
|
|
|
workers.size == 0 ? 'idle' : 'active'
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def relative_time(time)
|
2017-01-08 20:00:55 -05:00
|
|
|
stamp = time.getutc.iso8601
|
|
|
|
%{<time title="#{stamp}" datetime="#{stamp}">#{time}</time>}
|
2013-10-13 18:56:17 -04:00
|
|
|
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
|
2014-12-17 15:09:20 -05:00
|
|
|
params.merge(options).map do |key, value|
|
2013-10-25 21:52:59 -04:00
|
|
|
SAFE_QPARAMS.include?(key) ? "#{key}=#{value}" : next
|
2015-11-15 17:07:02 -05:00
|
|
|
end.compact.join("&")
|
2013-10-25 21:52:59 -04:00
|
|
|
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|
|
2016-04-06 18:08:47 -04:00
|
|
|
h(truncate(to_display(arg), truncate_after_chars))
|
2013-10-13 18:56:17 -04:00
|
|
|
end.join(", ")
|
|
|
|
end
|
|
|
|
|
2015-07-06 18:52:41 -04:00
|
|
|
def csrf_tag
|
|
|
|
"<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>"
|
2015-07-06 15:51:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-26 13:06:23 -04:00
|
|
|
def to_display(arg)
|
|
|
|
begin
|
|
|
|
arg.inspect
|
|
|
|
rescue
|
|
|
|
begin
|
|
|
|
arg.to_s
|
|
|
|
rescue => ex
|
|
|
|
"Cannot display argument: [#{ex.class.name}] #{ex.message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
RETRY_JOB_KEYS = Set.new(%w(
|
|
|
|
queue class args retry_count retried_at failed_at
|
|
|
|
jid error_message error_class backtrace
|
2015-03-24 17:57:17 -04:00
|
|
|
error_backtrace enqueued_at retry wrapped
|
2015-12-09 14:44:47 -05:00
|
|
|
created_at
|
2013-10-13 18:56:17 -04:00
|
|
|
))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-12-17 15:09:20 -05:00
|
|
|
options = {delimiter: ',', separator: '.'}
|
2013-10-13 18:56:17 -04:00
|
|
|
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
|
2014-10-15 22:51:04 -04:00
|
|
|
|
|
|
|
def environment_title_prefix
|
|
|
|
environment = Sidekiq.options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
|
|
|
|
|
|
|
"[#{environment.upcase}] " unless environment == "production"
|
|
|
|
end
|
2015-01-21 13:36:17 -05:00
|
|
|
|
|
|
|
def product_version
|
|
|
|
"Sidekiq v#{Sidekiq::VERSION}"
|
|
|
|
end
|
2015-11-06 16:18:50 -05:00
|
|
|
|
2015-11-06 16:24:57 -05:00
|
|
|
def redis_connection_and_namespace
|
2015-11-09 13:09:51 -05:00
|
|
|
@redis_connection_and_namespace ||= begin
|
2015-11-13 12:14:00 -05:00
|
|
|
namespace_suffix = namespace == nil ? '' : "##{namespace}"
|
2015-11-09 13:09:51 -05:00
|
|
|
"#{redis_connection}#{namespace_suffix}"
|
|
|
|
end
|
2015-11-06 16:18:50 -05:00
|
|
|
end
|
2016-07-27 15:18:52 -04:00
|
|
|
|
|
|
|
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
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
end
|