Skip to content

Commit

Permalink
feat(:force_batch_callback): Adds new belt that allow a Batch callbac…
Browse files Browse the repository at this point in the history
…k to be executed
  • Loading branch information
hbontempo-cw committed Nov 12, 2024
1 parent 5259326 commit f5ec18a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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[0].upcase + action_chars[1..].join
<<~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 f5ec18a

Please sign in to comment.