mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
minor cleanup of RedisConnection
This commit is contained in:
parent
a1f78816a7
commit
3fdb26b4db
4 changed files with 16 additions and 33 deletions
|
@ -18,8 +18,8 @@ module Sidekiq
|
||||||
FOREVER = 2_000_000_000
|
FOREVER = 2_000_000_000
|
||||||
|
|
||||||
def run
|
def run
|
||||||
::Sidekiq::Client.redis = Sidekiq::RedisConnection.create(@options[:server], @options[:namespace])
|
Sidekiq::Client.redis = RedisConnection.create(:url => @options[:server], :namespace => @options[:namespace], :use_pool => true)
|
||||||
manager_redis = Sidekiq::RedisConnection.create(@options[:server], @options[:namespace], false)
|
manager_redis = RedisConnection.create(:url => @options[:server], :namespace => @options[:namespace])
|
||||||
manager = Sidekiq::Manager.new(manager_redis, @options)
|
manager = Sidekiq::Manager.new(manager_redis, @options)
|
||||||
begin
|
begin
|
||||||
log 'Starting processing, hit Ctrl-C to stop'
|
log 'Starting processing, hit Ctrl-C to stop'
|
||||||
|
|
|
@ -22,9 +22,8 @@ module Sidekiq
|
||||||
@redis = redis
|
@redis = redis
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.ignore_duplicate_jobs=(value)
|
def self.ignore_duplicate_jobs=(ignore)
|
||||||
@ignore_duplicate_jobs = value
|
if ignore
|
||||||
if @ignore_duplicate_jobs
|
|
||||||
middleware.register do
|
middleware.register do
|
||||||
use Middleware::Client::UniqueJobs, Client.redis
|
use Middleware::Client::UniqueJobs, Client.redis
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,37 +3,21 @@ require 'redis/namespace'
|
||||||
|
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
class RedisConnection
|
class RedisConnection
|
||||||
def self.create(url = nil, namespace = nil, pool = true)
|
def self.create(options={})
|
||||||
@namespace = namespace ? namespace : nil
|
url = options[:url] || ENV['REDISTOGO_URL'] || 'redis://localhost:6379/0'
|
||||||
@url = url ? url : nil
|
client = build_client(url, options[:namespace])
|
||||||
|
return ConnectionPool.new { client } if options[:use_pool]
|
||||||
if pool
|
client
|
||||||
ConnectionPool.new { connect }
|
|
||||||
else
|
|
||||||
connect
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.connect
|
def self.build_client(url, namespace)
|
||||||
|
client = Redis.connect(:url => url)
|
||||||
if namespace
|
if namespace
|
||||||
Redis::Namespace.new(namespace, :redis => redis_connection)
|
Redis::Namespace.new(namespace, :redis => client)
|
||||||
else
|
else
|
||||||
redis_connection
|
client
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
private_class_method :build_client
|
||||||
def self.namespace
|
|
||||||
@namespace
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.url
|
|
||||||
@url || ENV['REDISTOGO_URL'] || 'redis://localhost:6379/0'
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def self.redis_connection
|
|
||||||
Redis.connect(:url => url)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ require 'connection_pool'
|
||||||
class TestManager < MiniTest::Unit::TestCase
|
class TestManager < MiniTest::Unit::TestCase
|
||||||
describe 'with redis' do
|
describe 'with redis' do
|
||||||
before do
|
before do
|
||||||
Sidekiq::Client.redis = @redis = Sidekiq::RedisConnection.create('redis://localhost/sidekiq_test', nil, false)
|
Sidekiq::Client.redis = @redis = Sidekiq::RedisConnection.create(:url => 'redis://localhost/sidekiq_test')
|
||||||
@redis.flushdb
|
@redis.flushdb
|
||||||
$processed = 0
|
$processed = 0
|
||||||
end
|
end
|
||||||
|
@ -25,7 +25,7 @@ class TestManager < MiniTest::Unit::TestCase
|
||||||
Sidekiq::Client.push(:foo, 'class' => IntegrationWorker, 'args' => [1, 2])
|
Sidekiq::Client.push(:foo, 'class' => IntegrationWorker, 'args' => [1, 2])
|
||||||
|
|
||||||
q = TimedQueue.new
|
q = TimedQueue.new
|
||||||
redis = Sidekiq::RedisConnection.create('redis://localhost/sidekiq_test', nil, false)
|
redis = Sidekiq::RedisConnection.create(:url => 'redis://localhost/sidekiq_test')
|
||||||
mgr = Sidekiq::Manager.new(redis, :queues => [:foo])
|
mgr = Sidekiq::Manager.new(redis, :queues => [:foo])
|
||||||
mgr.when_done do |_|
|
mgr.when_done do |_|
|
||||||
q << 'done' if $processed == 2
|
q << 'done' if $processed == 2
|
||||||
|
|
Loading…
Add table
Reference in a new issue