Skip to content

Commit

Permalink
Adde button to remove failed batch (#4)
Browse files Browse the repository at this point in the history
* Added button to remove failed batch

* Fix dependency

* Added feature to README

* Fix rubocop warning
  • Loading branch information
dannnylo authored Oct 28, 2023
1 parent f2a5a88 commit e00f374
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/sidekiq/belt/community/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/sidekiq/belt/ent/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions lib/sidekiq/belt/pro/failed_batch_remove.rb
Original file line number Diff line number Diff line change
@@ -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
<form action="<%= root_path %>batches/<%= bid %>/remove" method="post">
<%= csrf_tag %>
<input class="btn btn-danger" type="submit" name="remove" value="<%= t('Remove') %>"
data-confirm="Do you want to remove batch <%= bid %>? <%= t('AreYouSure') %>" />
</form>
ERB

def self.registered(app)
app.replace_content("/batches") do |content|
content.gsub!("</th>\n <%", "</th><th><%= t('Delete') %></th>\n <%")

content.gsub!(
"</td>\n </tr>\n <% end %>",
"</td>\n<td>#{REMOVE_BUTTON}</td>\n </tr>\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
10 changes: 6 additions & 4 deletions lib/sidekiq/belt/pro/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/belt/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Sidekiq
module Belt
VERSION = "0.2.0"
VERSION = "0.3.0"
end
end
6 changes: 1 addition & 5 deletions lib/sidekiq/web_action_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/sidekiq/web_router_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sidekiq-belt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion spec/sidekiq/belt/ent/periodic_pause_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
# frozen_string_literal: false

require "sidekiq"
require "sidekiq/web"
Expand All @@ -11,6 +11,7 @@
include Sidekiq::Worker
end
end

let(:loop_class) do
Class.new do
attr_accessor :lid, :klass, :options
Expand Down
3 changes: 2 additions & 1 deletion spec/sidekiq/web_action_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions spec/sidekiq/web_router_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "simplecov"

SimpleCov.start :test_frameworks do
enable_coverage :branch
# enable_coverage :branch

# minimum_coverage line: 100, branch: 85
end
Expand Down

0 comments on commit e00f374

Please sign in to comment.