Module: Joblin::Batching::Compat::Sidekiq::Web::RouteHelpers
- Includes:
- Helpers
- Defined in:
- lib/joblin/batching/compat/sidekiq/web.rb
Overview
Helpers shared with the route blocks. We expose these via a module so the
same registration code works on both Sidekiq 7 (which accepts a block) and
Sidekiq 8 (whose Application.helpers only accepts a module).
Constant Summary
Constants included from Helpers
Instance Method Summary collapse
- #dev_mode? ⇒ Boolean
- #format_context(batch) ⇒ Object
-
#route_param(key) ⇒ Object
Read a route (path) parameter such as
:bidfrom/batches/:bid. - #tree_data(root_bid, slice: nil) ⇒ Object
-
#url_params(key) ⇒ Object
Read a query-string parameter such as
countorpage.
Methods included from Helpers
drain_zset, get_template, parse_time, safe_relative_time
Instance Method Details
#dev_mode? ⇒ Boolean
25 26 27 |
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 25 def dev_mode? DEV_MODE end |
#format_context(batch) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 108 def format_context(batch) bits = [] own_keys = batch.context.own.keys batch.context.flatten.each do |k,v| added = own_keys.include? k # Both the key and the JSON-encoded value are app/user data; escape # them so batch context can't inject markup into this page. safe_key = Rack::Utils.escape_html(k.to_s) safe_val = Rack::Utils.escape_html(v.to_json) bits << " <span class=\"key #{added ? 'own' : 'inherited'}\">\"#{safe_key}\": #{safe_val},</span>" end bits = [ "{ // <span class=\"own\">Added</span> / <span class=\"inherited\">Inherited</span>", *bits, '}' ] bits.join("\n") end |
#route_param(key) ⇒ Object
Read a route (path) parameter such as :bid from /batches/:bid.
We cannot rely on params[:bid] across majors: Sidekiq 7's params is
an indifferent hash that includes route params, but Sidekiq 8's params
returns only the query string (String keys), so params[:bid] would be
nil there. Sidekiq 8 exposes route params via route_params(:bid). We
read straight from the Rack env, which the router populates under this
key on both Sidekiq 7 (WebRouter::ROUTE_PARAMS) and Sidekiq 8, so the
same call works regardless of version and avoids deprecation warnings.
38 39 40 |
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 38 def route_param(key) (env["rack.route_params"] || {})[key.to_sym] end |
#tree_data(root_bid, slice: nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 54 def tree_data(root_bid, slice: nil) tree_bids = Joblin::Batching::Batch.bid_hierarchy(root_bid, slice: slice) Joblin::Batching::Batch.redis do |r| layer_data = ->(layer, parent = nil) { bid = layer[0] batch = Joblin::Batching::Batch.new(bid) jobs_total = r.hget("BID-#{bid}", "job_count").to_i jobs_pending = r.hget("BID-#{bid}", 'pending').to_i jobs_failed = r.scard("BID-#{bid}-failed").to_i jobs_dead = r.scard("BID-#{bid}-dead").to_i jobs_success = jobs_total - jobs_pending batches_total = r.hget("BID-#{bid}", 'children').to_i batches_success = r.scard("BID-#{bid}-batches-success").to_i batches_pending = batches_total - batches_success batches_failed = r.scard("BID-#{bid}-batches-failed").to_i status = 'in_progress' status = 'complete' if batches_pending == batches_failed && jobs_pending == jobs_failed status = 'success' if batches_pending == 0 && jobs_pending == 0 status = 'deleted' if bid != root_bid && !batch.parent_bid { bid: bid, created_at: r.hget("BID-#{bid}", 'created_at'), status: status, parent_bid: parent ? parent.bid : batch.parent_bid, description: batch.description, jobs: { pending_count: jobs_pending, successful_count: jobs_success, failed_count: jobs_failed, dead_count: jobs_dead, total_count: jobs_total, # items: batches.map{|b| layer_data[b] }, }, batches: { pending_count: batches_pending, successful_count: batches_success, failed_count: batches_failed, total_count: batches_total, items: layer[1].map{|b| layer_data[b, batch] }, }, } } data = layer_data[tree_bids] data[:batches][:slice] = slice if slice data end end |
#url_params(key) ⇒ Object
Read a query-string parameter such as count or page.
Sidekiq 7+ defines url_params(key) on the web action and returns the
query value only. Sidekiq 6.x has no url_params; there the action's
params is an indifferent hash that already merges the query string,
so we read from it. On Sidekiq 7/8 the action's own url_params
shadows this fallback (a class's instance method wins over one from an
included module), so this definition only takes effect on Sidekiq 6.x.
50 51 52 |
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 50 def url_params(key) params[key] end |