sunspot-rails-failover 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sunspot-rails-failover.rb +1 -34
- data/lib/sunspot/rails/failover.rb +34 -0
- data/lib/{sunspot-rails-failover → sunspot/rails/failover}/exception_handler_adapter.rb +0 -0
- data/lib/{sunspot-rails-failover → sunspot/rails/failover}/version.rb +1 -1
- data/lib/{sunspot-rails-failover → sunspot/session_proxy}/master_slave_with_failover_session_proxy.rb +2 -3
- data/spec/sunspot/rails/failover/exception_handler_adapter_spec.rb +41 -0
- data/spec/sunspot/rails/failover_spec.rb +57 -0
- data/spec/sunspot/session_proxy/master_slave_with_failover_session_proxy_spec.rb +63 -0
- data/sunspot-rails-failover.gemspec +1 -1
- metadata +11 -10
- data/spec/sunspot-rails-failover/exception_handler_adapter_spec.rb +0 -34
- data/spec/sunspot-rails-failover/master_slave_with_failover_session_proxy_spec.rb +0 -59
- data/spec/sunspot-rails-failover_spec.rb +0 -53
@@ -1,34 +1 @@
|
|
1
|
-
require 'sunspot'
|
2
|
-
require 'sunspot-rails-failover/master_slave_with_failover_session_proxy'
|
3
|
-
require 'sunspot-rails-failover/exception_handler_adapter'
|
4
|
-
|
5
|
-
module Sunspot
|
6
|
-
module Rails
|
7
|
-
module Failover
|
8
|
-
class << self
|
9
|
-
attr_accessor :exception_handler
|
10
|
-
|
11
|
-
def setup
|
12
|
-
Sunspot.session = if Sunspot::Rails.configuration.has_master?
|
13
|
-
Sunspot::SessionProxy::MasterSlaveWithFailoverSessionProxy.new(
|
14
|
-
SessionProxy::ThreadLocalSessionProxy.new(master_config),
|
15
|
-
SessionProxy::ThreadLocalSessionProxy.new(slave_config)
|
16
|
-
)
|
17
|
-
else
|
18
|
-
Sunspot::SessionProxy::ThreadLocalSessionProxy.new(slave_config)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def slave_config
|
25
|
-
Sunspot::Rails.send :slave_config, Sunspot::Rails.configuration
|
26
|
-
end
|
27
|
-
|
28
|
-
def master_config
|
29
|
-
Sunspot::Rails.send :master_config, Sunspot::Rails.configuration
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
1
|
+
require 'sunspot/rails/failover'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'sunspot'
|
2
|
+
require 'sunspot/session_proxy/master_slave_with_failover_session_proxy'
|
3
|
+
require 'sunspot/rails/failover/exception_handler_adapter'
|
4
|
+
|
5
|
+
module Sunspot
|
6
|
+
module Rails
|
7
|
+
module Failover
|
8
|
+
class << self
|
9
|
+
attr_accessor :exception_handler
|
10
|
+
|
11
|
+
def setup
|
12
|
+
Sunspot.session = if Rails.configuration.has_master?
|
13
|
+
SessionProxy::MasterSlaveWithFailoverSessionProxy.new(
|
14
|
+
SessionProxy::ThreadLocalSessionProxy.new(master_config),
|
15
|
+
SessionProxy::ThreadLocalSessionProxy.new(slave_config)
|
16
|
+
)
|
17
|
+
else
|
18
|
+
SessionProxy::ThreadLocalSessionProxy.new(slave_config)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def slave_config
|
25
|
+
Rails.send :slave_config, Rails.configuration
|
26
|
+
end
|
27
|
+
|
28
|
+
def master_config
|
29
|
+
Rails.send :master_config, Rails.configuration
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
File without changes
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module Sunspot
|
2
2
|
module SessionProxy
|
3
|
-
class MasterSlaveWithFailoverSessionProxy < MasterSlaveSessionProxy
|
4
|
-
|
3
|
+
class MasterSlaveWithFailoverSessionProxy < MasterSlaveSessionProxy
|
5
4
|
attr_accessor :exception
|
6
5
|
|
7
6
|
def commit
|
@@ -21,7 +20,7 @@ module Sunspot
|
|
21
20
|
def with_exception_handling
|
22
21
|
yield
|
23
22
|
rescue Exception => exception
|
24
|
-
|
23
|
+
Rails::Failover::ExceptionHandlerAdapter.handle(exception)
|
25
24
|
self.exception = exception
|
26
25
|
false
|
27
26
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'hoptoad_notifier'
|
4
|
+
|
5
|
+
shared_examples_for 'a hoptoad handler' do
|
6
|
+
it 'uses the api' do
|
7
|
+
HoptoadNotifier.should_receive(:notify).with(exception)
|
8
|
+
described_class.handle(exception)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Sunspot
|
13
|
+
module Rails
|
14
|
+
module Failover
|
15
|
+
describe ExceptionHandlerAdapter do
|
16
|
+
describe '.handle' do
|
17
|
+
let(:exception) { Exception.new }
|
18
|
+
|
19
|
+
context 'with exception_handler not set (default)' do
|
20
|
+
it_should_behave_like 'a hoptoad handler'
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with exception_handler set to :hoptoad' do
|
24
|
+
before { Failover.exception_handler = :hoptoad }
|
25
|
+
it_should_behave_like 'a hoptoad handler'
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with exception_handler set to a custom class' do
|
29
|
+
before { Failover.exception_handler = MyExceptionHandler }
|
30
|
+
|
31
|
+
it 'passes the exception to #handle' do
|
32
|
+
MyExceptionHandler.should_receive(:handle).with(exception)
|
33
|
+
described_class.handle(exception)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sunspot
|
4
|
+
module Rails
|
5
|
+
describe Failover do
|
6
|
+
describe '.setup' do
|
7
|
+
let(:configuration) { double('configuration') }
|
8
|
+
let(:slave_config) { double('slave_config') }
|
9
|
+
let(:master_config) { double('master_config') }
|
10
|
+
|
11
|
+
before do
|
12
|
+
Rails.stub(:configuration).and_return(configuration)
|
13
|
+
Rails.stub(:slave_config).and_return(slave_config)
|
14
|
+
Rails.stub(:master_config).and_return(master_config)
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with a master configuration' do
|
18
|
+
before do
|
19
|
+
configuration.should_receive(:has_master?).and_return(true)
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:proxy) { double('master_slave_failover_proxy') }
|
23
|
+
let(:master_session) { double('master_session') }
|
24
|
+
let(:slave_session) { double('slave_session') }
|
25
|
+
|
26
|
+
it 'sets the session to master/slave with failover support' do
|
27
|
+
SessionProxy::ThreadLocalSessionProxy.should_receive(:new).with(master_config).and_return(master_session)
|
28
|
+
SessionProxy::ThreadLocalSessionProxy.should_receive(:new).with(slave_config).and_return(slave_session)
|
29
|
+
|
30
|
+
SessionProxy::MasterSlaveWithFailoverSessionProxy.should_receive(:new).with(
|
31
|
+
master_session, slave_session
|
32
|
+
).and_return(proxy)
|
33
|
+
|
34
|
+
described_class.setup
|
35
|
+
Sunspot.session.should eq(proxy)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with no master configuration' do
|
40
|
+
before do
|
41
|
+
configuration.should_receive(:has_master?).and_return(false)
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:proxy) { double('thread_local_proxy') }
|
45
|
+
|
46
|
+
it 'sets the session to the default proxy' do
|
47
|
+
SessionProxy::ThreadLocalSessionProxy.should_receive(:new).with(slave_config).and_return(proxy)
|
48
|
+
|
49
|
+
described_class.setup
|
50
|
+
Sunspot.session.should eq(proxy)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sunspot
|
4
|
+
module SessionProxy
|
5
|
+
describe MasterSlaveWithFailoverSessionProxy do
|
6
|
+
let(:master) { Session.new }
|
7
|
+
let(:slave) { Session.new }
|
8
|
+
let(:proxy) { SessionProxy::MasterSlaveWithFailoverSessionProxy.new(master, slave) }
|
9
|
+
|
10
|
+
let(:exception_handler_adapter) { Rails::Failover::ExceptionHandlerAdapter }
|
11
|
+
|
12
|
+
describe '#commit' do
|
13
|
+
context 'with no error' do
|
14
|
+
it 'calls on the master session' do
|
15
|
+
master.should_receive(:commit)
|
16
|
+
proxy.commit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with an error' do
|
21
|
+
it 'passes the exception to the exception handler adapter' do
|
22
|
+
exception = Exception.new
|
23
|
+
exception_handler_adapter.should_receive(:handle).with(exception)
|
24
|
+
master.should_receive(:commit).and_raise(exception)
|
25
|
+
proxy.commit
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#search' do
|
31
|
+
context 'with no error' do
|
32
|
+
it 'calls on the slave session' do
|
33
|
+
slave.should_receive(:search).and_return(true)
|
34
|
+
master.should_not_receive(:search)
|
35
|
+
proxy.search
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with an error on the slave session' do
|
40
|
+
it 'calls on the master session' do
|
41
|
+
exception = Exception.new
|
42
|
+
slave.should_receive(:search).and_raise(exception)
|
43
|
+
exception_handler_adapter.should_receive(:handle).with(exception)
|
44
|
+
master.should_receive(:search).and_return(true)
|
45
|
+
proxy.search
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with an error on the slave and master sessions' do
|
50
|
+
it 'raises the error with no handling' do
|
51
|
+
exception = Exception.new
|
52
|
+
slave.should_receive(:search).and_raise(exception)
|
53
|
+
master.should_receive(:search).and_raise(exception)
|
54
|
+
exception_handler_adapter.should_receive(:handle).with(exception).twice
|
55
|
+
proxy.should_receive(:raise).with(exception)
|
56
|
+
proxy.search
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: sunspot-rails-failover
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Justin Ko
|
@@ -62,13 +62,14 @@ files:
|
|
62
62
|
- README.md
|
63
63
|
- Rakefile
|
64
64
|
- lib/sunspot-rails-failover.rb
|
65
|
-
- lib/sunspot
|
66
|
-
- lib/sunspot
|
67
|
-
- lib/sunspot
|
65
|
+
- lib/sunspot/rails/failover.rb
|
66
|
+
- lib/sunspot/rails/failover/exception_handler_adapter.rb
|
67
|
+
- lib/sunspot/rails/failover/version.rb
|
68
|
+
- lib/sunspot/session_proxy/master_slave_with_failover_session_proxy.rb
|
68
69
|
- spec/spec_helper.rb
|
69
|
-
- spec/sunspot
|
70
|
-
- spec/sunspot
|
71
|
-
- spec/sunspot
|
70
|
+
- spec/sunspot/rails/failover/exception_handler_adapter_spec.rb
|
71
|
+
- spec/sunspot/rails/failover_spec.rb
|
72
|
+
- spec/sunspot/session_proxy/master_slave_with_failover_session_proxy_spec.rb
|
72
73
|
- spec/support/my_exception_handler.rb
|
73
74
|
- sunspot-rails-failover.gemspec
|
74
75
|
has_rdoc: true
|
@@ -101,7 +102,7 @@ specification_version: 3
|
|
101
102
|
summary: If the Sunspot slave session goes down, reads *and* writes are called upon the master session
|
102
103
|
test_files:
|
103
104
|
- spec/spec_helper.rb
|
104
|
-
- spec/sunspot
|
105
|
-
- spec/sunspot
|
106
|
-
- spec/sunspot
|
105
|
+
- spec/sunspot/rails/failover/exception_handler_adapter_spec.rb
|
106
|
+
- spec/sunspot/rails/failover_spec.rb
|
107
|
+
- spec/sunspot/session_proxy/master_slave_with_failover_session_proxy_spec.rb
|
107
108
|
- spec/support/my_exception_handler.rb
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
require 'hoptoad_notifier'
|
4
|
-
|
5
|
-
shared_examples_for 'a hoptoad handler' do
|
6
|
-
it 'uses the api' do
|
7
|
-
HoptoadNotifier.should_receive(:notify).with(exception)
|
8
|
-
described_class.handle(exception)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe Sunspot::Rails::Failover::ExceptionHandlerAdapter do
|
13
|
-
describe '.handle' do
|
14
|
-
let(:exception) { Exception.new }
|
15
|
-
|
16
|
-
context 'with exception_handler not set (default)' do
|
17
|
-
it_should_behave_like 'a hoptoad handler'
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with exception_handler set to :hoptoad' do
|
21
|
-
before { Sunspot::Rails::Failover.exception_handler = :hoptoad }
|
22
|
-
it_should_behave_like 'a hoptoad handler'
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'with exception_handler set to a custom class' do
|
26
|
-
before { Sunspot::Rails::Failover.exception_handler = MyExceptionHandler }
|
27
|
-
|
28
|
-
it 'passes the exception to #handle' do
|
29
|
-
MyExceptionHandler.should_receive(:handle).with(exception)
|
30
|
-
described_class.handle(exception)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Sunspot::SessionProxy::MasterSlaveWithFailoverSessionProxy do
|
4
|
-
let(:master) { Sunspot::Session.new }
|
5
|
-
let(:slave) { Sunspot::Session.new }
|
6
|
-
let(:proxy) { Sunspot::SessionProxy::MasterSlaveWithFailoverSessionProxy.new(master, slave) }
|
7
|
-
|
8
|
-
let(:exception_handler_adapter) { Sunspot::Rails::Failover::ExceptionHandlerAdapter }
|
9
|
-
|
10
|
-
describe '#commit' do
|
11
|
-
context 'with no error' do
|
12
|
-
it 'calls on the master session' do
|
13
|
-
master.should_receive(:commit)
|
14
|
-
proxy.commit
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'with an error' do
|
19
|
-
it 'passes the exception to the exception handler adapter' do
|
20
|
-
exception = Exception.new
|
21
|
-
exception_handler_adapter.should_receive(:handle).with(exception)
|
22
|
-
master.should_receive(:commit).and_raise(exception)
|
23
|
-
proxy.commit
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe '#search' do
|
29
|
-
context 'with no error' do
|
30
|
-
it 'calls on the slave session' do
|
31
|
-
slave.should_receive(:search).and_return(true)
|
32
|
-
master.should_not_receive(:search)
|
33
|
-
proxy.search
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'with an error on the slave session' do
|
38
|
-
it 'calls on the master session' do
|
39
|
-
exception = Exception.new
|
40
|
-
slave.should_receive(:search).and_raise(exception)
|
41
|
-
exception_handler_adapter.should_receive(:handle).with(exception)
|
42
|
-
master.should_receive(:search).and_return(true)
|
43
|
-
proxy.search
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
context 'with an error on the slave and master sessions' do
|
48
|
-
it 'raises the error with no handling' do
|
49
|
-
exception = Exception.new
|
50
|
-
slave.should_receive(:search).and_raise(exception)
|
51
|
-
master.should_receive(:search).and_raise(exception)
|
52
|
-
exception_handler_adapter.should_receive(:handle).with(exception).twice
|
53
|
-
proxy.should_receive(:raise).with(exception)
|
54
|
-
proxy.search
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Sunspot::Rails::Failover do
|
4
|
-
describe '.setup' do
|
5
|
-
let(:configuration) { double('configuration') }
|
6
|
-
let(:slave_config) { double('slave_config') }
|
7
|
-
let(:master_config) { double('master_config') }
|
8
|
-
|
9
|
-
before do
|
10
|
-
Sunspot::Rails.stub(:configuration).and_return(configuration)
|
11
|
-
Sunspot::Rails.stub(:slave_config).and_return(slave_config)
|
12
|
-
Sunspot::Rails.stub(:master_config).and_return(master_config)
|
13
|
-
end
|
14
|
-
|
15
|
-
context 'with a master configuration' do
|
16
|
-
before do
|
17
|
-
configuration.should_receive(:has_master?).and_return(true)
|
18
|
-
end
|
19
|
-
|
20
|
-
let(:proxy) { double('master_slave_failover_proxy') }
|
21
|
-
let(:master_session) { double('master_session') }
|
22
|
-
let(:slave_session) { double('slave_session') }
|
23
|
-
|
24
|
-
it 'sets the session to master/slave with failover support' do
|
25
|
-
Sunspot::SessionProxy::ThreadLocalSessionProxy.should_receive(:new).with(master_config).and_return(master_session)
|
26
|
-
Sunspot::SessionProxy::ThreadLocalSessionProxy.should_receive(:new).with(slave_config).and_return(slave_session)
|
27
|
-
|
28
|
-
Sunspot::SessionProxy::MasterSlaveWithFailoverSessionProxy.should_receive(:new).with(
|
29
|
-
master_session, slave_session
|
30
|
-
).and_return(proxy)
|
31
|
-
|
32
|
-
described_class.setup
|
33
|
-
Sunspot.session.should eq(proxy)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'with no master configuration' do
|
38
|
-
before do
|
39
|
-
configuration.should_receive(:has_master?).and_return(false)
|
40
|
-
end
|
41
|
-
|
42
|
-
let(:proxy) { double('thread_local_proxy') }
|
43
|
-
|
44
|
-
it 'sets the session to the default proxy' do
|
45
|
-
Sunspot::SessionProxy::ThreadLocalSessionProxy.should_receive(:new).with(slave_config).and_return(proxy)
|
46
|
-
|
47
|
-
described_class.setup
|
48
|
-
Sunspot.session.should eq(proxy)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
53
|
-
end
|