diff --git a/Gemfile b/Gemfile index f95a3bb..ec89ac5 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,5 @@ gemspec group :test do gem "simplecov" gem "codeclimate-test-reporter", "~> 1.0.0" + gem "pry" end diff --git a/lib/sidekiq/batch.rb b/lib/sidekiq/batch.rb index fb2aba7..42375b2 100644 --- a/lib/sidekiq/batch.rb +++ b/lib/sidekiq/batch.rb @@ -63,7 +63,7 @@ def jobs Sidekiq.redis do |r| r.multi do |pipeline| - pipeline.hset(@bidkey, "created_at", @created_at) + pipeline.hset(@bidkey, "created_at", Time.now.to_s) pipeline.hset(@bidkey, "parent_bid", parent_bid.to_s) if parent_bid pipeline.expire(@bidkey, BID_EXPIRE_TTL) end @@ -96,7 +96,7 @@ def jobs pipeline.hincrby(@bidkey, "total", @ready_to_queue.size) pipeline.expire(@bidkey, BID_EXPIRE_TTL) - pipeline.sadd(@bidkey + "-jids", [@ready_to_queue]) + pipeline.sadd(@bidkey + "-jids", @ready_to_queue) pipeline.expire(@bidkey + "-jids", BID_EXPIRE_TTL) end end @@ -113,7 +113,7 @@ def increment_job_queue(jid) def invalidate_all Sidekiq.redis do |r| - r.setex("invalidated-bid-#{bid}", BID_EXPIRE_TTL, 1) + r.call("SETEX","invalidated-bid-#{bid}", BID_EXPIRE_TTL, 1) end end @@ -130,7 +130,7 @@ def parent end def valid?(batch = self) - valid = !Sidekiq.redis { |r| r.exists("invalidated-bid-#{batch.bid}") } + valid = !Sidekiq.redis { |r| r.exists("invalidated-bid-#{batch.bid}").to_i > 0 } batch.parent ? valid && valid?(batch.parent) : valid end @@ -139,7 +139,7 @@ def valid?(batch = self) def persist_bid_attr(attribute, value) Sidekiq.redis do |r| r.multi do |pipeline| - pipeline.hset(@bidkey, attribute, value) + pipeline.hset(@bidkey, attribute, value.to_s) pipeline.expire(@bidkey, BID_EXPIRE_TTL) end end @@ -208,8 +208,8 @@ def enqueue_callbacks(event, bid) callback_key = "#{batch_key}-callbacks-#{event_name}" already_processed, _, callbacks, queue, parent_bid, callback_batch = Sidekiq.redis do |r| r.multi do |pipeline| - pipeline.hget(batch_key, event_name) - pipeline.hset(batch_key, event_name, true) + pipeline.call('HGET', batch_key, event_name) + pipeline.hset(batch_key, event_name, 'true') pipeline.smembers(callback_key) pipeline.hget(batch_key, "callback_queue") pipeline.hget(batch_key, "parent_bid") diff --git a/lib/sidekiq/batch/middleware.rb b/lib/sidekiq/batch/middleware.rb index 04f7bd0..c218146 100644 --- a/lib/sidekiq/batch/middleware.rb +++ b/lib/sidekiq/batch/middleware.rb @@ -4,6 +4,8 @@ module Sidekiq class Batch module Middleware class ClientMiddleware + include ::Sidekiq::ClientMiddleware + def call(_worker, msg, _queue, _redis_pool = nil) if (batch = Thread.current[:batch]) batch.increment_job_queue(msg['jid']) if (msg[:bid] = batch.bid) @@ -13,6 +15,8 @@ def call(_worker, msg, _queue, _redis_pool = nil) end class ServerMiddleware + include ::Sidekiq::ServerMiddleware + def call(_worker, msg, _queue) if (bid = msg['bid']) begin diff --git a/sidekiq-batch.gemspec b/sidekiq-batch.gemspec index c5be436..80480f3 100644 --- a/sidekiq-batch.gemspec +++ b/sidekiq-batch.gemspec @@ -19,10 +19,10 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_dependency "sidekiq", ">= 3" + spec.add_dependency "sidekiq", ">= 7" spec.add_development_dependency "bundler", "~> 2.1" spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "rspec", "~> 3.0" - spec.add_development_dependency "fakeredis", "~> 0.8.0" + spec.add_development_dependency "redis-client", "~> 0.11" end diff --git a/spec/sidekiq/batch/middleware_spec.rb b/spec/sidekiq/batch/middleware_spec.rb index 776b198..6f70395 100644 --- a/spec/sidekiq/batch/middleware_spec.rb +++ b/spec/sidekiq/batch/middleware_spec.rb @@ -79,7 +79,7 @@ end describe Sidekiq::Batch::Middleware do - let(:config) { class_double(Sidekiq) } + let(:config) { instance_double(Sidekiq::Config) } let(:client_middleware) { double(Sidekiq::Middleware::Chain) } context 'client' do diff --git a/spec/sidekiq/batch/status_spec.rb b/spec/sidekiq/batch/status_spec.rb index 3b4a85a..16cd5be 100644 --- a/spec/sidekiq/batch/status_spec.rb +++ b/spec/sidekiq/batch/status_spec.rb @@ -1,10 +1,20 @@ require 'spec_helper' +class TestWorker + include Sidekiq::Worker + def perform + end +end + describe Sidekiq::Batch::Status do let(:bid) { 'BID' } let(:batch) { Sidekiq::Batch.new(bid) } subject { described_class.new(bid) } + before do + Sidekiq.redis do |r| r.flushdb end + end + describe '#join' do it 'raises info' do expect { subject.join }.to raise_error('Not supported') diff --git a/spec/sidekiq/batch_spec.rb b/spec/sidekiq/batch_spec.rb index 4d6afaa..84f8fbf 100644 --- a/spec/sidekiq/batch_spec.rb +++ b/spec/sidekiq/batch_spec.rb @@ -7,6 +7,10 @@ def perform end describe Sidekiq::Batch do + before do + Sidekiq.redis do |r| r.flushdb end + end + it 'has a version number' do expect(Sidekiq::Batch::VERSION).not_to be nil end @@ -170,7 +174,7 @@ def was_performed; end Sidekiq::Batch.process_failed_job(bid, 'failed-job-id') Sidekiq::Batch.process_failed_job(bid, failed_jid) failed = Sidekiq.redis { |r| r.smembers("BID-#{bid}-failed") } - expect(failed).to eq(['xxx', 'failed-job-id']) + expect(failed).to match_array(['xxx', 'failed-job-id']) end end end @@ -245,7 +249,7 @@ def was_performed; end it 'returns and does not enqueue callbacks' do batch = Sidekiq::Batch.new batch.on(event, SampleCallback) - Sidekiq.redis { |r| r.hset("BID-#{batch.bid}", event, true) } + Sidekiq.redis { |r| r.hset("BID-#{batch.bid}", event, 'true') } expect(Sidekiq::Client).not_to receive(:push) Sidekiq::Batch.enqueue_callbacks(event, batch.bid) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ca90a0d..004cbb8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,11 +2,9 @@ SimpleCov.start $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'fakeredis/rspec' require 'sidekiq/batch' redis_opts = { url: "redis://127.0.0.1:6379/1" } -redis_opts[:driver] = Redis::Connection::Memory if defined?(Redis::Connection::Memory) Sidekiq.configure_client do |config| config.redis = redis_opts