Skip to content

Commit

Permalink
feat: force batch callback belt (#17)
Browse files Browse the repository at this point in the history
* fix(:failed_batch_remove): spacing on gsub!

* feat(:force_batch_callback): Adds new belt that allow a Batch callback to be executed
  • Loading branch information
hbontempo-cw authored Nov 19, 2024
1 parent 4555720 commit fa6964d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sidekiq/belt/pro/failed_batch_remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.registered(app)

content.gsub!(
"</td>\n </tr>\n <% end %>",
"</td>\n<td>#{REMOVE_BUTTON}</td>\n </tr>\n <% end %>"
"</td>\n<td>#{REMOVE_BUTTON}</td>\n </tr>\n <% end %>"
)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/sidekiq/belt/pro/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "sidekiq"

require_relative "failed_batch_remove"
require_relative 'force_batch_callback'

module Sidekiq
module Belt
Expand All @@ -14,6 +15,7 @@ def self.use!(options = [:all])
all = options.include?(:all)

Sidekiq::Belt::Pro::FailedBatchRemove.use! if all || options.include?(:failed_batch_remove)
Sidekiq::Belt::Pro::ForceBatchCallback.use! if all || options.include?(:force_batch_callback)

true
end
Expand Down
58 changes: 58 additions & 0 deletions lib/sidekiq/belt/pro/force_batch_callback.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require "sidekiq/web/helpers"

module Sidekiq
module Belt
module Pro
module ForceBatchCallback
module SidekiqForceBatchCallback
def self.action_button(action)
action_name = "force_#{action}"
action_chars = action.chars
action_button = action_chars.capitalize
<<~ERB
<form action="<%= root_path %>batches/<%= @batch.bid %>/force_callback/#{action}" method="post">
<%= csrf_tag %>
<input class="btn btn-danger" type="submit" name="#{action_name}" value="<%= t('#{action_button}') %>"
data-confirm="Do you want to force the #{action} callback for batch <%= @batch.bid %>? <%= t('AreYouSure') %>" />
</form>
ERB
end

def self.registered(app)
app.replace_content("/batches/:bid") do |content|
content.sub!(/(<\/tbody>)/) do |match|
<<-HTML
<tr>
<th><%= t("Force Action") %></th>
<td>
<div style="display: flex;">
#{action_button('success')}
#{action_button('complete')}
#{action_button('death')}
</div>
</td>
</tr>
#{match}
HTML
end
end

app.post("/batches/:bid/force_callback/:action") do
Sidekiq::Batch::Callback.perform_inline(params[:action], params[:bid])

return redirect "#{root_path}batches"
end
end
end

def self.use!
require("sidekiq/web")

Sidekiq::Web.register(Sidekiq::Belt::Pro::ForceBatchCallback::SidekiqForceBatchCallback)
end
end
end
end
end

0 comments on commit fa6964d

Please sign in to comment.