2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2019-04-01 12:20:41 -04:00
|
|
|
|
|
|
|
require "uri"
|
|
|
|
require "set"
|
|
|
|
require "yaml"
|
|
|
|
require "cgi"
|
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)
|
2019-04-01 12:20:41 -04:00
|
|
|
@strings ||= {}
|
2021-05-24 15:30:03 -04:00
|
|
|
|
|
|
|
# Allow sidekiq-web extensions to add locale paths
|
|
|
|
# so extensions can be localized
|
|
|
|
@strings[lang] ||= settings.locales.each_with_object({}) do |path, global|
|
|
|
|
find_locale_files(lang).each do |file|
|
|
|
|
strs = YAML.load(File.open(file))
|
|
|
|
global.merge!(strs[lang])
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-18 19:28:44 -05:00
|
|
|
def singularize(str, count)
|
|
|
|
if count == 1 && str.respond_to?(:singularize) # rails
|
|
|
|
str.singularize
|
|
|
|
else
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-06 13:29:41 -05:00
|
|
|
def clear_caches
|
2019-04-01 12:20:41 -04:00
|
|
|
@strings = nil
|
|
|
|
@locale_files = nil
|
|
|
|
@available_locales = nil
|
2016-01-06 13:29:41 -05:00
|
|
|
end
|
|
|
|
|
2015-04-09 17:26:03 -04:00
|
|
|
def locale_files
|
2019-04-01 12:20:41 -04:00
|
|
|
@locale_files ||= settings.locales.flat_map { |path|
|
2015-04-09 17:26:03 -04:00
|
|
|
Dir["#{path}/*.yml"]
|
2019-04-01 12:20:41 -04:00
|
|
|
}
|
2015-04-09 17:26:03 -04:00
|
|
|
end
|
|
|
|
|
2017-05-01 13:47:51 -04:00
|
|
|
def available_locales
|
2019-04-01 12:20:41 -04:00
|
|
|
@available_locales ||= locale_files.map { |path| File.basename(path, ".yml") }.uniq
|
2017-05-01 13:47:51 -04:00
|
|
|
end
|
|
|
|
|
2015-04-09 17:26:03 -04:00
|
|
|
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
|
|
|
|
|
2017-03-16 16:51:29 -04:00
|
|
|
def text_direction
|
2019-04-01 12:20:41 -04:00
|
|
|
get_locale["TextDirection"] || "ltr"
|
2017-03-16 16:51:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rtl?
|
2019-04-01 12:20:41 -04:00
|
|
|
text_direction == "rtl"
|
2017-03-16 16:51:29 -04:00
|
|
|
end
|
|
|
|
|
2017-05-01 13:47:51 -04:00
|
|
|
# See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
|
|
|
|
def user_preferred_languages
|
2019-04-01 12:20:41 -04:00
|
|
|
languages = env["HTTP_ACCEPT_LANGUAGE"]
|
|
|
|
languages.to_s.downcase.gsub(/\s+/, "").split(",").map { |language|
|
|
|
|
locale, quality = language.split(";q=", 2)
|
2019-08-28 13:13:41 -04:00
|
|
|
locale = nil if locale == "*" # Ignore wildcards
|
2017-05-01 13:47:51 -04:00
|
|
|
quality = quality ? quality.to_f : 1.0
|
|
|
|
[locale, quality]
|
2019-04-01 12:20:41 -04:00
|
|
|
}.sort { |(_, left), (_, right)|
|
2017-05-01 13:47:51 -04:00
|
|
|
right <=> left
|
2019-04-01 12:20:41 -04:00
|
|
|
}.map(&:first).compact
|
2017-05-01 13:47:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Given an Accept-Language header like "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2"
|
|
|
|
# this method will try to best match the available locales to the user's preferred languages.
|
|
|
|
#
|
|
|
|
# Inspiration taken from https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language/parser.rb
|
2013-10-13 18:56:17 -04:00
|
|
|
def locale
|
2015-04-09 14:14:05 -04:00
|
|
|
@locale ||= begin
|
2019-04-01 12:20:41 -04:00
|
|
|
matched_locale = user_preferred_languages.map { |preferred|
|
|
|
|
preferred_language = preferred.split("-", 2).first
|
2017-05-01 13:47:51 -04:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
lang_group = available_locales.select { |available|
|
|
|
|
preferred_language == available.split("-", 2).first
|
|
|
|
}
|
2017-05-01 13:47:51 -04:00
|
|
|
|
|
|
|
lang_group.find { |lang| lang == preferred } || lang_group.min_by(&:length)
|
2019-04-01 12:20:41 -04:00
|
|
|
}.compact.first
|
2017-05-01 13:47:51 -04:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
matched_locale || "en"
|
2015-04-09 14:14:05 -04:00
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
2019-09-24 11:35:05 -04:00
|
|
|
# within is used by Sidekiq Pro
|
|
|
|
def display_tags(job, within = nil)
|
2019-09-23 17:52:15 -04:00
|
|
|
job.tags.map { |tag|
|
2021-06-22 19:49:33 -04:00
|
|
|
"<span class='label label-info jobtag'>#{::Rack::Utils.escape_html(tag)}</span>"
|
2019-09-23 17:52:15 -04:00
|
|
|
}.join(" ")
|
|
|
|
end
|
|
|
|
|
2016-11-19 16:24:13 -05:00
|
|
|
# mperham/sidekiq#3243
|
|
|
|
def unfiltered?
|
2019-04-01 12:20:41 -04:00
|
|
|
yield unless env["PATH_INFO"].start_with?("/filter/")
|
2016-11-19 16:24:13 -05:00
|
|
|
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
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
def t(msg, options = {})
|
|
|
|
string = get_locale[msg] || strings("en")[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
|
|
|
|
|
2019-09-07 17:49:28 -04:00
|
|
|
def sort_direction_label
|
|
|
|
params[:direction] == "asc" ? "↑" : "↓"
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
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
|
|
|
|
|
2013-10-22 10:15:24 -04:00
|
|
|
def redis_connection
|
2018-01-11 10:12:29 -05:00
|
|
|
Sidekiq.redis do |conn|
|
2021-03-12 10:44:19 -05:00
|
|
|
conn.connection[:id]
|
2018-01-11 10:12:29 -05:00
|
|
|
end
|
2013-10-22 10:15:24 -04:00
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
|
|
|
|
def namespace
|
2018-05-06 22:01:55 -04: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
|
2019-04-01 12:20:41 -04:00
|
|
|
"#{env["SCRIPT_NAME"]}/"
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_path
|
2019-04-01 12:20:41 -04:00
|
|
|
@current_path ||= request.path_info.gsub(/^\//, "")
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_status
|
2019-04-01 12:20:41 -04: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
|
2019-04-01 12:20:41 -04:00
|
|
|
%(<time class="ltr" dir="ltr" title="#{stamp}" datetime="#{stamp}">#{time}</time>)
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def job_params(job, score)
|
2019-04-01 12:20:41 -04:00
|
|
|
"#{score}-#{job["jid"]}"
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_params(params)
|
2018-07-27 02:45:43 -04:00
|
|
|
score, jid = params.split("-", 2)
|
2013-10-13 18:56:17 -04:00
|
|
|
[score.to_f, jid]
|
|
|
|
end
|
|
|
|
|
2021-08-30 15:31:41 -04:00
|
|
|
SAFE_QPARAMS = %w[page direction]
|
2013-10-25 21:52:59 -04:00
|
|
|
|
|
|
|
# Merge options with current params, filter safe params, and stringify to query string
|
|
|
|
def qparams(options)
|
2020-05-22 18:38:52 -04:00
|
|
|
stringified_options = options.transform_keys(&:to_s)
|
2017-05-13 01:03:32 -04:00
|
|
|
|
2020-05-22 18:38:52 -04:00
|
|
|
to_query_string(params.merge(stringified_options))
|
2019-09-08 09:27:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_query_string(params)
|
|
|
|
params.map { |key, value|
|
2017-02-15 12:29:17 -05:00
|
|
|
SAFE_QPARAMS.include?(key) ? "#{key}=#{CGI.escape(value.to_s)}" : next
|
2019-04-01 12:20:41 -04:00
|
|
|
}.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)
|
2019-04-01 12:20:41 -04:00
|
|
|
return "Invalid job payload, args is nil" if args.nil?
|
|
|
|
return "Invalid job payload, args must be an Array, not #{args.class.name}" unless args.is_a?(Array)
|
2019-03-01 16:25:56 -05:00
|
|
|
|
|
|
|
begin
|
2019-04-01 12:20:41 -04:00
|
|
|
args.map { |arg|
|
2019-03-01 16:25:56 -05:00
|
|
|
h(truncate(to_display(arg), truncate_after_chars))
|
2019-04-01 12:20:41 -04:00
|
|
|
}.join(", ")
|
2019-03-01 16:25:56 -05:00
|
|
|
rescue
|
|
|
|
"Illegal job arguments: #{h args.inspect}"
|
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
|
2015-07-06 18:52:41 -04:00
|
|
|
def csrf_tag
|
2020-06-03 19:06:36 -04:00
|
|
|
"<input type='hidden' name='authenticity_token' value='#{env[:csrf_token]}'/>"
|
2015-07-06 15:51:15 -04:00
|
|
|
end
|
|
|
|
|
2015-06-26 13:06:23 -04:00
|
|
|
def to_display(arg)
|
2019-04-01 12:20:41 -04:00
|
|
|
arg.inspect
|
|
|
|
rescue
|
2015-06-26 13:06:23 -04:00
|
|
|
begin
|
2019-04-01 12:20:41 -04:00
|
|
|
arg.to_s
|
|
|
|
rescue => ex
|
|
|
|
"Cannot display argument: [#{ex.class.name}] #{ex.message}"
|
2015-06-26 13:06:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
RETRY_JOB_KEYS = Set.new(%w[
|
2013-10-13 18:56:17 -04:00
|
|
|
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
|
2022-01-26 20:02:02 -05:00
|
|
|
created_at tags display_class
|
2019-04-01 12:20:41 -04:00
|
|
|
])
|
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
|
|
|
|
|
2020-10-14 18:09:50 -04:00
|
|
|
def format_memory(rss_kb)
|
2021-03-12 19:03:36 -05:00
|
|
|
return "0" if rss_kb.nil? || rss_kb == 0
|
2020-10-14 18:09:50 -04:00
|
|
|
|
|
|
|
if rss_kb < 100_000
|
|
|
|
"#{number_with_delimiter(rss_kb)} KB"
|
2021-02-18 19:28:44 -05:00
|
|
|
elsif rss_kb < 10_000_000
|
2020-10-14 18:09:50 -04:00
|
|
|
"#{number_with_delimiter((rss_kb / 1024.0).to_i)} MB"
|
2021-02-18 19:28:44 -05:00
|
|
|
else
|
|
|
|
"#{number_with_delimiter((rss_kb / (1024.0 * 1024.0)).round(1))} GB"
|
2020-10-14 18:09:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
def number_with_delimiter(number)
|
2020-10-14 18:09:50 -04:00
|
|
|
return "" if number.nil?
|
|
|
|
|
2013-10-13 18:56:17 -04:00
|
|
|
begin
|
|
|
|
Float(number)
|
|
|
|
rescue ArgumentError, TypeError
|
|
|
|
return number
|
|
|
|
end
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
options = {delimiter: ",", separator: "."}
|
|
|
|
parts = number.to_s.to_str.split(".")
|
2013-10-13 18:56:17 -04:00
|
|
|
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
|
2019-04-01 12:20:41 -04:00
|
|
|
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")
|
2013-11-01 19:17:15 -04:00
|
|
|
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
|
2020-01-13 20:03:18 -05:00
|
|
|
environment = Sidekiq.options[:environment] || ENV["APP_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
|
2014-10-15 22:51:04 -04:00
|
|
|
|
|
|
|
"[#{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
|
|
|
|
2017-05-04 10:20:05 -04:00
|
|
|
def server_utc_time
|
2019-04-01 12:20:41 -04:00
|
|
|
Time.now.utc.strftime("%H:%M:%S UTC")
|
2017-05-04 10:20:05 -04:00
|
|
|
end
|
|
|
|
|
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
|
2019-04-01 12:20:41 -04: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)
|
2019-04-01 12:20:41 -04:00
|
|
|
if params["retry"]
|
2016-07-27 15:18:52 -04:00
|
|
|
job.retry
|
2019-04-01 12:20:41 -04:00
|
|
|
elsif params["delete"]
|
2016-07-27 15:18:52 -04:00
|
|
|
job.delete
|
2019-04-01 12:20:41 -04:00
|
|
|
elsif params["kill"]
|
2016-07-27 15:18:52 -04:00
|
|
|
job.kill
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_or_add_queue(job, params)
|
2019-04-01 12:20:41 -04:00
|
|
|
if params["delete"]
|
2016-07-27 15:18:52 -04:00
|
|
|
job.delete
|
2019-04-01 12:20:41 -04:00
|
|
|
elsif params["add_to_queue"]
|
2016-07-27 15:18:52 -04:00
|
|
|
job.add_to_queue
|
|
|
|
end
|
|
|
|
end
|
2013-10-13 18:56:17 -04:00
|
|
|
end
|
|
|
|
end
|