Skip to content
8 changes: 5 additions & 3 deletions lib/sidekiq/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "active_support"
require "active_support/core_ext/string"
require "active_support/configurable"
require "active_support/ordered_options"
require "active_support/core_ext/numeric/time"
require "sidekiq"
require "sidekiq/grouping/version"
Expand Down Expand Up @@ -55,6 +55,8 @@ def start!
config.client_middleware do |chain|
chain.add Sidekiq::Grouping::Middleware
end
end

Sidekiq::Grouping.start! if Sidekiq.server?
config.on(:startup) do
Sidekiq::Grouping.start!
end
end
61 changes: 33 additions & 28 deletions lib/sidekiq/grouping/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,43 @@
module Sidekiq
module Grouping
module Config
include ActiveSupport::Configurable

def self.options
if Sidekiq.respond_to?(:[]) # Sidekiq 6.x
Sidekiq[:grouping] || {}
elsif Sidekiq.respond_to?(:options) # Sidekiq <= 5.x
Sidekiq.options[:grouping] || Sidekiq.options["grouping"] || {}
else # Sidekiq 7.x
Sidekiq.default_configuration[:grouping] || {}
class << self
%i[lock_ttl max_batch_size poll_interval tests_env].each do |method_name|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

define_method(method_name) do
config[method_name]
end
define_method(:"#{method_name}=") do |value|
config[method_name] = value
end
end
end

# Queue size overflow check polling interval
config_accessor :poll_interval do
options[:poll_interval] || 3
end

# Maximum batch size
config_accessor :max_batch_size do
options[:max_batch_size] || 1000
end
private

# Batch queue flush lock timeout
config_accessor :lock_ttl do
options[:lock_ttl] || 1
end
def config
@config ||= ActiveSupport::InheritableOptions.new(
lock_ttl: options[:lock_ttl] || 1,
max_batch_size: options[:max_batch_size] || 1_000,
poll_interval: options[:poll_interval] || 3,
tests_env: options[:tests_env] || (
defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.env.test?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]

)
)
end

# Option to override how Sidekiq::Grouping know about tests env
config_accessor :tests_env do
options[:tests_env] || (
defined?(::Rails) && Rails.respond_to?(:env) && Rails.env.test?
)
def options
@options ||=
begin
if Sidekiq.respond_to?(:[]) # Sidekiq 6.x
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationWidth: Use 2 (not 3) spaces for indentation.

Sidekiq[:grouping] || {}
elsif Sidekiq.respond_to?(:options) # Sidekiq <= 5.x
Sidekiq.options[:grouping] || Sidekiq.options["grouping"] || {}
elsif Sidekiq.server? # Sidekiq >= 7.x
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/ExtraSpacing: Unnecessary spacing detected.

Sidekiq::CLI.instance.config[:grouping]
else
Sidekiq.default_configuration[:grouping] || {}
end
end
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/sidekiq/grouping/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
<td><%= batch.last_execution_time || "&ndash;"%></td>
<td><%= batch.next_execution_time || "&ndash;"%></td>
<td>
<form action="<%= "#{root_path}grouping/#{batch.name}/delete" %>" method="post">
<form action="<%= "#{root_path}grouping/#{Base64.urlsafe_encode64(batch.name)}" %>" method="post">
<%= csrf_tag %>
<input type="hidden" name="_method" value="delete" />
<input class="btn btn-danger btn-xs" type="submit" name="delete" value="Delete" data-confirm="Are you sure you want to delete this batch?" />
</form>
</td>
Expand Down
21 changes: 17 additions & 4 deletions lib/sidekiq/grouping/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def self.registered(app)
locals: { view_path: VIEWS }
end

app.post "/grouping/:name/delete" do
app.delete "/grouping/:name64" do
name = Base64.decode64(params["name64"])
worker_class, queue =
Sidekiq::Grouping::Batch.extract_worker_klass_and_queue(
params["name"]
name
)
batch = Sidekiq::Grouping::Batch.new(worker_class, queue)
batch.delete
Expand All @@ -28,5 +29,17 @@ def self.registered(app)
end
end

Sidekiq::Web.register(Sidekiq::Grouping::Web)
Sidekiq::Web.tabs["Grouping"] = "grouping"
args = {
name: "grouping",
tab: ["Grouping"],
index: ["grouping"],
root_dir: File.dirname(__FILE__)
}

if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("8.0.0")
Sidekiq::Web.configure do |cfg|
cfg.register(Sidekiq::Grouping::Web, **args)
end
else
Sidekiq::Web.register(Sidekiq::Grouping::Web, **args)
end