Last active
September 6, 2024 15:34
-
-
Save badosu/0d9bccc4928856f25ee1d95b31d224dd to your computer and use it in GitHub Desktop.
Sidekiq Batch Invalidation UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const appendInvalidationRow = row => { | |
$('tbody') | |
.first() | |
.append(`<tr><th>Validation</th><td>${row}</td></tr>`); | |
}; | |
const setupBatchInvalidation = (rootUrl, bid) => { | |
$.getJSON(`${rootUrl}api/batches/${bid}/invalidated`, data => { | |
if (data.invalidated) { | |
appendInvalidationRow('Invalid'); | |
} else { | |
const invalidateUrl = `${rootUrl}batches/${bid}/invalidate`; | |
const html = | |
`<form method="POST" action="${invalidateUrl}" style="display: inline-block">` + | |
`<input type="hidden" name="authenticity_token" value="${window.authenticityToken}">` + | |
`<input class="btn btn-danger" type="submit" value="Invalidate all jobs in batch">` + | |
`</form>`; | |
appendInvalidationRow(html); | |
} | |
}).fail(() => { | |
appendInvalidationRow('There was an error retrieving the status'); | |
}); | |
}; | |
const pathMatches = window.location.pathname.match(/(.*)batches\/([\w-]+)/); | |
if (pathMatches) { | |
$(() => setupBatchInvalidation(pathMatches[1], pathMatches[2])); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/sidekiq.rb | |
# ... until EOF | |
require 'sidekiq_batch_invalidation/web' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/sidekiq_batch_invalidation/web.rb | |
require 'sidekiq/web' | |
module Sidekiq | |
module BatchInvalidation | |
module Web | |
# rubocop:disable Metrics/MethodLength | |
def self.registered(app) | |
view_path = File.join(File.expand_path('..', __FILE__), 'views') | |
batch_invalidation_js = File.read(File.join(view_path, 'batch_invalidation.js')) | |
app.before('/batches/[\w-]+') do | |
add_to_head do | |
"<script type='text/javascript'>window.authenticityToken='#{session[:csrf]}'</script>"\ | |
"<script type='text/javascript' src='#{root_path}/batch_invalidation.js'></script>" | |
end | |
end | |
app.get '/batch_invalidation.js' do | |
[ | |
200, | |
{ 'Content-Type' => 'application/javascript' }, | |
[batch_invalidation_js] | |
] | |
end | |
app.post '/batches/:bid/invalidate' do | |
bid = params[:bid] | |
batch = Sidekiq::Batch.new(bid) | |
batch.invalidate_all | |
redirect "#{root_path}batches/#{bid}" | |
rescue Sidekiq::Batch::NoSuchBatch | |
redirect "#{root_path}batches" | |
end | |
app.get '/api/batches/:bid/invalidated' do | |
batch = Sidekiq::Batch::Status.new(params[:bid]) | |
json(invalidated: batch.invalidated?) | |
end | |
end | |
# rubocop:enable Metrics/MethodLength | |
end | |
end | |
end | |
Sidekiq::Web.register Sidekiq::BatchInvalidation::Web |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment