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

Helpers::VIEW_PATH

Instance Method Summary collapse

Methods included from Helpers

drain_zset, get_template, parse_time, safe_relative_time

Instance Method Details

#dev_mode?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 25

def dev_mode?
  DEV_MODE
end

#format_context(batch) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 96

def format_context(batch)
  bits = []
  own_keys = batch.context.own.keys
  batch.context.flatten.each do |k,v|
    added = own_keys.include? k
    bits << "  <span class=\"key #{added ? 'own' : 'inherited'}\">\"#{k}\": #{v.to_json},</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` 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` 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



42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 42

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