Module: Joblin::Batching::Compat::Sidekiq::Web

Defined in:
lib/joblin/batching/compat/sidekiq/web.rb,
lib/joblin/batching/compat/sidekiq/web/helpers.rb

Defined Under Namespace

Modules: Helpers, RouteHelpers

Constant Summary collapse

DEV_MODE =
(Rails) && Rails.env.development?) || !!ENV["SIDEKIQ_WEB_TESTING"]

Class Method Summary collapse

Class Method Details

.register_tabs(web_class) ⇒ Object

Register this extension's Batches + Pools tabs against web_class, choosing the call shape that matches the installed Sidekiq.

The Sidekiq::Web.register API changed twice across the supported range, so we capability-detect rather than parse version strings:

  • Sidekiq 7.3.9+ and 8.x expose Sidekiq::Web.configure. The Sidekiq 8 register emits a deprecation warning unless called through the configure block, so we use that. The keyword register registers the tabs itself via the tab/index zip, so we do not also set tabs[...].
  • Sidekiq 7.3.0-7.3.8 have the keyword register but no configure. We call register(ext, name:, tab:, index:) directly; it too registers the tabs from the zip.
  • Sidekiq <= 7.2.x only have the single-argument register(extension), which just calls extension.registered(app) and never touches tabs. There we fall back to the original behavior: register, then set the tabs manually.

Detect the keyword form by inspecting register's parameters for the name: keyword instead of relying on a Sidekiq version constant.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 258

def self.register_tabs(web_class) # rubocop:disable Metrics/MethodLength
  register_args = {
    name: "joblin_batches",
    tab: ["Batches", "Pools"],
    index: ["batches", "pools"]
  }

  if web_class.respond_to?(:configure)
    web_class.configure do |cfg|
      cfg.register(self, **register_args)
    end
  elsif web_class.method(:register).parameters.any? { |type, name| name == :name && (type == :key || type == :keyreq) }
    web_class.register(self, **register_args)
  else
    web_class.register(self)
    web_class.tabs["Batches"] = "batches"
    web_class.tabs["Pools"] = "pools"
  end
end

.registered(app) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/joblin/batching/compat/sidekiq/web.rb', line 128

def self.registered(app) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  app.helpers(RouteHelpers)

  # =============== BATCHES =============== #

  app.get "/batches" do
    @count = (url_params('count') || 25).to_i

    source_key = url_params('all_batches') ? "batches" : "BID-ROOT-bids"
    @current_page, @total_size, @batches = page(source_key, url_params('page'), @count)
    @batches = @batches.map {|b, score| Joblin::Batching::Batch.new(b) }

    erb(get_template(:batches))
  end

  app.get "/batches/:bid" do
    @bid = route_param(:bid)
    @batch = Joblin::Batching::Batch.new(@bid)

    @tree_data = tree_data(@bid)

    @count = (url_params('count') || 25).to_i
    @current_batches_page, @total_batches_size, @sub_batches = page("BID-#{@batch.bid}-bids", url_params('batch_page'), @count)
    @sub_batches = @sub_batches.map {|b, score| Joblin::Batching::Batch.new(b) }

    @current_jobs_page, @total_jobs_size, @jobs = page("BID-#{@batch.bid}-jids", url_params('job_page'), @count)
    @jobs = @jobs.map do |jid, score|
      { jid: jid, }
    end

    erb(get_template(:batch))
  end

  app.get "/batches/:bid/tree" do
    @bid = route_param(:bid)

    json(tree_data(@bid, slice: url_params('slice')))
  end

  app.post "/batches/all" do
    if url_params('delete')
      index_key = Joblin::Batching::Batch::INDEX_ALL_BATCHES ? "batches" : "BID-ROOT-bids"
      drain_zset(index_key) do |batches|
        batches.each do |bid|
          Joblin::Batching::Batch.cleanup_redis(bid)
        end
      end
    end

    redirect "#{root_path}batches"
  end

  app.post "/batches/:bid" do
    @bid = route_param(:bid)
    @batch = Joblin::Batching::Batch.new(@bid)

    if url_params('delete')
      Joblin::Batching::Batch.delete_prematurely!(@bid)
    end

    redirect_with_query("#{root_path}batches")
  end

  # =============== POOLS =============== #

  app.get "/pools" do
    @count = (url_params('count') || 25).to_i
    @current_page, @total_size, @pools = page('pools', url_params('page'), @count)
    @pools = @pools.map {|b, score| Joblin::Batching::Pool.new(b) }

    erb(get_template(:pools))
  end

  app.get "/pools/:pid" do
    @pid = route_param(:pid)
    @pool = Joblin::Batching::Pool.new(@pid)

    @active_tasks = @pool.active_jobs

    @count = (url_params('count') || 25).to_i
    @current_jobs_page, @total_jobs_size, @jobs = page("POOLID-#{@pool.pid}-jobs", url_params('job_page'), @count)
    @jobs = @jobs.map {|desc, score=nil| JSON.parse(desc)[0] }

    erb(get_template(:pool))
  end

  app.post "/pools/all" do
    if url_params('delete')
      drain_zset('pools') do |pools|
        pools.each do |pid|
          Joblin::Batching::Pool.from_pid(pid).cleanup_redis
        end
      end
    end

    redirect "#{root_path}pools"
  end

  app.post "/pools/:pid" do
    @pid = route_param(:pid)
    @pool = Joblin::Batching::Pool.from_pid(@pid)

    if url_params('delete')
      @pool.cleanup_redis
    end

    redirect_with_query("#{root_path}pools")
  end
end