From f5ec18a437a9ef71678e8d1c46e793c2b86688d4 Mon Sep 17 00:00:00 2001 From: Henrique Bontempo Date: Tue, 12 Nov 2024 18:19:10 -0300 Subject: [PATCH] feat(:force_batch_callback): Adds new belt that allow a Batch callback to be executed --- lib/sidekiq/belt/pro/files.rb | 2 + lib/sidekiq/belt/pro/force_batch_callback.rb | 58 ++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 lib/sidekiq/belt/pro/force_batch_callback.rb diff --git a/lib/sidekiq/belt/pro/files.rb b/lib/sidekiq/belt/pro/files.rb index 5abb890..c3764b7 100644 --- a/lib/sidekiq/belt/pro/files.rb +++ b/lib/sidekiq/belt/pro/files.rb @@ -3,6 +3,7 @@ require "sidekiq" require_relative "failed_batch_remove" +require_relative 'force_batch_callback' module Sidekiq module Belt @@ -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 diff --git a/lib/sidekiq/belt/pro/force_batch_callback.rb b/lib/sidekiq/belt/pro/force_batch_callback.rb new file mode 100644 index 0000000..aef440d --- /dev/null +++ b/lib/sidekiq/belt/pro/force_batch_callback.rb @@ -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[0].upcase + action_chars[1..].join + <<~ERB +
+ <%= csrf_tag %> + +
+ ERB + end + + def self.registered(app) + app.replace_content("/batches/:bid") do |content| + content.sub!(/(<\/tbody>)/) do |match| + <<-HTML + + <%= t("Force Action") %> + +
+ #{action_button('success')} + #{action_button('complete')} + #{action_button('death')} +
+ + + #{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