Skip to content

Commit 3fd6452

Browse files
committed
Release 0.4.0
1 parent 3a2275a commit 3fd6452

File tree

5 files changed

+42
-21
lines changed

5 files changed

+42
-21
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
bigrails-redis (0.3.0)
4+
bigrails-redis (0.4.0)
55
rails (>= 6)
66

77
GEM

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,16 @@ This library also allows you to verify connections on demand. If you want, perfo
9595
# Verify all connections:
9696
Rails.application.redis.verify!
9797

98-
# Verify a single connection:
99-
Rails.application.redis.verify!(:foobar)
98+
# Verify specific connections:
99+
Rails.application.redis.verify!(:foobar, :sidekiq)
100+
```
101+
102+
### Disconnect Connections
103+
104+
You can disconnect all connections with a single call. This is useful for "before fork" hooks.
105+
106+
```ruby
107+
Rails.application.redis.disconnect
100108
```
101109

102110
## Development

lib/big_rails/redis/registry.rb

+26-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class Registry
66
class UnknownConnection < StandardError
77
end
88

9+
class VerificationError < StandardError
10+
end
11+
912
attr_accessor :builder
1013

1114
def initialize
@@ -36,11 +39,29 @@ def each(&block)
3639
configurations.keys.map { |name| self.for(name) }.each(&block)
3740
end
3841

39-
def verify!(name = nil)
40-
if name
41-
verify_connection(self.for(name))
42-
else
43-
each { |connection| verify_connection(connection) }
42+
def disconnect
43+
each do |connection|
44+
if connection.is_a?(::ConnectionPool)
45+
connection.reload { |conn| conn.quit }
46+
else
47+
connection.quit
48+
end
49+
end
50+
end
51+
52+
def verify!(*names)
53+
names.map! { |name| validate_name(name) }
54+
names = configurations.keys if names.empty?
55+
names.each do |name|
56+
self.for(name).with do |connection|
57+
next if connection.connected?
58+
59+
begin
60+
connection.quit
61+
rescue
62+
raise VerificationError, "verification for '#{name}' failed"
63+
end
64+
end
4465
end
4566

4667
true
@@ -66,14 +87,6 @@ def build_wrapped_connection(connection)
6687
end
6788
end
6889

69-
def verify_connection(connection)
70-
connection.with do |conn|
71-
connected = conn.connected?
72-
conn.ping
73-
conn.quit unless connected
74-
end
75-
end
76-
7790
def validate_name(name)
7891
name = name.to_s
7992
unless configurations.key?(name)

lib/big_rails/redis/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module BigRails
44
module Redis
5-
VERSION = "0.3.0"
5+
VERSION = "0.4.0"
66
end
77
end

spec/big_rails/redis/registry_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575
it "verifies specified connection" do
7676
conn = instance.for("default")
7777
conn2 = instance.for("pooled")
78-
expect(conn).to receive(:ping).and_call_original
78+
expect(conn).to receive(:quit).and_call_original
7979
conn2.with do |redis|
80-
expect(redis).to receive(:ping).and_call_original
80+
expect(redis).to receive(:quit).and_call_original
8181
end
8282

8383
instance.verify!("default")
@@ -89,9 +89,9 @@
8989
it "verifies all connections" do
9090
conn = instance.for("default")
9191
conn2 = instance.for("pooled")
92-
expect(conn).to receive(:ping).and_call_original
92+
expect(conn).to receive(:quit).and_call_original
9393
conn2.with do |redis|
94-
expect(redis).to receive(:ping).and_call_original
94+
expect(redis).to receive(:quit).and_call_original
9595
end
9696

9797
instance.verify!

0 commit comments

Comments
 (0)