slowpoke 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af786e335277355221963f452414cadd17d53fec
4
- data.tar.gz: cfdce56d500d599f5d854bb68a44b86f03e6a4b1
3
+ metadata.gz: 536fd6527a9659642fa4aaad681b8eb617627db8
4
+ data.tar.gz: 969103107e61959e4eda5efdc975ae7d478b3b9c
5
5
  SHA512:
6
- metadata.gz: b93df7d6d424bc941c37a7451cc6c63783cb9a390b08941907f1e8187f31a3a1c328d6e017e661c1b94fabb333260d2c0c0dbc6225e44b4bba78125fdd6d64c9
7
- data.tar.gz: 7447ac2e09c6bd908b0ff9b81ef4ec28b0e73eb14a9ac201d94e85c7bb2dc39e85ac4860af73c358f52f7f5bfdabd4321a5c739108986a3ac683932979e74913
6
+ metadata.gz: 8aa007b642cc348e14613c0baedfd774373fbf2ae4b81ab4ae4b48e335d30d42dbc82644bb4475e34ae2e051d8fd5a9c5e70ad6b7b0c9cac6078911178f6f20b
7
+ data.tar.gz: 7c978167f493c0aef20ad9c3aaced4a0170480f2cb243da0257bf0c95569daf975bc39e0d64a73d752da7650432067a8316d4fff18405cced23bbb3b5dcdf55c
data/README.md CHANGED
@@ -34,12 +34,6 @@ or use:
34
34
  ENV["TIMEOUT"] = 10
35
35
  ```
36
36
 
37
- For ActiveRecord (PostgreSQL only), change the database timeout with:
38
-
39
- ```ruby
40
- Slowpoke.database_timeout = 5
41
- ```
42
-
43
37
  Subscribe to timeouts
44
38
 
45
39
  ```ruby
@@ -48,6 +42,14 @@ ActiveSupport::Notifications.subscribe "timeout.slowpoke" do |name, start, finis
48
42
  end
49
43
  ```
50
44
 
45
+ ### Database Timeouts
46
+
47
+ For ActiveRecord (PostgreSQL only), change the database timeout with:
48
+
49
+ ```ruby
50
+ Slowpoke.database_timeout = 5
51
+ ```
52
+
51
53
  ## Contributing
52
54
 
53
55
  Everyone is encouraged to help improve this project. Here are a few ways you can help:
@@ -0,0 +1,22 @@
1
+ module Slowpoke
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ around_filter :bubble_timeout
7
+ end
8
+
9
+ private
10
+
11
+ def bubble_timeout
12
+ yield
13
+ rescue => exception
14
+ if exception.respond_to?(:original_exception) and exception.original_exception.is_a?(Rack::Timeout::Error)
15
+ raise exception.original_exception
16
+ else
17
+ raise exception
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Slowpoke
2
+ module Postgres
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ alias_method_chain :configure_connection, :statement_timeout
7
+ end
8
+
9
+ def configure_connection_with_statement_timeout
10
+ configure_connection_without_statement_timeout
11
+ safely do
12
+ timeout = Slowpoke.database_timeout || Slowpoke.timeout
13
+ if ActiveRecord::Base.logger
14
+ ActiveRecord::Base.logger.silence do
15
+ execute("SET statement_timeout = #{timeout * 1000}")
16
+ end
17
+ else
18
+ execute("SET statement_timeout = #{timeout * 1000}")
19
+ end
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Slowpoke
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/slowpoke.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  require "slowpoke/version"
2
2
  require "rack-timeout"
3
3
  require "robustly"
4
+ require "slowpoke/controller"
5
+ require "slowpoke/postgres"
4
6
  require "slowpoke/railtie"
5
- require "action_view/template"
7
+ require "action_dispatch/middleware/exception_wrapper"
6
8
  require "action_controller/base"
7
9
  require "active_record/connection_adapters/postgresql_adapter"
8
- require "action_dispatch/middleware/exception_wrapper"
9
10
 
10
11
  module Slowpoke
12
+ ENV_KEY = "slowpoke.timed_out"
13
+
11
14
  class << self
12
15
  attr_accessor :database_timeout
13
16
  end
@@ -17,59 +20,36 @@ module Slowpoke
17
20
  end
18
21
 
19
22
  def self.timeout=(timeout)
23
+ timeout = timeout.to_i if timeout
20
24
  @timeout = Rack::Timeout.timeout = timeout
21
25
  end
22
26
  end
23
27
 
24
- # remove noisy logger
25
- Rack::Timeout.unregister_state_change_observer(:logger)
26
-
28
+ # custom error page
27
29
  ActionDispatch::ExceptionWrapper.rescue_responses["Rack::Timeout::RequestTimeoutError"] = :service_unavailable
28
30
 
29
- # hack to bubble timeout errors
30
- class ActionView::Template
31
-
32
- def handle_render_error_with_timeout(view, e)
33
- raise e if e.is_a?(Rack::Timeout::Error)
34
- handle_render_error_without_timeout(view, e)
35
- end
36
- alias_method_chain :handle_render_error, :timeout
37
-
38
- end
31
+ # remove noisy logger
32
+ Rack::Timeout.unregister_state_change_observer(:logger)
39
33
 
40
- # notifications
41
- class ActionController::Base
34
+ # process protection and notifications
35
+ Rack::Timeout.register_state_change_observer(:slowpoke) do |env|
36
+ case env[Rack::Timeout::ENV_INFO_KEY].state
37
+ when :timed_out
38
+ env[Slowpoke::ENV_KEY] = true
42
39
 
43
- def rescue_from_timeout(exception)
40
+ # TODO better payload
44
41
  ActiveSupport::Notifications.instrument("timeout.slowpoke", {})
45
-
42
+ when :completed
46
43
  # extremely important
47
44
  # protect the process with a restart
48
45
  # https://github.com/heroku/rack-timeout/issues/39
49
- Process.kill "QUIT", Process.pid
50
-
51
- raise exception
46
+ # can't do in timed_out state consistently
47
+ Process.kill("QUIT", Process.pid) if env[Slowpoke::ENV_KEY]
52
48
  end
53
- rescue_from Rack::Timeout::Error, with: :rescue_from_timeout
54
-
55
49
  end
56
50
 
57
- # database timeout
58
- class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
59
-
60
- def configure_connection_with_statement_timeout
61
- configure_connection_without_statement_timeout
62
- safely do
63
- timeout = Slowpoke.database_timeout || Slowpoke.timeout
64
- if ActiveRecord::Base.logger
65
- ActiveRecord::Base.logger.silence do
66
- execute("SET statement_timeout = #{timeout * 1000}")
67
- end
68
- else
69
- execute("SET statement_timeout = #{timeout * 1000}")
70
- end
71
- end
72
- end
73
- alias_method_chain :configure_connection, :statement_timeout
51
+ # bubble exceptions for error reporting libraries
52
+ ActionController::Base.send(:include, Slowpoke::Controller)
74
53
 
75
- end
54
+ # database timeout
55
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, Slowpoke::Postgres)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slowpoke
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-22 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-timeout
@@ -81,6 +81,8 @@ files:
81
81
  - lib/generators/slowpoke/install_generator.rb
82
82
  - lib/generators/slowpoke/templates/503.html
83
83
  - lib/slowpoke.rb
84
+ - lib/slowpoke/controller.rb
85
+ - lib/slowpoke/postgres.rb
84
86
  - lib/slowpoke/railtie.rb
85
87
  - lib/slowpoke/version.rb
86
88
  - slowpoke.gemspec