Skip to content

Commit 0c68a93

Browse files
authored
Merge pull request gzigzigzeo#54 from mrexox/feature/support-sidekiq-upto-7
Support sidekiq upto 7
2 parents 19cc8a8 + 7dfa158 commit 0c68a93

File tree

13 files changed

+133
-53
lines changed

13 files changed

+133
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ spec/reports
1616
test/tmp
1717
test/version_tmp
1818
tmp
19+
.lefthook-local.yml

Appraisals

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ end
2121
appraise 'sidekiq-6.5' do
2222
gem 'sidekiq', '~> 6.5.0'
2323
end
24+
25+
appraise 'sidekiq-7.0' do
26+
gem 'sidekiq', '~> 7.0.0'
27+
end
28+
29+
appraise 'sidekiq-master' do
30+
gem 'sidekiq', github: 'mperham/sidekiq'
31+
end

bin/console

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "sidekiq/grouping"
6+
7+
require "pry"
8+
Pry.start

gemfiles/sidekiq_7.0.gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "sidekiq", "~> 7.0.0"
6+
7+
gemspec path: "../"

lefthook.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
# See: github.com/evilmartians/lefthook
55

66
pre-commit:
7+
parallel: true
78
commands:
9+
appraisal:
10+
glob: "{Appraisals,*.gemfile}"
11+
run: echo {staged_files} > /dev/null; bundle exec appraisal install && git add gemfiles/*.gemfile
812
rubocop:
9-
glob: "*.rb"
13+
glob: "{*.rb,*.gemspec,Gemfile,Rakefile}"
1014
run: bundle exec rubocop -A {staged_files} && git add {staged_files}
1115

1216
pre-push:
1317
commands:
1418
rspec:
15-
run: bundle exec appraisal rspec
19+
glob: "*.rb"
20+
run: echo {push_files} > /dev/null; bundle exec appraisal rspec

lib/sidekiq/grouping.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require "active_support/core_ext/string"
55
require "active_support/configurable"
66
require "active_support/core_ext/numeric/time"
7+
require "sidekiq"
78
require "sidekiq/grouping/version"
89
require "concurrent"
910

lib/sidekiq/grouping/config.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ module Config
66
include ActiveSupport::Configurable
77

88
def self.options
9-
if Sidekiq.respond_to?(:[])
10-
Sidekiq[:grouping] || Sidekiq["grouping"] || {}
11-
else
9+
if Sidekiq.respond_to?(:[]) # Sidekiq 6.x
10+
Sidekiq[:grouping] || {}
11+
elsif Sidekiq.respond_to?(:options) # Sidekiq <= 5.x
1212
Sidekiq.options[:grouping] || Sidekiq.options["grouping"] || {}
13+
else # Sidekiq 7.x
14+
Sidekiq.default_configuration[:grouping] || {}
1315
end
1416
end
1517

lib/sidekiq/grouping/redis.rb

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# frozen_string_literal: true
22

3+
require_relative "./redis_dispatcher"
4+
35
module Sidekiq
46
module Grouping
57
class Redis
8+
include RedisDispatcher
9+
610
PLUCK_SCRIPT = <<-SCRIPT
711
local pluck_values = redis.call('lpop', KEYS[1], ARGV[1]) or {}
812
if #pluck_values > 0 then
@@ -15,61 +19,75 @@ def push_msg(name, msg, remember_unique: false)
1519
redis do |conn|
1620
conn.multi do |pipeline|
1721
sadd = pipeline.respond_to?(:sadd?) ? :sadd? : :sadd
18-
pipeline.public_send(sadd, ns("batches"), name)
19-
pipeline.rpush(ns(name), msg)
22+
redis_connection_call(pipeline, sadd, ns("batches"), name)
23+
redis_connection_call(pipeline, :rpush, ns(name), msg)
24+
2025
if remember_unique
21-
pipeline.public_send(
22-
sadd,
23-
unique_messages_key(name),
24-
msg
26+
redis_connection_call(
27+
pipeline, sadd, unique_messages_key(name), msg
2528
)
2629
end
2730
end
2831
end
2932
end
3033

3134
def enqueued?(name, msg)
32-
redis do |conn|
33-
conn.sismember(unique_messages_key(name), msg)
34-
end
35+
member = redis_call(:sismember, unique_messages_key(name), msg)
36+
return member if member.is_a?(TrueClass) || member.is_a?(FalseClass)
37+
38+
member != 0
3539
end
3640

3741
def batch_size(name)
38-
redis { |conn| conn.llen(ns(name)) }
42+
redis_call(:llen, ns(name))
3943
end
4044

4145
def batches
42-
redis { |conn| conn.smembers(ns("batches")) }
46+
redis_call(:smembers, ns("batches"))
4347
end
4448

4549
def pluck(name, limit)
46-
keys = [ns(name), unique_messages_key(name)]
47-
args = [limit]
48-
redis { |conn| conn.eval PLUCK_SCRIPT, keys, args }
50+
if new_redis_client?
51+
redis_call(
52+
:eval,
53+
PLUCK_SCRIPT,
54+
2,
55+
ns(name),
56+
unique_messages_key(name),
57+
limit
58+
)
59+
else
60+
keys = [ns(name), unique_messages_key(name)]
61+
args = [limit]
62+
redis_call(:eval, PLUCK_SCRIPT, keys, args)
63+
end
4964
end
5065

5166
def get_last_execution_time(name)
52-
redis { |conn| conn.get(ns("last_execution_time:#{name}")) }
67+
redis_call(:get, ns("last_execution_time:#{name}"))
5368
end
5469

5570
def set_last_execution_time(name, time)
56-
redis do |conn|
57-
conn.set(ns("last_execution_time:#{name}"), time.to_json)
58-
end
71+
redis_call(
72+
:set, ns("last_execution_time:#{name}"), time.to_json
73+
)
5974
end
6075

6176
def lock(name)
62-
redis do |conn|
63-
id = ns("lock:#{name}")
64-
conn.set(id, true, nx: true, ex: Sidekiq::Grouping::Config.lock_ttl)
65-
end
77+
redis_call(
78+
:set,
79+
ns("lock:#{name}"),
80+
"true",
81+
nx: true,
82+
ex: Sidekiq::Grouping::Config.lock_ttl
83+
)
6684
end
6785

6886
def delete(name)
6987
redis do |conn|
70-
conn.del(ns("last_execution_time:#{name}"))
71-
conn.del(ns(name))
72-
conn.srem(ns("batches"), name)
88+
redis_connection_call(conn, :del, ns("last_execution_time:#{name}"))
89+
redis_connection_call(conn, :del, ns(name))
90+
redis_connection_call(conn, :srem, ns("batches"), name)
7391
end
7492
end
7593

@@ -82,10 +100,6 @@ def unique_messages_key(name)
82100
def ns(key = nil)
83101
"batching:#{key}"
84102
end
85-
86-
def redis(&block)
87-
Sidekiq.redis(&block)
88-
end
89103
end
90104
end
91105
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module Sidekiq
4+
module Grouping
5+
module RedisDispatcher
6+
def redis_call(command, *args, **kwargs)
7+
redis do |connection|
8+
redis_connection_call(connection, command, *args, **kwargs)
9+
end
10+
end
11+
12+
def redis_connection_call(connection, command, *args, **kwargs)
13+
if new_redis_client? # redis-client
14+
connection.call(command.to_s.upcase, *args, **kwargs)
15+
else # redis
16+
connection.public_send(command, *args, **kwargs)
17+
end
18+
end
19+
20+
def new_redis_client?
21+
Sidekiq::VERSION[0].to_i >= 7
22+
end
23+
24+
def redis(&block)
25+
Sidekiq.redis(&block)
26+
end
27+
end
28+
end
29+
end

lib/sidekiq/grouping/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Sidekiq
44
module Grouping
5-
VERSION = "1.2.0"
5+
VERSION = "1.3.0"
66
end
77
end

0 commit comments

Comments
 (0)