redis_failover-rails 0.1.0

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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +45 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +21 -0
  5. data/README.md +100 -0
  6. data/Rakefile +22 -0
  7. data/config/initializers/passenger.rb +12 -0
  8. data/config/redis.yml +33 -0
  9. data/lib/active_support/cache/redis_cache_store.rb +167 -0
  10. data/lib/redis_factory.rb +113 -0
  11. data/lib/redis_failover-rails/version.rb +3 -0
  12. data/redis_failover-rails.gemspec +38 -0
  13. data/test/dummy/README.rdoc +28 -0
  14. data/test/dummy/Rakefile +6 -0
  15. data/test/dummy/app/assets/images/.keep +0 -0
  16. data/test/dummy/app/assets/javascripts/application.js +13 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  18. data/test/dummy/app/controllers/application_controller.rb +5 -0
  19. data/test/dummy/app/controllers/concerns/.keep +0 -0
  20. data/test/dummy/app/helpers/application_helper.rb +2 -0
  21. data/test/dummy/app/mailers/.keep +0 -0
  22. data/test/dummy/app/models/.keep +0 -0
  23. data/test/dummy/app/models/concerns/.keep +0 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/test/dummy/bin/bundle +3 -0
  26. data/test/dummy/bin/rails +4 -0
  27. data/test/dummy/bin/rake +4 -0
  28. data/test/dummy/config.ru +4 -0
  29. data/test/dummy/config/application.rb +26 -0
  30. data/test/dummy/config/boot.rb +5 -0
  31. data/test/dummy/config/database.yml +25 -0
  32. data/test/dummy/config/environment.rb +5 -0
  33. data/test/dummy/config/environments/development.rb +37 -0
  34. data/test/dummy/config/environments/production.rb +83 -0
  35. data/test/dummy/config/environments/test.rb +45 -0
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  38. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/test/dummy/config/initializers/inflections.rb +16 -0
  40. data/test/dummy/config/initializers/mime_types.rb +4 -0
  41. data/test/dummy/config/initializers/session_store.rb +3 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/en.yml +23 -0
  44. data/test/dummy/config/routes.rb +56 -0
  45. data/test/dummy/config/secrets.yml +22 -0
  46. data/test/dummy/lib/assets/.keep +0 -0
  47. data/test/dummy/log/.keep +0 -0
  48. data/test/dummy/public/404.html +67 -0
  49. data/test/dummy/public/422.html +67 -0
  50. data/test/dummy/public/500.html +66 -0
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/test_helper.rb +62 -0
  53. data/test/unit/active_support/cache/redis_cache_store_test.rb +178 -0
  54. data/test/unit/redis_factory_test.rb +111 -0
  55. metadata +301 -0
@@ -0,0 +1,4 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Add new mime types for use in respond_to blocks:
4
+ # Mime::Type.register "text/richtext", :rtf
@@ -0,0 +1,3 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ Rails.application.config.session_store :cookie_store, key: '_dummy_session'
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json] if respond_to?(:wrap_parameters)
9
+ end
10
+
11
+ # To enable root element in JSON for ActiveRecord objects.
12
+ # ActiveSupport.on_load(:active_record) do
13
+ # self.include_root_in_json = true
14
+ # end
@@ -0,0 +1,23 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # To learn more, please read the Rails Internationalization guide
20
+ # available at http://guides.rubyonrails.org/i18n.html.
21
+
22
+ en:
23
+ hello: "Hello world"
@@ -0,0 +1,56 @@
1
+ Rails.application.routes.draw do
2
+ # The priority is based upon order of creation: first created -> highest priority.
3
+ # See how all your routes lay out with "rake routes".
4
+
5
+ # You can have the root of your site routed with "root"
6
+ # root 'welcome#index'
7
+
8
+ # Example of regular route:
9
+ # get 'products/:id' => 'catalog#view'
10
+
11
+ # Example of named route that can be invoked with purchase_url(id: product.id)
12
+ # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
13
+
14
+ # Example resource route (maps HTTP verbs to controller actions automatically):
15
+ # resources :products
16
+
17
+ # Example resource route with options:
18
+ # resources :products do
19
+ # member do
20
+ # get 'short'
21
+ # post 'toggle'
22
+ # end
23
+ #
24
+ # collection do
25
+ # get 'sold'
26
+ # end
27
+ # end
28
+
29
+ # Example resource route with sub-resources:
30
+ # resources :products do
31
+ # resources :comments, :sales
32
+ # resource :seller
33
+ # end
34
+
35
+ # Example resource route with more complex sub-resources:
36
+ # resources :products do
37
+ # resources :comments
38
+ # resources :sales do
39
+ # get 'recent', on: :collection
40
+ # end
41
+ # end
42
+
43
+ # Example resource route with concerns:
44
+ # concern :toggleable do
45
+ # post 'toggle'
46
+ # end
47
+ # resources :posts, concerns: :toggleable
48
+ # resources :photos, concerns: :toggleable
49
+
50
+ # Example resource route within a namespace:
51
+ # namespace :admin do
52
+ # # Directs /admin/products/* to Admin::ProductsController
53
+ # # (app/controllers/admin/products_controller.rb)
54
+ # resources :products
55
+ # end
56
+ end
@@ -0,0 +1,22 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key is used for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+
6
+ # Make sure the secret is at least 30 characters and all random,
7
+ # no regular words or you'll be exposed to dictionary attacks.
8
+ # You can use `rake secret` to generate a secure secret key.
9
+
10
+ # Make sure the secrets in this file are kept private
11
+ # if you're sharing your code publicly.
12
+
13
+ development:
14
+ secret_key_base: 83670d224a4568af4fdc6ba2c84a75b97ba66644afbe8023efefdb921361617e006a4ace33dd97ea3ef6b64de05756ae08d763f0533fa2da35493cdcebe9ed9e
15
+
16
+ test:
17
+ secret_key_base: 9521c98e439c051a085910d88d1c2d162abf3ade78e1999f058497a97e1b5562b65077f9869207a3b269d126d2a7c9d307cbd2069b05e13d0ac595fd7945e63a
18
+
19
+ # Do not keep production secrets in the repository,
20
+ # instead read values from the environment.
21
+ production:
22
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
File without changes
File without changes
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,62 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'rails'
5
+ require 'action_controller/metal/live'
6
+
7
+ require 'test/unit'
8
+ # require 'mocha/setup'
9
+ require 'mocha/test_unit'
10
+ require 'shoulda-context'
11
+ require 'ap'
12
+
13
+ require 'redis_factory'
14
+
15
+ ENV["RAILS_ENV"] = "test"
16
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
17
+ require "rails/test_help"
18
+
19
+ require 'active_support/cache/redis_cache_store'
20
+
21
+ Rails.backtrace_cleaner.remove_silencers!
22
+
23
+ # Load support files
24
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
25
+
26
+ # Load fixtures from the engine
27
+ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
28
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
29
+ end
30
+
31
+ def dump_redis
32
+ result = {}
33
+ Resque.redis.keys("*").each do |key|
34
+ type = Resque.redis.type(key)
35
+ result["#{key} (#{type})"] = case type
36
+ when 'string' then Resque.redis.get(key)
37
+ when 'list' then Resque.redis.lrange(key, 0, -1)
38
+ when 'zset' then Hash[*Resque.redis.zrange(key, 0, -1, :with_scores => true)]
39
+ when 'set' then Resque.redis.smembers(key)
40
+ when 'hash' then Resque.redis.hgetall(key)
41
+ else type
42
+ end
43
+ end
44
+ return result
45
+ end
46
+
47
+ def truncate_test_redis
48
+ if Rails.env.test? # extra paranoid with an operation like this
49
+ redis_dbs = RedisFactory.configuration.keys.each do |k|
50
+ RedisFactory.connect(k).flushdb()
51
+ end
52
+ end
53
+ end
54
+
55
+ class ActiveSupport::TestCase
56
+ setup do
57
+ # Make sure the tests start with a clean redis
58
+ truncate_test_redis
59
+ end
60
+
61
+ end
62
+
@@ -0,0 +1,178 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../test_helper.rb')
2
+ # require File.expand_path(File.dirname(__FILE__) + '/../../../../lib/active_support/cache/redis_cache_store.rb')
3
+ require 'rbconfig'
4
+
5
+ require 'rails/test_help'
6
+
7
+ class RedisCacheStoreTest < ActiveSupport::TestCase
8
+
9
+ def get_redis_server_pid
10
+ pid = `ps -e -o pid,command | grep [r]edis-server | grep -v sh`.split(" ")[0]
11
+ end
12
+
13
+ def kill_redis_server(sig="KILL")
14
+ pid = get_redis_server_pid
15
+ pid = pid.to_i
16
+ raise "Invalid pid" if pid == 0
17
+ if RbConfig::CONFIG['host_os'] =~ /linux/i
18
+ system("sudo /bin/kill -#{sig} #{pid}") || fail("Couldn't kill -#{sig} redis")
19
+ else
20
+ Process.kill(sig, pid)
21
+ end
22
+ end
23
+
24
+ def start_redis_server
25
+ if RbConfig::CONFIG['host_os'] =~ /linux/i
26
+ system("sudo /usr/sbin/service redis-server start") || fail("Couldn't start redis")
27
+ else
28
+ ENV['PATH'] += ":/usr/local/bin" unless ENV['PATH'] =~ /\/usr\/local\/bin/
29
+ spawn "nohup redis-server", :chdir => "/tmp/", [:out, :err] => ["redis.log", "w"]
30
+ end
31
+ end
32
+
33
+ context "integrated into rails cache" do
34
+
35
+ should "be using redis cache store" do
36
+ assert_equal ActiveSupport::Cache::RedisCacheStore, Rails.cache.class
37
+ assert Rails.cache.kind_of?(ActiveSupport::Cache::Store)
38
+ end
39
+
40
+ should "perform read/write correctly" do
41
+ assert_nil Rails.cache.read("foo")
42
+ Rails.cache.write("foo", "bar")
43
+ assert_equal "bar", Rails.cache.read("foo")
44
+ end
45
+
46
+ should "handle basic types in read/write correctly" do
47
+ assert_nil Rails.cache.read("foo")
48
+ Rails.cache.write("foo", "bar")
49
+ assert_equal "bar", Rails.cache.read("foo")
50
+
51
+ Rails.cache.write("foo", 1)
52
+ assert_equal 1, Rails.cache.read("foo")
53
+
54
+ Rails.cache.write("foo", ['x'])
55
+ assert_equal ['x'], Rails.cache.read("foo")
56
+
57
+ Rails.cache.write("foo", {'x' => 2})
58
+ assert_equal({'x' => 2}, Rails.cache.read("foo"))
59
+ end
60
+
61
+ should "perform increment correctly" do
62
+ assert_nil Rails.cache.read("foo")
63
+ assert_equal 1, Rails.cache.increment("foo")
64
+ assert_equal "1", Rails.cache.read("foo")
65
+ assert_equal 4, Rails.cache.increment("foo", 3)
66
+ assert_equal "4", Rails.cache.read("foo")
67
+ end
68
+
69
+ should "perform decrement correctly" do
70
+ assert_nil Rails.cache.read("foo")
71
+ assert_equal -1, Rails.cache.decrement("foo")
72
+ assert_equal "-1", Rails.cache.read("foo")
73
+ assert_equal -4, Rails.cache.decrement("foo", 3)
74
+ assert_equal "-4", Rails.cache.read("foo")
75
+ end
76
+
77
+ should "fail to increment/decrement on value set through write" do
78
+ # implement :raw support in ActiveSupport::Cache::RedisCacheStore if this is desired
79
+ assert_nil Rails.cache.read("foo")
80
+ Rails.cache.write("foo", 5)
81
+ assert_nil Rails.cache.increment("foo")
82
+ assert_nil Rails.cache.decrement("foo")
83
+ end
84
+
85
+ should "perform fetch correctly" do
86
+ assert_nil Rails.cache.fetch("foo")
87
+ assert_equal "bar", Rails.cache.fetch("foo") { "bar" }
88
+ assert_equal "bar", Rails.cache.read("foo")
89
+ assert_equal "bar", Rails.cache.fetch("foo")
90
+ end
91
+
92
+ end
93
+
94
+ context "behave in a failsafe way" do
95
+
96
+ context "when server is down" do
97
+
98
+ setup do
99
+ conf = RedisFactory.configuration[:cache]
100
+ @cache = ActiveSupport::Cache::RedisCacheStore.new(conf.merge(:port => 9999, :timeout => 0.1))
101
+ end
102
+
103
+ should "still perform fetch correctly" do
104
+ assert_nil @cache.fetch("foo")
105
+ assert "bar", @cache.fetch("foo") { "bar" }
106
+ end
107
+
108
+ should "return nil on read" do
109
+ assert_nil @cache.read("foo")
110
+ end
111
+
112
+ should "return false on write" do
113
+ assert ! @cache.write("foo", "bar")
114
+ end
115
+
116
+ should "return false on delete" do
117
+ assert ! @cache.delete("foo")
118
+ end
119
+
120
+ end
121
+
122
+ context "when server is up" do
123
+
124
+ setup do
125
+ conf = RedisFactory.configuration[:cache]
126
+ @cache = ActiveSupport::Cache::RedisCacheStore.new(conf.merge(:timeout => 0.1))
127
+ end
128
+
129
+
130
+ should "handle restarted server" do
131
+ skip
132
+ assert_equal nil, Rails.cache.read("foo")
133
+ Rails.cache.write("foo", "bar")
134
+ assert_equal "bar", Rails.cache.read("foo")
135
+
136
+ Rails.cache.instance_eval{@data}.save
137
+ kill_redis_server
138
+ while get_redis_server_pid != nil; Rails.logger.info("Waiting for redis to stop"); sleep 0.1; end
139
+
140
+ assert_equal nil, Rails.cache.read("foo")
141
+ assert_equal false, Rails.cache.write("foo", "bar")
142
+ assert_equal nil, Rails.cache.read("foo")
143
+
144
+ start_redis_server
145
+ while get_redis_server_pid.nil?; Rails.logger.info("Waiting for redis to start"); sleep 0.1; end
146
+
147
+ assert_equal "bar", Rails.cache.read("foo")
148
+ Rails.cache.write("foo", "baz")
149
+ assert_equal "baz", Rails.cache.read("foo")
150
+ end
151
+
152
+ should "handle stopped server" do
153
+ skip
154
+ begin
155
+ kill_redis_server("STOP")
156
+ sleep 0.1
157
+
158
+ assert_equal nil, Rails.cache.read("foo")
159
+ Rails.cache.write("foo", "bar")
160
+ assert_equal nil, Rails.cache.read("foo")
161
+
162
+ kill_redis_server("CONT")
163
+ sleep 0.1
164
+
165
+ assert_equal nil, Rails.cache.read("foo")
166
+ Rails.cache.write("foo", "bar")
167
+ assert_equal "bar", Rails.cache.read("foo")
168
+ ensure
169
+ kill_redis_server("CONT")
170
+ sleep 0.1
171
+ end
172
+ end
173
+
174
+ end
175
+
176
+ end
177
+
178
+ end