Module: Sidekiq::WebHelpers
- Defined in:
- lib/sidekiq/web_helpers.rb
Overview
This is not a public API
Constant Summary collapse
- SAFE_QPARAMS =
%w(page poll)
- 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 ))
Instance Method Summary collapse
- #current_path ⇒ Object
- #current_status ⇒ Object
- #display_args(args, truncate_after_chars = 2000) ⇒ Object
-
#filtering ⇒ Object
This is a hook for a Sidekiq Pro feature.
- #get_locale ⇒ Object
- #h(text) ⇒ Object
- #job_params(job, score) ⇒ Object
- #locale ⇒ Object
- #location ⇒ Object
- #namespace ⇒ Object
- #number_with_delimiter(number) ⇒ Object
- #parse_params(params) ⇒ Object
-
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string.
-
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
- #redis_connection ⇒ Object
- #relative_time(time) ⇒ Object
- #retries_with_score(score) ⇒ Object
- #retry_extra_items(retry_job) ⇒ Object
- #root_path ⇒ Object
- #stats ⇒ Object
- #strings ⇒ Object
- #t(msg, options = {}) ⇒ Object
- #truncate(text, truncate_after_chars = 2000) ⇒ Object
- #workers ⇒ Object
- #workers_size ⇒ Object
Instance Method Details
#current_path ⇒ Object
75 76 77 |
# File 'lib/sidekiq/web_helpers.rb', line 75 def current_path @current_path ||= request.path_info.gsub(/^\//,'') end |
#current_status ⇒ Object
79 80 81 |
# File 'lib/sidekiq/web_helpers.rb', line 79 def current_status workers_size == 0 ? 'idle' : 'active' end |
#display_args(args, truncate_after_chars = 2000) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/sidekiq/web_helpers.rb', line 110 def display_args(args, truncate_after_chars = 2000) args.map do |arg| a = arg.inspect h(truncate(a)) end.join(", ") end |
#filtering ⇒ Object
This is a hook for a Sidekiq Pro feature. Please don’t touch.
20 21 |
# File 'lib/sidekiq/web_helpers.rb', line 20 def filtering(*) end |
#get_locale ⇒ Object
28 29 30 |
# File 'lib/sidekiq/web_helpers.rb', line 28 def get_locale strings[locale] end |
#h(text) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/sidekiq/web_helpers.rb', line 144 def h(text) ::Rack::Utils.escape_html(text) rescue ArgumentError => e raise unless e..eql?('invalid byte sequence in UTF-8') text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode!('UTF-8', 'UTF-16') retry end |
#job_params(job, score) ⇒ Object
87 88 89 |
# File 'lib/sidekiq/web_helpers.rb', line 87 def job_params(job, score) "#{score}-#{job['jid']}" end |
#locale ⇒ Object
23 24 25 26 |
# File 'lib/sidekiq/web_helpers.rb', line 23 def locale lang = (request.env["HTTP_ACCEPT_LANGUAGE"] || 'en')[0,2] strings[lang] ? lang : 'en' end |
#location ⇒ Object
59 60 61 |
# File 'lib/sidekiq/web_helpers.rb', line 59 def location Sidekiq.redis { |conn| conn.client.location } end |
#namespace ⇒ Object
67 68 69 |
# File 'lib/sidekiq/web_helpers.rb', line 67 def namespace @@ns ||= Sidekiq.redis {|conn| conn.respond_to?(:namespace) ? conn.namespace : nil } end |
#number_with_delimiter(number) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sidekiq/web_helpers.rb', line 131 def number_with_delimiter(number) begin Float(number) rescue ArgumentError, TypeError return number end = {:delimiter => ',', :separator => '.'} parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{[:delimiter]}") parts.join([:separator]) end |
#parse_params(params) ⇒ Object
91 92 93 94 |
# File 'lib/sidekiq/web_helpers.rb', line 91 def parse_params(params) score, jid = params.split("-") [score.to_f, jid] end |
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string
99 100 101 102 103 104 |
# File 'lib/sidekiq/web_helpers.rb', line 99 def qparams() = .stringify_keys params.merge().map { |key, value| SAFE_QPARAMS.include?(key) ? "#{key}=#{value}" : next }.join("&") end |
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
154 155 156 157 158 159 160 161 162 |
# File 'lib/sidekiq/web_helpers.rb', line 154 def redirect_with_query(url) r = request.referer if r && r =~ /\?/ ref = URI(r) redirect("#{url}?#{ref.query}") else redirect url end end |
#redis_connection ⇒ Object
63 64 65 |
# File 'lib/sidekiq/web_helpers.rb', line 63 def redis_connection Sidekiq.redis { |conn| conn.client.id } end |
#relative_time(time) ⇒ Object
83 84 85 |
# File 'lib/sidekiq/web_helpers.rb', line 83 def relative_time(time) %{<time datetime="#{time.getutc.iso8601}">#{time}</time>} end |
#retries_with_score(score) ⇒ Object
53 54 55 56 57 |
# File 'lib/sidekiq/web_helpers.rb', line 53 def retries_with_score(score) Sidekiq.redis do |conn| conn.zrangebyscore('retry', score, score) end.map { |msg| Sidekiq.load_json(msg) } end |
#retry_extra_items(retry_job) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/sidekiq/web_helpers.rb', line 123 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 |
#root_path ⇒ Object
71 72 73 |
# File 'lib/sidekiq/web_helpers.rb', line 71 def root_path "#{env['SCRIPT_NAME']}/" end |
#stats ⇒ Object
49 50 51 |
# File 'lib/sidekiq/web_helpers.rb', line 49 def stats @stats ||= Sidekiq::Stats.new end |
#strings ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/sidekiq/web_helpers.rb', line 6 def strings @@strings ||= begin # 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 end end end |
#t(msg, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/sidekiq/web_helpers.rb', line 32 def t(msg, ={}) string = get_locale[msg] || msg if .empty? string else string % end end |
#truncate(text, truncate_after_chars = 2000) ⇒ Object
106 107 108 |
# File 'lib/sidekiq/web_helpers.rb', line 106 def truncate(text, truncate_after_chars = 2000) truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text end |
#workers ⇒ Object
45 46 47 |
# File 'lib/sidekiq/web_helpers.rb', line 45 def workers @workers ||= Sidekiq::Workers.new end |
#workers_size ⇒ Object
41 42 43 |
# File 'lib/sidekiq/web_helpers.rb', line 41 def workers_size @workers_size ||= workers.size end |