From e00f37403d2d8b70300664729fddec39a529c020 Mon Sep 17 00:00:00 2001 From: Danilo Jeremias da Silva Date: Fri, 27 Oct 2023 21:52:59 -0300 Subject: [PATCH] Adde button to remove failed batch (#4) * Added button to remove failed batch * Fix dependency * Added feature to README * Fix rubocop warning --- CHANGELOG.md | 4 ++ Gemfile.lock | 6 +-- README.md | 7 +++- lib/sidekiq/belt/community/files.rb | 2 + lib/sidekiq/belt/ent/files.rb | 2 + lib/sidekiq/belt/pro/failed_batch_remove.rb | 44 ++++++++++++++++++++ lib/sidekiq/belt/pro/files.rb | 10 +++-- lib/sidekiq/belt/version.rb | 2 +- lib/sidekiq/web_action_helper.rb | 6 +-- lib/sidekiq/web_router_helper.rb | 5 ++- sidekiq-belt.gemspec | 2 +- spec/sidekiq/belt/ent/periodic_pause_spec.rb | 3 +- spec/sidekiq/web_action_helper_spec.rb | 3 +- spec/sidekiq/web_router_helper_spec.rb | 3 +- spec/spec_helper.rb | 2 +- 15 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 lib/sidekiq/belt/pro/failed_batch_remove.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index c3cfa69..3620b1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +## [0.3.0] - 2023-10-07 + +- Feature to add button to remove failed batches + ## [0.2.0] - 2023-10-07 - Feature to Pause/Unpause Periodic Jobs diff --git a/Gemfile.lock b/Gemfile.lock index aa6aeaa..fd72c1d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,8 @@ PATH remote: . specs: - sidekiq-belt (0.2.0) - sidekiq (> 7.0) + sidekiq-belt (0.3.0) + sidekiq (> 7.1.4) GEM remote: https://rubygems.org/ @@ -66,7 +66,7 @@ GEM rubocop-capybara (~> 2.17) rubocop-factory_bot (~> 2.22) ruby-progressbar (1.13.0) - sidekiq (7.1.2) + sidekiq (7.1.6) concurrent-ruby (< 2) connection_pool (>= 2.3.0) rack (>= 2.2.4) diff --git a/README.md b/README.md index f521c15..2cf7f48 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,12 @@ Sidekiq::Belt.use!([:periodic_pause]) ### Delete an Unfinished Batch (sidekiq-pro) -It will be implemented in upcoming versions. +This option adds a button to remove failed batches. + +To enable this feature, pass the `failed_batch_remove` option: +```ruby +Sidekiq::Belt.use!([:failed_batch_remove]) +``` ## Development diff --git a/lib/sidekiq/belt/community/files.rb b/lib/sidekiq/belt/community/files.rb index 9c78eb1..cb697d5 100644 --- a/lib/sidekiq/belt/community/files.rb +++ b/lib/sidekiq/belt/community/files.rb @@ -10,6 +10,8 @@ module Files def self.use!(_options = [:all]) # all = options.include?(:all) # Sidekiq::Belt::Pro::Feature.load! if all || options.include?(:feature) + + true end end end diff --git a/lib/sidekiq/belt/ent/files.rb b/lib/sidekiq/belt/ent/files.rb index 27f19b1..63ecee5 100644 --- a/lib/sidekiq/belt/ent/files.rb +++ b/lib/sidekiq/belt/ent/files.rb @@ -16,6 +16,8 @@ def self.use!(options = [:all]) Sidekiq::Belt::Ent::PeriodicPause.use! if all || options.include?(:periodic_pause) Sidekiq::Belt::Ent::PeriodicRun.use! if all || options.include?(:periodic_run) + + true end end end diff --git a/lib/sidekiq/belt/pro/failed_batch_remove.rb b/lib/sidekiq/belt/pro/failed_batch_remove.rb new file mode 100644 index 0000000..a5a3a4e --- /dev/null +++ b/lib/sidekiq/belt/pro/failed_batch_remove.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "sidekiq/web/helpers" + +module Sidekiq + module Belt + module Pro + module FailedBatchRemove + module SidekiqFailedBatchRemove + REMOVE_BUTTON = <<~ERB +
+ <%= csrf_tag %> + +
+ ERB + + def self.registered(app) + app.replace_content("/batches") do |content| + content.gsub!("\n <%", "<%= t('Delete') %>\n <%") + + content.gsub!( + "\n \n <% end %>", + "\n#{REMOVE_BUTTON}\n \n <% end %>" + ) + end + + app.post("/batches/:bid/remove") do + Sidekiq::Batch::Status.new(params[:bid]).delete + + return redirect "#{root_path}batches" + end + end + end + + def self.use! + require("sidekiq/web") + + Sidekiq::Web.register(Sidekiq::Belt::Pro::FailedBatchRemove::SidekiqFailedBatchRemove) + end + end + end + end +end diff --git a/lib/sidekiq/belt/pro/files.rb b/lib/sidekiq/belt/pro/files.rb index 99f7972..5abb890 100644 --- a/lib/sidekiq/belt/pro/files.rb +++ b/lib/sidekiq/belt/pro/files.rb @@ -2,18 +2,20 @@ require "sidekiq" -# require_relative "feature" +require_relative "failed_batch_remove" module Sidekiq module Belt module Pro module Files - def self.use!(_options = [:all]) + def self.use!(options = [:all]) return unless Sidekiq.pro? + all = options.include?(:all) + + Sidekiq::Belt::Pro::FailedBatchRemove.use! if all || options.include?(:failed_batch_remove) + true - # all = options.include?(:all) - # Sidekiq::Belt::Pro::Feature.load! if all || options.include?(:feature) end end end diff --git a/lib/sidekiq/belt/version.rb b/lib/sidekiq/belt/version.rb index ede9c71..a3e2213 100644 --- a/lib/sidekiq/belt/version.rb +++ b/lib/sidekiq/belt/version.rb @@ -2,6 +2,6 @@ module Sidekiq module Belt - VERSION = "0.2.0" + VERSION = "0.3.0" end end diff --git a/lib/sidekiq/web_action_helper.rb b/lib/sidekiq/web_action_helper.rb index 81a7daa..36cc65d 100644 --- a/lib/sidekiq/web_action_helper.rb +++ b/lib/sidekiq/web_action_helper.rb @@ -5,10 +5,6 @@ module Sidekiq module WebActionHelper - def self.blocks - @blocks ||= {} - end - def render(engine, content, options = {}) begin path_info = /"([^"]*)"/.match(block.source.to_s)[1] @@ -18,7 +14,7 @@ def render(engine, content, options = {}) path_info ||= ::Rack::Utils.unescape(env["PATH_INFO"]) - Sidekiq::WebActionHelper.blocks.fetch(path_info.to_s, []).each do |content_block| + Sidekiq::Config::DEFAULTS[:replace_views].fetch(path_info.to_s, []).each do |content_block| content_block.call(content) end diff --git a/lib/sidekiq/web_router_helper.rb b/lib/sidekiq/web_router_helper.rb index 27c3b1b..3a7113f 100644 --- a/lib/sidekiq/web_router_helper.rb +++ b/lib/sidekiq/web_router_helper.rb @@ -7,8 +7,9 @@ module Sidekiq module WebRouterHelper def replace_content(path, &block) - Sidekiq::WebActionHelper.blocks[path.to_s] ||= [] - Sidekiq::WebActionHelper.blocks[path.to_s] << block + Sidekiq::Config::DEFAULTS[:replace_views] ||= {} + Sidekiq::Config::DEFAULTS[:replace_views][path.to_s] ||= [] + Sidekiq::Config::DEFAULTS[:replace_views][path.to_s] << block end end diff --git a/sidekiq-belt.gemspec b/sidekiq-belt.gemspec index dc9597b..a926e5a 100644 --- a/sidekiq-belt.gemspec +++ b/sidekiq-belt.gemspec @@ -32,6 +32,6 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_dependency "sidekiq", "> 7.0" + spec.add_dependency "sidekiq", "> 7.1.4" spec.metadata["rubygems_mfa_required"] = "true" end diff --git a/spec/sidekiq/belt/ent/periodic_pause_spec.rb b/spec/sidekiq/belt/ent/periodic_pause_spec.rb index 466ce7b..52d5a3e 100644 --- a/spec/sidekiq/belt/ent/periodic_pause_spec.rb +++ b/spec/sidekiq/belt/ent/periodic_pause_spec.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true +# frozen_string_literal: false require "sidekiq" require "sidekiq/web" @@ -11,6 +11,7 @@ include Sidekiq::Worker end end + let(:loop_class) do Class.new do attr_accessor :lid, :klass, :options diff --git a/spec/sidekiq/web_action_helper_spec.rb b/spec/sidekiq/web_action_helper_spec.rb index c402fed..b83b378 100644 --- a/spec/sidekiq/web_action_helper_spec.rb +++ b/spec/sidekiq/web_action_helper_spec.rb @@ -15,7 +15,8 @@ def render(_engine, content, _options = {}) end before do - described_class.blocks["/"] = blocks + Sidekiq::Config::DEFAULTS[:replace_views] ||= {} + Sidekiq::Config::DEFAULTS[:replace_views]["/"] = blocks dummy_web_action.prepend(described_class) end diff --git a/spec/sidekiq/web_router_helper_spec.rb b/spec/sidekiq/web_router_helper_spec.rb index 818495a..c358943 100644 --- a/spec/sidekiq/web_router_helper_spec.rb +++ b/spec/sidekiq/web_router_helper_spec.rb @@ -21,8 +21,7 @@ content.gsub!("replaced", "replaced twice") end - expect(Sidekiq::WebActionHelper.blocks.size).to eq(1) - expect(Sidekiq::WebActionHelper.blocks["/"].size).to eq(2) + expect(Sidekiq::Config::DEFAULTS[:replace_views]["/"].size).to eq(2) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 56e974b..9c5cd0f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,7 @@ require "simplecov" SimpleCov.start :test_frameworks do - enable_coverage :branch + # enable_coverage :branch # minimum_coverage line: 100, branch: 85 end