begin require "sidekiq/web" rescue LoadError # client-only usage end require_relative "web/helpers" module CanvasSync::JobBatches::Sidekiq module Web def self.registered(app) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize app.helpers do include Web::Helpers end app.get "/batches" do @count = (params['count'] || 25).to_i @current_page, @total_size, @batches = page('batches', params['page'], @count) @batches = @batches.map {|b, score| CanvasSync::JobBatches::Batch.new(b) } erb(get_template(:batches)) end app.get "/batches/:bid" do @bid = params[:bid] @batch = CanvasSync::JobBatches::Batch.new(@bid) @count = (params['count'] || 25).to_i @current_batches_page, @total_batches_size, @sub_batches = page("BID-#{@batch.bid}-bids", params['batch_page'], @count) @sub_batches = @sub_batches.map {|b, score| CanvasSync::JobBatches::Batch.new(b) } @current_jobs_page, @total_jobs_size, @jobs = page("BID-#{@batch.bid}-jids", params['job_page'], @count) @jobs = @jobs.map {|jid, score| jid } erb(get_template(:batch)) end app.post "/batches/all" do if params['delete'] drain_zset('batches') do |batches| batches.each do |bid| CanvasSync::JobBatches::Batch.cleanup_redis(bid) end end end redirect "#{root_path}batches" end app.post "/batches/:bid" do @bid = params[:bid] @batch = CanvasSync::JobBatches::Batch.new(@bid) if params['delete'] CanvasSync::JobBatches::Batch.delete_prematurely!(@bid) end redirect_with_query("#{root_path}batches") end # =============== POOLS =============== # app.get "/pools" do @count = (params['count'] || 25).to_i @current_page, @total_size, @pools = page('pools', params['page'], @count) @pools = @pools.map {|b, score| CanvasSync::JobBatches::Pool.new(b) } erb(get_template(:pools)) end app.get "/pools/:pid" do @pid = params[:pid] @pool = CanvasSync::JobBatches::Pool.new(@pid) @count = (params['count'] || 25).to_i @current_jobs_page, @total_jobs_size, @jobs = page("POOLID-#{@pool.pid}-jobs", 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 params['delete'] drain_zset('pools') do |pools| pools.each do |pid| CanvasSync::JobBatches::Pool.from_pid(pid).cleanup_redis end end end redirect "#{root_path}pools" end app.post "/pools/:pid" do @pid = params[:pid] @pool = CanvasSync::JobBatches::Pool.from_pid(@pid) if params['delete'] @pool.cleanup_redis end redirect_with_query("#{root_path}pools") end end end end if defined?(::Sidekiq::Web) ::Sidekiq::Web.register CanvasSync::JobBatches::Sidekiq::Web ::Sidekiq::Web.tabs["Batches"] = "batches" ::Sidekiq::Web.tabs["Pools"] = "pools" ::Sidekiq::Web.settings.locales << File.join(File.dirname(__FILE__), "locales") end