Commit 7b06dbe4cdec37ff1349ccf01bf8cb3295971007
1 parent
2edb71f2
Exists in
master
and in
19 other branches
Added support for connection_pool
Showing
6 changed files
with
29 additions
and
17 deletions
Show diff stats
CHANGELOG.md
Gemfile
lib/searchkick.rb
@@ -145,6 +145,18 @@ module Searchkick | @@ -145,6 +145,18 @@ module Searchkick | ||
145 | end | 145 | end |
146 | end | 146 | end |
147 | 147 | ||
148 | + def self.with_redis | ||
149 | + if redis | ||
150 | + if redis.respond_to?(:with) | ||
151 | + redis.with do |r| | ||
152 | + yield r | ||
153 | + end | ||
154 | + else | ||
155 | + yield redis | ||
156 | + end | ||
157 | + end | ||
158 | + end | ||
159 | + | ||
148 | # private | 160 | # private |
149 | def self.load_records(records, ids) | 161 | def self.load_records(records, ids) |
150 | records = | 162 | records = |
lib/searchkick/index.rb
@@ -258,7 +258,7 @@ module Searchkick | @@ -258,7 +258,7 @@ module Searchkick | ||
258 | 258 | ||
259 | if batch | 259 | if batch |
260 | import_or_update scope.to_a, method_name, async | 260 | import_or_update scope.to_a, method_name, async |
261 | - redis.srem(batches_key, batch_id) if batch_id && redis | 261 | + Searchkick.with_redis { |r| r.srem(batches_key, batch_id) } if batch_id |
262 | elsif full && async | 262 | elsif full && async |
263 | full_reindex_async(scope) | 263 | full_reindex_async(scope) |
264 | elsif scope.respond_to?(:find_in_batches) | 264 | elsif scope.respond_to?(:find_in_batches) |
@@ -283,7 +283,7 @@ module Searchkick | @@ -283,7 +283,7 @@ module Searchkick | ||
283 | end | 283 | end |
284 | 284 | ||
285 | def batches_left | 285 | def batches_left |
286 | - redis.scard(batches_key) if redis | 286 | + Searchkick.with_redis { |r| r.scard(batches_key) } |
287 | end | 287 | end |
288 | 288 | ||
289 | # other | 289 | # other |
@@ -459,7 +459,7 @@ module Searchkick | @@ -459,7 +459,7 @@ module Searchkick | ||
459 | index_name: name, | 459 | index_name: name, |
460 | batch_id: batch_id | 460 | batch_id: batch_id |
461 | }.merge(options)) | 461 | }.merge(options)) |
462 | - redis.sadd(batches_key, batch_id) if redis | 462 | + Searchkick.with_redis { |r| r.sadd(batches_key, batch_id) } |
463 | end | 463 | end |
464 | 464 | ||
465 | def batch_size | 465 | def batch_size |
@@ -492,10 +492,6 @@ module Searchkick | @@ -492,10 +492,6 @@ module Searchkick | ||
492 | Searchkick.indexer.queue(records.map { |r| {update: record_data(r).merge(data: {doc: search_data(r, method_name)})} }) | 492 | Searchkick.indexer.queue(records.map { |r| {update: record_data(r).merge(data: {doc: search_data(r, method_name)})} }) |
493 | end | 493 | end |
494 | 494 | ||
495 | - def redis | ||
496 | - Searchkick.redis | ||
497 | - end | ||
498 | - | ||
499 | def batches_key | 495 | def batches_key |
500 | "searchkick:reindex:#{name}:batches" | 496 | "searchkick:reindex:#{name}:batches" |
501 | end | 497 | end |
lib/searchkick/reindex_queue.rb
@@ -5,36 +5,32 @@ module Searchkick | @@ -5,36 +5,32 @@ module Searchkick | ||
5 | def initialize(name) | 5 | def initialize(name) |
6 | @name = name | 6 | @name = name |
7 | 7 | ||
8 | - raise Searchkick::Error, "Searchkick.redis not set" unless redis | 8 | + raise Searchkick::Error, "Searchkick.redis not set" unless Searchkick.redis |
9 | end | 9 | end |
10 | 10 | ||
11 | def push(record_id) | 11 | def push(record_id) |
12 | - redis.lpush(redis_key, record_id) | 12 | + Searchkick.with_redis { |r| r.lpush(redis_key, record_id) } |
13 | end | 13 | end |
14 | 14 | ||
15 | # TODO use reliable queuing | 15 | # TODO use reliable queuing |
16 | def reserve(limit: 1000) | 16 | def reserve(limit: 1000) |
17 | record_ids = Set.new | 17 | record_ids = Set.new |
18 | - while record_ids.size < limit && record_id = redis.rpop(redis_key) | 18 | + while record_ids.size < limit && record_id = Searchkick.with_redis { |r| r.rpop(redis_key) } |
19 | record_ids << record_id | 19 | record_ids << record_id |
20 | end | 20 | end |
21 | record_ids.to_a | 21 | record_ids.to_a |
22 | end | 22 | end |
23 | 23 | ||
24 | def clear | 24 | def clear |
25 | - redis.del(redis_key) | 25 | + Searchkick.with_redis { |r| r.del(redis_key) } |
26 | end | 26 | end |
27 | 27 | ||
28 | def length | 28 | def length |
29 | - redis.llen(redis_key) | 29 | + Searchkick.with_redis { |r| r.llen(redis_key) } |
30 | end | 30 | end |
31 | 31 | ||
32 | private | 32 | private |
33 | 33 | ||
34 | - def redis | ||
35 | - Searchkick.redis | ||
36 | - end | ||
37 | - | ||
38 | def redis_key | 34 | def redis_key |
39 | "searchkick:reindex_queue:#{name}" | 35 | "searchkick:reindex_queue:#{name}" |
40 | end | 36 | end |
test/test_helper.rb
@@ -14,7 +14,13 @@ File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") | @@ -14,7 +14,13 @@ File.delete("elasticsearch.log") if File.exist?("elasticsearch.log") | ||
14 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") | 14 | Searchkick.client.transport.logger = Logger.new("elasticsearch.log") |
15 | Searchkick.search_timeout = 5 | 15 | Searchkick.search_timeout = 5 |
16 | 16 | ||
17 | -Searchkick.redis = Redis.new if defined?(Redis) | 17 | +if defined?(Redis) |
18 | + if defined?(ConnectionPool) | ||
19 | + Searchkick.redis = ConnectionPool.new { Redis.new } | ||
20 | + else | ||
21 | + Searchkick.redis = Redis.new | ||
22 | + end | ||
23 | +end | ||
18 | 24 | ||
19 | puts "Running against Elasticsearch #{Searchkick.server_version}" | 25 | puts "Running against Elasticsearch #{Searchkick.server_version}" |
20 | 26 |